From 2debfe4ba8dbdd4a578a3391b23c1a11178b5f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 2 Apr 2023 17:13:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B1=D1=80=D0=B5=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/Worker/BuildLogic.cs | 1 + .../BusinessLogics/Worker/PurchaseLogic.cs | 18 ++++++++ .../BusinessLogicsContracts/IPurchaseLogic.cs | 1 + .../StoragesContracts/IPurchaseStorage.cs | 2 + .../Implements/Worker/PurchaseStorage.cs | 46 +++++++++++++------ 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs index f1cde06..c7c96bc 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs @@ -137,5 +137,6 @@ namespace HardwareShopContracts.BusinessLogicsContracts _logger.LogInformation("ReadList. Count:{Count}", list.Count); return sum; } + } } diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs index 8563aac..0e7ff6a 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs @@ -123,5 +123,23 @@ namespace HardwareShopContracts.BusinessLogicsContracts } _logger.LogInformation("Purchase. UserId:{UserId}. PurchaseID:{Id}. Sum:{ Sum}", model.UserId, model.Id, model.Sum); } + + public double Price(PurchaseSearchModel model) + { + _logger.LogInformation("ReadList. Id:{Id}", model?.Id); + var list = _purchaseStorage.GetFilteredListBuild(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return 0; + } + double sum = 0; + foreach (var build in list) + { + sum += build.Value.Item1* build.Value.Item2.Price; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return sum; + } } } diff --git a/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IPurchaseLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IPurchaseLogic.cs index 0882fad..265fadb 100644 --- a/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IPurchaseLogic.cs +++ b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IPurchaseLogic.cs @@ -8,6 +8,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts { List? ReadList(PurchaseSearchModel? model); CommentViewModel? ReadElement(CommentSearchModel model); + double Price(PurchaseSearchModel model); bool Create(PurchaseBindingModel model); bool Update(PurchaseBindingModel model); bool Delete(PurchaseBindingModel model); diff --git a/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs b/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs index 7a60b86..f75115b 100644 --- a/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs +++ b/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs @@ -1,6 +1,7 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Models; namespace HardwareShopContracts.StoragesContracts { @@ -8,6 +9,7 @@ namespace HardwareShopContracts.StoragesContracts { List GetFullList(); List GetFilteredList(PurchaseSearchModel model); + Dictionary GetFilteredListBuild(PurchaseSearchModel model); PurchaseViewModel? GetElement(PurchaseSearchModel model); PurchaseViewModel? Insert(PurchaseBindingModel model); PurchaseViewModel? Update(PurchaseBindingModel model); diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs index 24cfc9a..d241852 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs @@ -2,7 +2,9 @@ using HardwareShopContracts.SearchModels; using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.ManyToMany; using HardwareShopDatabaseImplement.Models.Worker; +using HardwareShopDataModels.Models; using Microsoft.EntityFrameworkCore; namespace HardwareShopDatabaseImplement.Implements.Worker @@ -15,6 +17,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker return context.Purchases .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .Select(x => x.GetViewModel) .ToList(); @@ -23,25 +26,36 @@ namespace HardwareShopDatabaseImplement.Implements.Worker public List GetFilteredList(PurchaseSearchModel model) { using var context = new HardwareShopDatabase(); - if (model.UserId.HasValue) + if (!model.UserId.HasValue) { - return context.Purchases - .Include(x => x.Builds) - .Include(x => x.Goods) - .Include(x => x.User) - .Where(x => x.UserId == model.UserId) - .Select(x => x.GetViewModel) - .ToList(); + return new(); } return context.Purchases - .Include(x => x.Builds) - .Include(x => x.Goods) - .Include(x => x.User) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Builds) + .Include(x => x.Goods) + .ThenInclude(x => x.Good) + .Include(x => x.User) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); } + public Dictionary GetFilteredListBuild(PurchaseSearchModel model) + { + using var context = new HardwareShopDatabase(); + var listBuilds = context.PurchasesBuilds + .Include(x => x.Build) + .Where(x => x.PurchaseId == model.Id) + .ToList(); + Dictionary? _purchaseBuilds = null; + foreach(var build in listBuilds) + { + _purchaseBuilds.Add(build.BuildId, (build.Count, build.Build)); + } + return _purchaseBuilds; + } + + public PurchaseViewModel? GetElement(PurchaseSearchModel model) { if (!model.Id.HasValue) @@ -52,6 +66,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker return context.Purchases .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; @@ -70,6 +85,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker return context.Purchases .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(x => x.Id == newPurchase.Id) ?.GetViewModel; @@ -84,6 +100,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker var purchase = context.Purchases .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(x => x.Id == model.Id); if (purchase == null) @@ -109,6 +126,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker var element = context.Purchases .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null)