From 3e68f069ce987f3234113d5a1ddd5beffb7729a4 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:06:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=83=D1=80=D1=87=D0=B0=D0=B9=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ComponentBindingModel.cs | 2 +- .../ViewModels/ComponentViewModel.cs | 2 +- .../Models/IComponentModel.cs | 2 +- .../Models/Component.cs | 5 +-- .../Models/Purchase.cs | 31 ++++++++++++------- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs index af0759b..e14244d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs @@ -7,6 +7,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs index 6aadb04..eb9fd9a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs index 6eea153..84f259d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs @@ -4,6 +4,6 @@ { string Name { get; } double Cost { get; } - int StoreKeeperId { get; } + IStoreKeeperModel StoreKeeper { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index 3089400..e26f654 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -14,8 +14,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public double Cost { get; set; } [Required] - public int StoreKeeperId { get; private set; } public virtual StoreKeeper? StoreKeeper { get; set; } + [NotMapped] + IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); @@ -30,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeperId) + StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) }; } public void Update (ComponentBindingModel model) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs index 3c5c9f0..3a60436 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -58,18 +58,25 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Cost = model.Cost, DateCreate = model.DateCreate, - Builds = model.PurchaseBuilds.Select(x => - new PurchaseBuild - { - Build = context.Builds.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList(), - Products = model.PurchaseProducts.Select(x => - new PurchaseProduct - { - Product = context.Products.First(z => z.Id == z.Key), - Count = x.Value.Item2 - }).ToList() + Builds = context.Builds + .Where(b => model.PurchaseBuilds.ContainsKey(b.Id)) + .Select(b => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = b.Id, + Build = b, + Count = model.PurchaseProducts[b.Id].Item2 + }).ToList(), + Products = context.Products + .Where(p => model.PurchaseProducts.ContainsKey(p.Id)) + .Select (p => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = p.Id, + Product = p, + Count = model.PurchaseProducts[p.Id].Item2 + }) + .ToList() }; } public void Update(PurchaseBindingModel model)