diff --git a/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs b/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs index 5c67fb7..b007899 100644 --- a/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs +++ b/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs @@ -14,7 +14,7 @@ namespace ComputerShopContracts.BindingModels public string Category { get; set; } = string.Empty; - public Dictionary AssemblyComponents { get; set; } = new(); + public Dictionary AssemblyComponents { get; set; } = new(); public Dictionary Test { get; set; } = new(); } diff --git a/ComputerShopContracts/BindingModels/ProductBindingModel.cs b/ComputerShopContracts/BindingModels/ProductBindingModel.cs index 9991cb2..1fb28ec 100644 --- a/ComputerShopContracts/BindingModels/ProductBindingModel.cs +++ b/ComputerShopContracts/BindingModels/ProductBindingModel.cs @@ -16,6 +16,6 @@ namespace ComputerShopContracts.BindingModels public int Warranty { get; set; } - public Dictionary ProductComponents { get; set; } = new(); + public Dictionary ProductComponents { get; set; } = new(); } } diff --git a/ComputerShopContracts/ViewModels/AssemblyViewModel.cs b/ComputerShopContracts/ViewModels/AssemblyViewModel.cs index 4a10b30..e536e9a 100644 --- a/ComputerShopContracts/ViewModels/AssemblyViewModel.cs +++ b/ComputerShopContracts/ViewModels/AssemblyViewModel.cs @@ -18,6 +18,6 @@ namespace ComputerShopContracts.ViewModels [DisplayName("Категория")] public string Category { get; set; } = string.Empty; - public Dictionary AssemblyComponents { get; set; } = new(); + public Dictionary AssemblyComponents { get; set; } = new(); } } diff --git a/ComputerShopContracts/ViewModels/ProductViewModel.cs b/ComputerShopContracts/ViewModels/ProductViewModel.cs index 6853f23..a5d4498 100644 --- a/ComputerShopContracts/ViewModels/ProductViewModel.cs +++ b/ComputerShopContracts/ViewModels/ProductViewModel.cs @@ -23,6 +23,6 @@ namespace ComputerShopContracts.ViewModels [DisplayName("Гарантия (мес.)")] public int Warranty { get; set; } - public Dictionary ProductComponents { get; set; } = new(); + public Dictionary ProductComponents { get; set; } = new(); } } diff --git a/ComputerShopDataModels/Models/IAssemblyModel.cs b/ComputerShopDataModels/Models/IAssemblyModel.cs index 3a0b933..8ad19f7 100644 --- a/ComputerShopDataModels/Models/IAssemblyModel.cs +++ b/ComputerShopDataModels/Models/IAssemblyModel.cs @@ -28,6 +28,6 @@ /// /// Список комплектующих /// - Dictionary AssemblyComponents { get; } + Dictionary AssemblyComponents { get; } } } diff --git a/ComputerShopDataModels/Models/IProductModel.cs b/ComputerShopDataModels/Models/IProductModel.cs index d15baca..7e82289 100644 --- a/ComputerShopDataModels/Models/IProductModel.cs +++ b/ComputerShopDataModels/Models/IProductModel.cs @@ -28,7 +28,7 @@ /// /// Список комплектующих /// - Dictionary ProductComponents { get; } + Dictionary ProductComponents { get; } /// /// Привязка товара к партии товаров diff --git a/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs b/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs index cb02afb..dfa4b99 100644 --- a/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs @@ -24,7 +24,7 @@ namespace ComputerShopDatabaseImplement.Implements { using var Context = new ComputerShopDatabase(); - // Optional search by Category name + // Optional search by Category if (!string.IsNullOrEmpty(Model.Category)) { return Context.Assemblies @@ -35,6 +35,7 @@ namespace ComputerShopDatabaseImplement.Implements .ToList(); } + // Search by UserId by default return Context.Assemblies .Include(x => x.Components) .ThenInclude(x => x.Component) diff --git a/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs index ef48970..6a558b5 100644 --- a/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -106,7 +106,7 @@ namespace ComputerShopDatabaseImplement.Implements ComponentName = x.ComponentName, ComponentCost = x.Cost, Shipments = x.ProductComponents - .Select(y => (y.Count, y.Product.ProductName, y.Product.Price, y.Product.Shipment!.ProviderName, y.Product.Shipment.DateShipment)) + .Select(y => (0, y.Product.ProductName, y.Product.Price, y.Product.Shipment!.ProviderName, y.Product.Shipment.DateShipment)) .ToList(), }) .ToList(); diff --git a/ComputerShopDatabaseImplement/Implements/ProductStorage.cs b/ComputerShopDatabaseImplement/Implements/ProductStorage.cs index fa5ced6..ea1db42 100644 --- a/ComputerShopDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/ProductStorage.cs @@ -37,6 +37,7 @@ namespace ComputerShopDatabaseImplement.Implements .ToList(); } + // Search by UserId by default return Context.Products .Include(x => x.Shipment) .Include(x => x.Components) diff --git a/ComputerShopDatabaseImplement/Models/Assembly.cs b/ComputerShopDatabaseImplement/Models/Assembly.cs index 7b470ae..4cbe166 100644 --- a/ComputerShopDatabaseImplement/Models/Assembly.cs +++ b/ComputerShopDatabaseImplement/Models/Assembly.cs @@ -28,18 +28,18 @@ namespace ComputerShopDatabaseImplement.Models [ForeignKey("AssemblyId")] public virtual List Components { get; set; } = new(); - private Dictionary? _assemblyComponents; + private Dictionary? _assemblyComponents; [NotMapped] - public Dictionary AssemblyComponents + public Dictionary AssemblyComponents { get { if (_assemblyComponents == null) { _assemblyComponents = Components.ToDictionary( - AsmComp => AsmComp.ComponentId, - AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count) + AsmComp => AsmComp.ComponentId, + AsmComp => AsmComp.Component as IComponentModel ); } @@ -49,18 +49,22 @@ namespace ComputerShopDatabaseImplement.Models public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model) { - return new() + var Components = Model.AssemblyComponents + .Select(x => new AssemblyComponent + { + Component = Context.Components.First(y => y.Id == x.Key) + }) + .ToList(); + double Price = Components.Sum(x => x.Component.Cost); + + return new Assembly() { Id = Model.Id, UserId = Model.UserId, AssemblyName = Model.AssemblyName, - Price = Model.Price, + Price = Price, Category = Model.Category, - Components = Model.AssemblyComponents.Select(x => new AssemblyComponent - { - Component = Context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList(), + Components = Components, }; } @@ -69,14 +73,12 @@ namespace ComputerShopDatabaseImplement.Models if (!string.IsNullOrEmpty(Model.AssemblyName)) { AssemblyName = Model.AssemblyName; - } + } if (!string.IsNullOrEmpty(Model.Category)) { Category = Model.Category; } - - Price = Model.Price; } public AssemblyViewModel ViewModel => new() @@ -91,17 +93,22 @@ namespace ComputerShopDatabaseImplement.Models public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model) { - var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList(); + // Сначала подсчитывается новая цена, т.к. Model.AssemblyComponents далее может измениться + double NewPrice = Context.Components + .Where(x => Model.AssemblyComponents.ContainsKey(x.Id)) + .Sum(x => x.Cost); + + var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList(); if (AssemblyComponents != null && AssemblyComponents.Count > 0) { + // Удаление записей из таблицы AssemblyComponents тех компонентов, которых нет в модели Context.AssemblyComponents - .RemoveRange(AssemblyComponents - .Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId))); + .RemoveRange(AssemblyComponents.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId))); Context.SaveChanges(); - foreach (var ComponentToUpdate in AssemblyComponents) + // После этого в Model.AssemblyComponents останутся только те компоненты, записей о которых еще нет в БД + foreach (var ComponentToUpdate in AssemblyComponents) { - ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2; Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId); } @@ -115,13 +122,31 @@ namespace ComputerShopDatabaseImplement.Models { Assembly = CurrentAssembly, Component = Context.Components.First(x => x.Id == AssemblyComponent.Key), - Count = AssemblyComponent.Value.Item2 }); Context.SaveChanges(); } + Price = NewPrice; + Context.SaveChanges(); + _assemblyComponents = null; } + + private void CalculatePrice( + ComputerShopDatabase Context, + Dictionary ModelComponents, + out List OutComponents, + out double OutPrice) + { + OutComponents = ModelComponents + .Select(x => new AssemblyComponent + { + Component = Context.Components.First(y => y.Id == x.Key) + }) + .ToList(); + + OutPrice = Components.Sum(x => x.Component.Cost); + } } } diff --git a/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs b/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs index ea0c084..b66dab7 100644 --- a/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs +++ b/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs @@ -12,9 +12,6 @@ namespace ComputerShopDatabaseImplement.Models [Required] public int ComponentId { get; set; } - [Required] - public int Count { get; set; } - public virtual Assembly Assembly { get; set; } = new(); public virtual Component Component { get; set; } = new(); diff --git a/ComputerShopDatabaseImplement/Models/Product.cs b/ComputerShopDatabaseImplement/Models/Product.cs index 924a19d..24df409 100644 --- a/ComputerShopDatabaseImplement/Models/Product.cs +++ b/ComputerShopDatabaseImplement/Models/Product.cs @@ -29,10 +29,10 @@ namespace ComputerShopDatabaseImplement.Models [ForeignKey("ProductId")] public virtual List Components { get; set; } = new(); - private Dictionary? _productComponents; + private Dictionary? _productComponents; [NotMapped] - public Dictionary ProductComponents + public Dictionary ProductComponents { get { @@ -40,7 +40,7 @@ namespace ComputerShopDatabaseImplement.Models { _productComponents = Components.ToDictionary( ProdComp => ProdComp.ComponentId, - ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count) + ProdComp => ProdComp.Component as IComponentModel ); } @@ -50,7 +50,15 @@ namespace ComputerShopDatabaseImplement.Models public static Product Create(ComputerShopDatabase Context, ProductBindingModel Model) { - return new() + var Components = Model.ProductComponents + .Select(x => new ProductComponent + { + Component = Context.Components.First(y => y.Id == x.Key) + }) + .ToList(); + double Price = Components.Sum(x => x.Component.Cost); + + return new Product() { Id = Model.Id, UserId = Model.UserId, @@ -58,11 +66,7 @@ namespace ComputerShopDatabaseImplement.Models ProductName = Model.ProductName, Price = Model.Price, Warranty = Model.Warranty, - Components = Model.ProductComponents.Select(x => new ProductComponent - { - Component = Context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList(), + Components = Components, }; } @@ -74,7 +78,6 @@ namespace ComputerShopDatabaseImplement.Models } ShipmentId = Model.ShipmentId; - Price = Model.Price; Warranty = Model.Warranty; } @@ -91,37 +94,44 @@ namespace ComputerShopDatabaseImplement.Models public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model) { - var ProductComponents = Context.ProductComponents.Where(x => x.ProductId == Model.Id).ToList(); - if (ProductComponents != null && ProductComponents.Count > 0) - { - Context.ProductComponents - .RemoveRange(ProductComponents - .Where(x => !Model.ProductComponents.ContainsKey(x.ComponentId))); - Context.SaveChanges(); + // Сначала подсчитывается новая цена, т.к. Model.ProductComponents далее может измениться + double NewPrice = Context.Components + .Where(x => Model.ProductComponents.ContainsKey(x.Id)) + .Sum(x => x.Cost); - foreach (var ComponentToUpdate in ProductComponents) - { - ComponentToUpdate.Count = Model.ProductComponents[ComponentToUpdate.ComponentId].Item2; - Model.ProductComponents.Remove(ComponentToUpdate.ComponentId); - } + var ProductComponents = Context.ProductComponents.Where(x => x.ProductId == Model.Id).ToList(); + if (ProductComponents != null && ProductComponents.Count > 0) + { + // Удаление записей из таблицы ProductComponents тех компонентов, которых нет в модели + Context.ProductComponents + .RemoveRange(ProductComponents.Where(x => !Model.ProductComponents.ContainsKey(x.ComponentId))); + Context.SaveChanges(); - Context.SaveChanges(); - } + // После этого в Model.ProductComponents останутся только те компоненты, записей о которых еще нет в БД + foreach (var ComponentToUpdate in ProductComponents) + { + Model.ProductComponents.Remove(ComponentToUpdate.ComponentId); + } - var CurrentProduct = Context.Products.First(x => x.Id == Id); - foreach (var ProductComponent in Model.ProductComponents) - { - Context.ProductComponents.Add(new ProductComponent - { - Product = CurrentProduct, - Component = Context.Components.First(x => x.Id == ProductComponent.Key), - Count = ProductComponent.Value.Item2 - }); + Context.SaveChanges(); + } - Context.SaveChanges(); - } + var CurrentProduct = Context.Products.First(x => x.Id == Id); + foreach (var ProductComponent in Model.ProductComponents) + { + Context.ProductComponents.Add(new ProductComponent + { + Product = CurrentProduct, + Component = Context.Components.First(x => x.Id == ProductComponent.Key), + }); - _productComponents = null; - } + Context.SaveChanges(); + } + + Price = NewPrice; + Context.SaveChanges(); + + _productComponents = null; + } } } diff --git a/ComputerShopDatabaseImplement/Models/ProductComponent.cs b/ComputerShopDatabaseImplement/Models/ProductComponent.cs index 10bf2ac..61b3371 100644 --- a/ComputerShopDatabaseImplement/Models/ProductComponent.cs +++ b/ComputerShopDatabaseImplement/Models/ProductComponent.cs @@ -12,9 +12,6 @@ namespace ComputerShopDatabaseImplement.Models [Required] public int ComponentId { get; set; } - [Required] - public int Count { get; set; } - public virtual Product Product { get; set; } = new(); public virtual Component Component { get; set; } = new(); diff --git a/ComputerShopDatabaseImplement/Models/User.cs b/ComputerShopDatabaseImplement/Models/User.cs index d5f749e..10ccceb 100644 --- a/ComputerShopDatabaseImplement/Models/User.cs +++ b/ComputerShopDatabaseImplement/Models/User.cs @@ -2,17 +2,12 @@ using ComputerShopContracts.ViewModels; using ComputerShopDataModels.Enums; using ComputerShopDataModels.Models; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ComputerShopDatabaseImplement.Models { - public class User : IUserModel + public class User : IUserModel { public int Id { get; private set; } @@ -30,19 +25,21 @@ namespace ComputerShopDatabaseImplement.Models [ForeignKey("UserId")] public virtual List Shipments { get; set; } = new(); + [ForeignKey("UserId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("UserId")] public virtual List Requests { get; set; } = new(); [ForeignKey("UserId")] public virtual List Assemblies { get; set; } = new(); + [ForeignKey("UserId")] public virtual List Components { get; set; } = new(); [ForeignKey("UserId")] - public virtual List Proucts { get; set; } = new(); - + public virtual List Products { get; set; } = new(); public static User Create(UserBindingModel model) {