Coursework_ComputerStore_Li.../ComputerStoreDatabaseImplement/Models/Consignment.cs
2023-04-07 20:46:13 +04:00

96 lines
2.8 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;
namespace ComputerStoreDatabaseImplement.Models
{
public class Consignment : IConsignmentModel
{
public int ID { get; private set; }
[Required]
public int OrderID { get; private set; }
[Required]
public double Price { get; private set; }
private Dictionary<int, (IProductModel, int)>? _consignmentProducts = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> ConsignmentProducts
{
get
{
if (_consignmentProducts == null)
{
_consignmentProducts = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC.Product as IProductModel, recPC.Count));
}
return _consignmentProducts;
}
}
[ForeignKey("ConsignmentId")]
public virtual List<CarComponent> Components { get; set; } = new();
public virtual Product Product { get; private set; }
private Dictionary<int, (IProductModel, int)>? Products = null;
[ForeignKey("ConsignmentID")]
public virtual List<ConsignmentComponent> ConsignmentComponent { get; private set; } = new();
public static Consignment Create(ComputerStoreDatabase context, ConsignmentBindingModel model)
{
if (model == null)
{
return null;
}
return new Consignment()
{
ID = model.ID,
OrderID = model.OrderID,
Price = model.Price,
_orderProducts = model.ConsignmentProducts
};
//return new Consignment()
//{
// ID = model.ID,
// OrderID = model.OrderID,
// Price = model.Price,
// Products = model.ConsignmentProducts.Select(x => new ConsignmentComponent
// {
// Component = context.Components.First(y => y.ID == x.Key),
// Count = x.Value.Item2
// }).ToList()
//};
}
public void Update(ConsignmentBindingModel? model)
{
if (model == null)
{
return;
}
OrderID = model.OrderID;
Price = model.Price;
}
public ConsignmentViewModel GetViewModel => new()
{
ID = ID,
OrderID = OrderID,
Price = Price
};
}
}