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

99 lines
3.0 KiB
C#

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;
using Azure.Core;
using ComputerStoreContracts.SearchModels;
using Microsoft.EntityFrameworkCore;
using System.Xml.Linq;
namespace ComputerStoreDatabaseImplement.Models
{
public class Request : IRequestModel
{
public int ID { get; private set; }
public int? OrderID { get; private set; }
public virtual Order? Order { get; set; }
public int? PCID { get; private set; }
[Required]
public double Price { get; private set; }
[ForeignKey("RequestID")]
public virtual List<RequestComponent> PCs { get; set; } = new();
[ForeignKey("RequestID")]
public virtual List<PC> PersonalComputers { get; set; } = new();
[NotMapped]
public List<(IComponentModel, int)> RequestComponents { get; set; } = new();
//[NotMapped]
public List<(IComponentModel, int)> RequestComponentsTest()
{
var tmp = PCs.Select(x => (x.Component as IComponentModel, x.Count)).ToList();
return PCs.Select(x => (x.Component as IComponentModel, x.Count)).ToList();
}
//public List<(IComponentModel, int)> RequestComponents { get; set; } = new();
//Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC.Product as IProductModel, recPC.Count));
public static Request? Create(ComputerStoreDatabase context, RequestBindingModel model)
{
if (model == null)
{
return null;
}
return new Request()
{
ID = model.ID,
OrderID = model.OrderID,
Price = model.Price,
};
}
public void Update(RequestBindingModel model)
{
if (model == null)
{
return;
}
Price = model.Price;
PCID = model.PCID;
OrderID = model.OrderID;
//RequestComponents = model.RequestComponents;
}
public void UpdatePCID(ComputerStoreDatabase context)
{
PCID = context.RequestComponents.First(x => x.RequestID == ID).PCID;
}
public RequestViewModel GetViewModel
{
get
{
var context = new ComputerStoreDatabase();
return new RequestViewModel()
{
ID = ID,
OrderID = OrderID,
Price = Price,
PCID = PCID,
PCName = context.PCS.FirstOrDefault(rec => rec.ID == PCID)?.Name,
RequestComponents = RequestComponentsTest(),
};
}
}
}
}