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]
|
2024-04-29 19:33:15 +04:00
|
|
|
|
public DateTime DateCreate { get; set; } = DateTime.Now;
|
2024-05-25 17:47:14 +04:00
|
|
|
|
|
|
|
|
|
public Dictionary<int, (IProductModel, int)>? _productList = null;
|
2024-04-29 01:55:20 +04:00
|
|
|
|
|
2024-04-29 18:34:49 +04:00
|
|
|
|
[NotMapped]
|
2024-05-25 17:47:14 +04:00
|
|
|
|
public Dictionary<int, (IProductModel, int)> ProductList
|
2024-04-29 18:34:49 +04:00
|
|
|
|
{
|
|
|
|
|
get {
|
2024-05-25 17:47:14 +04:00
|
|
|
|
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-25 22:32:22 +04:00
|
|
|
|
[ForeignKey("ProductID")]
|
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")]
|
|
|
|
|
public virtual List<Payment> Payments { 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,
|
|
|
|
|
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-25 22:32:22 +04:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-05-25 17:47:14 +04:00
|
|
|
|
}
|
2024-04-29 01:55:20 +04:00
|
|
|
|
}
|