Coursework_ComputerStore_Li.../ComputerStoreDatabaseImplement/Models/Request.cs

89 lines
2.5 KiB
C#
Raw Normal View History

2023-04-07 20:46:13 +04:00
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-04-09 01:07:38 +04:00
using Azure.Core;
using ComputerStoreContracts.SearchModels;
using Microsoft.EntityFrameworkCore;
using System.Xml.Linq;
2023-04-07 20:46:13 +04:00
namespace ComputerStoreDatabaseImplement.Models
{
public class Request : IRequestModel
{
public int ID { get; private set; }
2023-05-20 05:52:38 +04:00
public int? OrderID { get; private set; }
2023-04-09 01:07:38 +04:00
public virtual Order Order { get; set; }
public int? PCID { get; private set; }
2023-04-07 20:46:13 +04:00
[Required]
public double Price { get; private set; }
[ForeignKey("RequestID")]
2023-04-09 01:07:38 +04:00
public virtual List<RequestComponent> PCs { get; set; } = new();
2023-04-07 20:46:13 +04:00
2023-05-18 02:56:09 +04:00
[ForeignKey("RequestID")]
public virtual List<PC> PersonalComputers { get; set; } = new();
2023-05-19 16:11:31 +04:00
[NotMapped]
public List<(IComponentModel, int)> RequestComponents { get; set; } = new();
2023-04-09 01:07:38 +04:00
public static Request? Create(ComputerStoreDatabase context, RequestBindingModel model)
2023-04-07 20:46:13 +04:00
{
if (model == null)
{
return null;
}
2023-05-19 16:11:31 +04:00
2023-04-07 20:46:13 +04:00
return new Request()
{
ID = model.ID,
OrderID = model.OrderID,
2023-05-19 16:11:31 +04:00
Price = model.Price,
RequestComponents = model.RequestComponents,
2023-04-07 20:46:13 +04:00
};
}
2023-04-09 01:07:38 +04:00
public void Update(RequestBindingModel model)
2023-04-07 20:46:13 +04:00
{
if (model == null)
{
return;
}
Price = model.Price;
2023-04-09 01:07:38 +04:00
PCID = model.PCID;
2023-05-20 05:52:38 +04:00
OrderID = model.OrderID;
2023-05-19 16:11:31 +04:00
RequestComponents = model.RequestComponents;
2023-04-09 01:07:38 +04:00
}
public void UpdatePCID(ComputerStoreDatabase context)
{
PCID = context.RequestComponents.First(x => x.RequestID == ID).PCID;
2023-04-07 20:46:13 +04:00
}
2023-04-09 01:07:38 +04:00
public RequestViewModel GetViewModel
2023-04-07 20:46:13 +04:00
{
2023-04-09 01:07:38 +04:00
get
{
var context = new ComputerStoreDatabase();
return new RequestViewModel()
{
ID = ID,
OrderID = OrderID,
Price = Price,
PCID = PCID,
2023-05-19 16:11:31 +04:00
PCName = context.PCS.FirstOrDefault(rec => rec.ID == PCID)?.Name
2023-04-09 01:07:38 +04:00
};
}
}
2023-04-07 20:46:13 +04:00
}
}