From 7f9720200f6893faf2228738c6abe4a3f05c913a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=A4=D0=B5=D0=B4=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Sat, 25 May 2024 22:32:22 +0400 Subject: [PATCH] fix Db models --- .../BindingModels/OrderBindingModel.cs | 2 - .../SearchModels/OrderSearchModel.cs | 1 - .../ViewModels/OrderViewModel.cs | 4 +- .../Models/Client.cs | 18 +++--- .../Models/CostItem.cs | 4 +- .../Models/Employee.cs | 7 ++- .../Models/Order.cs | 59 ++++++++++--------- .../Models/OrderProduct.cs | 3 + .../Models/Payment.cs | 7 ++- .../Models/Product.cs | 12 ++-- .../Models/IOrderModel.cs | 1 - .../Models/IPaymentModel.cs | 1 - 12 files changed, 67 insertions(+), 52 deletions(-) diff --git a/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs b/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs index 197eb9a..ef908a3 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs @@ -14,8 +14,6 @@ namespace ElectronicsShopContracts.BindingModels public int ClientID { get; set; } - public int? EmployeeID { get; set; } - public DateTime DateCreate { get; set; } = DateTime.Now; public double Sum { get; set; } diff --git a/ElectronicsShop/ElectronicsShopContracts/SearchModels/OrderSearchModel.cs b/ElectronicsShop/ElectronicsShopContracts/SearchModels/OrderSearchModel.cs index eba74b6..8c05d59 100644 --- a/ElectronicsShop/ElectronicsShopContracts/SearchModels/OrderSearchModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/SearchModels/OrderSearchModel.cs @@ -12,7 +12,6 @@ namespace ElectronicsShopContracts.SearchModels { public int? ID { get; set; } public int? ClientID { get; set; } - public int? EmployeeID { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set;} } diff --git a/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs b/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs index e42e81b..5792c64 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs @@ -15,15 +15,13 @@ namespace ElectronicsShopContracts.ViewModels public int ClientID { get; set; } - public int? EmployeeID { get; set; } - [DisplayName("Дата заказа")] public DateTime DateCreate { get; set; } = DateTime.Now; [DisplayName("Сумма заказа")] public double Sum { get; set; } - [DisplayName("Список продуктов")] + [DisplayName("Список товаров")] public Dictionary ProductList { get; set; } = new(); } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Client.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Client.cs index dde2ea6..644de7b 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Client.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Client.cs @@ -11,18 +11,23 @@ using System.Threading.Tasks; namespace ElectronicsShopDataBaseImplement.Models { - public class Client : IClientModel - { + public class Client : IClientModel { public int ID { get; set; } + [Required] - public string ClientFIO { get; set; }=string.Empty; + public string ClientFIO { get; set; } = string.Empty; + [Required] public string Password { get; set; } = string.Empty; - [Required] + + [Required] public string Email { get; set; } = string.Empty; + //незаконная магия ленивой загрузки + [ForeignKey("ClientID")] + public virtual List Orders { get; set; } = new(); - public static Client? Create(ClientBindingModel model) + public static Client? Create(ClientBindingModel? model) { if(model == null) { @@ -37,8 +42,7 @@ namespace ElectronicsShopDataBaseImplement.Models }; } - - public void Update(ClientBindingModel model) + public void Update(ClientBindingModel? model) { if (model == null) { diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs index 93eb180..a992d97 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs @@ -17,8 +17,10 @@ namespace ElectronicsShopDataBaseImplement.Models [ForeignKey("EmployeeID")] public int EmployeeID { get; set; } + [Required] public string Name { get; set; }= string.Empty; + [Required] public double Price { get; set; } @@ -36,7 +38,7 @@ namespace ElectronicsShopDataBaseImplement.Models Price = model.Price, }; } - public void Update(CostItemBindingModel model) + public void Update(CostItemBindingModel? model) { if (model == null) { diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Employee.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Employee.cs index a153476..12955ef 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Employee.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Employee.cs @@ -15,10 +15,13 @@ namespace ElectronicsShopDataBaseImplement.Models public class Employee : IEmployeeModel { public int ID { get; set; } + [Required] - public string EmployeeFIO { get; set; }=string.Empty; + public string EmployeeFIO { get; set; } = string.Empty; + [Required] public string Login { get; set; } = string.Empty; + [Required] public string Password { get; set; } = string.Empty; @@ -36,7 +39,7 @@ namespace ElectronicsShopDataBaseImplement.Models Password = model.Password, }; } - public void Update(EmployeeBindingModel model) + public void Update(EmployeeBindingModel? model) { if (model == null) { diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs index 3798406..f41d634 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs @@ -2,6 +2,7 @@ using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataModels.Enums; using ElectronicsShopDataModels.Models; +using Microsoft.EntityFrameworkCore.Migrations; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -16,12 +17,13 @@ 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; } - [ForeignKey("EmployeeID")] - public int? EmployeeID { get; set; } + [Required] public DateTime DateCreate { get; set; } = DateTime.Now; @@ -38,8 +40,12 @@ namespace ElectronicsShopDataBaseImplement.Models } } + [ForeignKey("ProductID")] public virtual List Products { get; set; } = new(); + [ForeignKey("PaymentID")] + public virtual List Payments { get; set; } = new(); + public static Order? Create(Database context ,OrderBindingModel? model) { if (model == null) @@ -51,7 +57,6 @@ namespace ElectronicsShopDataBaseImplement.Models ID = model.ID, ClientID=model.ClientID, Sum = model.Sum, - EmployeeID=model.EmployeeID, DateCreate = model.DateCreate, Products = model.ProductList.Select(x => new OrderProduct { @@ -60,38 +65,36 @@ namespace ElectronicsShopDataBaseImplement.Models }).ToList() }; } - public void Update(Database context,OrderBindingModel model) - { - if (model == null) - { - return; - } - ClientID = model.ClientID; - Sum = model.Sum; - EmployeeID = model.EmployeeID; - DateCreate = model.DateCreate; - Products = model.ProductList.Select(x => new OrderProduct - { - _product = context.Products.First(y => y.ID == x.Key), - Count = x.Value.Item2 - }).ToList(); - //todo ProductList - } public OrderViewModel GetViewModel => new() { ID = ID, ClientID = ClientID, Sum = Sum, - EmployeeID = EmployeeID, DateCreate = DateCreate, - /* - ProductList = ProductList.Select(x => new OrderProducts - { - /_product = context.Products.First(y => y.ID == x.Key) - Count = x.Value.Item2 - }).ToList() - */ }; + + 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; + } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs index a6a1728..2b7f967 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs @@ -20,7 +20,10 @@ namespace ElectronicsShopDataBaseImplement.Models [Required] public int Count { get; set; } + [ForeignKey("OrderID")] public virtual Order _order { get; set; } = new(); + + [ForeignKey("ProductID")] public virtual Product _product { get; set; } = new(); } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Payment.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Payment.cs index 73156d4..6b2cb49 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Payment.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Payment.cs @@ -16,14 +16,19 @@ namespace ElectronicsShopDataBaseImplement.Models public class Payment : IPaymentModel { public int ID { get; set; } + [ForeignKey("ProductID")] public int ProductID { get; set; } + [ForeignKey("OrderID")] public int OrderID { get; set; } + [Required] public double SumPayment { get; set; } + [Required] public PaymeantOption PayOption { get; set; } = PaymeantOption.Неоплачено; + public static Payment? Create(PaymentBindingModel? model) { if (model == null) @@ -39,7 +44,7 @@ namespace ElectronicsShopDataBaseImplement.Models PayOption = model.PayOption, }; } - public void Update(PaymentBindingModel model) + public void Update(PaymentBindingModel? model) { if (model == null) { diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs index 94c9ac0..9fa3923 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs @@ -15,10 +15,13 @@ namespace ElectronicsShopDataBaseImplement.Models public class Product : IProductModel { public int ID { get; set; } + [Required] - public string ProductName { get; set; }=string.Empty; + public string ProductName { get; set; } = string.Empty; + [Required] public double Price { get; set; } + [ForeignKey("CostItemID")] public int CostItemID { get; set; } @@ -28,15 +31,14 @@ namespace ElectronicsShopDataBaseImplement.Models { return null; } - return new Product() - { + return new Product() { ID = model.ID, ProductName = model.ProductName, Price = model.Price, - CostItemID=model.CostItemID, + CostItemID = model.CostItemID, }; } - public void Update(ProductBindingModel model) + public void Update(ProductBindingModel? model) { if (model == null) { diff --git a/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs b/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs index dee68ac..3ef9849 100644 --- a/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs +++ b/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs @@ -10,7 +10,6 @@ namespace ElectronicsShopDataModels.Models public interface IOrderModel : IID { int ClientID { get; } - int? EmployeeID { get; } DateTime DateCreate { get; } double Sum { get; } diff --git a/ElectronicsShop/ElectronicsShopDataModels/Models/IPaymentModel.cs b/ElectronicsShop/ElectronicsShopDataModels/Models/IPaymentModel.cs index 6d11c0f..269c5e4 100644 --- a/ElectronicsShop/ElectronicsShopDataModels/Models/IPaymentModel.cs +++ b/ElectronicsShop/ElectronicsShopDataModels/Models/IPaymentModel.cs @@ -14,6 +14,5 @@ namespace ElectronicsShopDataModels.Models int OrderID { get; } double SumPayment { get; } PaymeantOption PayOption { get; } - } }