CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs

102 lines
3.4 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;
2024-05-25 22:32:22 +04:00
using Microsoft.EntityFrameworkCore.Migrations;
2024-04-29 01:55:20 +04:00
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; }
2024-05-25 22:32:22 +04:00
2024-04-29 01:55:20 +04:00
[Required]
2024-04-29 17:59:45 +04:00
public double Sum { get; set; }
2024-05-25 22:32:22 +04:00
2024-05-19 17:52:59 +04:00
[ForeignKey("ClientID")]
public int ClientID { get; set; }
2024-05-25 22:32:22 +04:00
2024-04-29 01:55:20 +04:00
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public Dictionary<int, (IProductModel, int)>? _productList = null;
2024-04-29 01:55:20 +04:00
2024-04-29 18:34:49 +04:00
[NotMapped]
public Dictionary<int, (IProductModel, int)> ProductList
2024-04-29 18:34:49 +04:00
{
get {
if (_productList == null) {
_productList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count));
2024-04-29 18:34:49 +04:00
}
return _productList;
}
}
2024-05-31 16:05:43 +04:00
[ForeignKey("OrderID")]
2024-05-25 13:06:38 +04:00
public virtual List<OrderProduct> Products { get; set; } = new();
2024-04-29 18:34:49 +04:00
2024-05-25 22:32:22 +04:00
[ForeignKey("PaymentID")]
2024-05-26 18:12:25 +04:00
public virtual List<Paymeant> Payments { get; set; } = new();
2024-05-25 22:32:22 +04:00
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,
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
};
}
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,
DateCreate = DateCreate,
2024-05-31 16:05:43 +04:00
ProductList = ProductList,
2024-04-29 01:55:20 +04:00
};
2024-05-25 22:32:22 +04:00
public void UpdateProducts(Database context,OrderBindingModel model) {
2024-05-31 16:05:43 +04:00
var orderProducts = context.OrderProducts.Where(rec => rec.OrderID == model.ID).ToList();
2024-05-25 22:32:22 +04:00
if (orderProducts != null && orderProducts.Count > 0) {
context.OrderProducts.RemoveRange(orderProducts.Where(rec => !model.ProductList.ContainsKey(rec.ProductID)));
context.SaveChanges();
foreach (var updateProducts in orderProducts) {
updateProducts.Count = model.ProductList[updateProducts.ProductID].Item2;
model.ProductList.Remove(updateProducts.ProductID);
}
context.SaveChanges();
}
2024-05-31 16:05:43 +04:00
var order = context.Orders.First(x => x.ID == model.ID);
2024-05-25 22:32:22 +04:00
foreach (var op in model.ProductList) {
context.OrderProducts.Add(new OrderProduct {
_order = order,
_product = context.Products.First(x => x.ID == op.Key),
Count = op.Value.Item2
});
context.SaveChanges();
}
_productList = null;
}
}
2024-04-29 01:55:20 +04:00
}