CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs

98 lines
3.1 KiB
C#
Raw Normal View History

2024-04-29 01:55:20 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using ElectronicsShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
2024-04-29 18:34:49 +04:00
using System.ComponentModel.DataAnnotations.Schema;
2024-04-29 01:55:20 +04:00
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Models
{
public class Order : IOrderModel
{
public int ID { get; set; }
[Required]
2024-04-29 17:59:45 +04:00
public double Sum { get; set; }
2024-05-19 17:52:59 +04:00
[ForeignKey("ClientID")]
public int ClientID { get; set; }
2024-05-25 13:06:38 +04:00
[ForeignKey("EmployeeID")]
public int? EmployeeID { get; set; }
2024-04-29 01:55:20 +04:00
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
2024-04-29 01:55:20 +04:00
[Required]
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();
2024-04-29 18:34:49 +04:00
[NotMapped]
public Dictionary<int, (IProductModel, int)> _productList
{
get {
if (ProductList == null) {
ProductList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count));
}
return _productList;
}
}
2024-05-25 13:06:38 +04:00
public virtual List<OrderProduct> Products { get; set; } = new();
2024-04-29 18:34:49 +04:00
public static Order? Create(Database context ,OrderBindingModel? model)
2024-04-29 01:55:20 +04:00
{
if (model == null)
{
return null;
}
return new Order()
{
ID = model.ID,
2024-05-19 17:52:59 +04:00
ClientID=model.ClientID,
2024-04-29 01:55:20 +04:00
Sum = model.Sum,
2024-05-25 13:06:38 +04:00
EmployeeID=model.EmployeeID,
2024-04-29 01:55:20 +04:00
DateCreate = model.DateCreate,
2024-05-25 13:06:38 +04:00
Products = model.ProductList.Select(x => new OrderProduct
2024-04-29 18:34:49 +04:00
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
2024-04-29 01:55:20 +04:00
};
}
2024-05-25 13:06:38 +04:00
public void Update(Database context,OrderBindingModel model)
2024-04-29 01:55:20 +04:00
{
if (model == null)
{
return;
}
2024-05-19 17:52:59 +04:00
ClientID = model.ClientID;
2024-04-29 01:55:20 +04:00
Sum = model.Sum;
2024-05-25 13:06:38 +04:00
EmployeeID = model.EmployeeID;
2024-04-29 01:55:20 +04:00
DateCreate = model.DateCreate;
2024-05-25 13:06:38 +04:00
Products = model.ProductList.Select(x => new OrderProduct
2024-04-29 01:55:20 +04:00
{
2024-05-25 13:06:38 +04:00
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList();
2024-04-29 01:55:20 +04:00
//todo ProductList
}
public OrderViewModel GetViewModel => new()
{
ID = ID,
2024-05-19 17:52:59 +04:00
ClientID = ClientID,
2024-04-29 01:55:20 +04:00
Sum = Sum,
2024-05-25 13:06:38 +04:00
EmployeeID = EmployeeID,
2024-04-29 01:55:20 +04:00
DateCreate = DateCreate,
2024-05-25 13:06:38 +04:00
/*
ProductList = ProductList.Select(x => new OrderProducts
{
/_product = context.Products.First(y => y.ID == x.Key)
Count = x.Value.Item2
}).ToList()
*/
2024-04-29 01:55:20 +04:00
};
}
}