CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs
Илья Федотов e07a12bf92 set paymeant
2024-05-26 18:12:25 +04:00

101 lines
3.4 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using ElectronicsShopDataModels.Models;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
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]
public double Sum { get; set; }
[ForeignKey("ClientID")]
public int ClientID { get; set; }
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public Dictionary<int, (IProductModel, int)>? _productList = null;
[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;
}
}
[ForeignKey("ProductID")]
public virtual List<OrderProduct> Products { get; set; } = new();
[ForeignKey("PaymentID")]
public virtual List<Paymeant> Payments { get; set; } = new();
public static Order? Create(Database context ,OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
ID = model.ID,
ClientID=model.ClientID,
Sum = model.Sum,
DateCreate = model.DateCreate,
Products = model.ProductList.Select(x => new OrderProduct
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public OrderViewModel GetViewModel => new()
{
ID = ID,
ClientID = ClientID,
Sum = Sum,
DateCreate = DateCreate,
};
public void UpdateProducts(Database context,OrderBindingModel model) {
var orderProducts = context.OrderProducts.Where(rec => rec.ProductID == model.ID).ToList();
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();
}
var order = context.Orders.First(x => x.ID == ID);
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;
}
}
}