From 65a22bb3f6d9bed59e1362a63345f4dae55cc4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Fri, 7 Apr 2023 08:22:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=B2=D1=8F=D0=B7=D0=BA=D0=B8=20=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=D0=B8=20=D0=B7=D0=B0=D1=82=D1=80=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ItemForRepairLogic.cs | 33 ++++++++++++++++++- .../StorageContracts/IItemStorage.cs | 2 ++ .../Implements/ItemStorage.cs | 16 +++++++++ CarService/CarServiceDatabase/Models/Item.cs | 8 +++++ CarService/CarServiceView/Program.cs | 2 +- 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/CarService/CarServiceBusinessLogic/BusinessLogics/ItemForRepairLogic.cs b/CarService/CarServiceBusinessLogic/BusinessLogics/ItemForRepairLogic.cs index 007859e..57f1549 100644 --- a/CarService/CarServiceBusinessLogic/BusinessLogics/ItemForRepairLogic.cs +++ b/CarService/CarServiceBusinessLogic/BusinessLogics/ItemForRepairLogic.cs @@ -11,10 +11,12 @@ namespace CarServiceBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IItemForRepairStorage _itemForRepairStorage; - public ItemForRepairLogic(ILogger logger, IItemForRepairStorage itemForRepairStorage) + private readonly IItemStorage _itemStorage; + public ItemForRepairLogic(ILogger logger, IItemForRepairStorage itemForRepairStorage, IItemStorage itemStorage) { _logger = logger; _itemForRepairStorage = itemForRepairStorage; + _itemStorage = itemStorage; } public List? ReadList(ItemForRepairSearchModel? model) { @@ -47,6 +49,13 @@ namespace CarServiceBusinessLogic.BusinessLogics public bool Create(ItemForRepairBindingModel model) { CheckModel(model); + //При добавлении привязки уменьшаем количество нужной статьи затрат + var item = _itemStorage.GetElement(new() { Id = model.ItemId }); + if (_itemStorage.UpdateCount(item, -model.Count) == null) + { + _logger.LogWarning("Insert operation failed while updating count"); + return false; + } if (_itemForRepairStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); @@ -57,6 +66,20 @@ namespace CarServiceBusinessLogic.BusinessLogics public bool Update(ItemForRepairBindingModel model) { CheckModel(model); + //Возвращаем нужное количество старых статей затрат на склад + var oldItem = _itemStorage.GetElement(new() { Id = (_itemForRepairStorage.GetElement(new() { Id = model.ItemId })?.ItemId ?? -1) }); + if (_itemStorage.UpdateCount(oldItem, model.Count) == null) + { + _logger.LogWarning("Insert operation failed while returning old intem on storage"); + return false; + } + //Списываем новую статью затрат со склада в новом количестве + var newItem = _itemStorage.GetElement(new() { Id = model.ItemId }); + if (_itemStorage.UpdateCount(newItem, -model.Count) == null) + { + _logger.LogWarning("Insert operation failed while updating count"); + return false; + } if (_itemForRepairStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); @@ -85,6 +108,14 @@ namespace CarServiceBusinessLogic.BusinessLogics { return; } + //Получаем текущее число статей затрат выбранного вида на складе + int currentItemOnStorage = _itemStorage.GetElement(new() { Id = model.ItemId })?.Count ?? -1; + //Если на складе осталось статей затрат выбранного вида меньше, чем нужно на заявку, отменяем добавление привязки + if (currentItemOnStorage < model.Count) + { + _logger.LogWarning("Not enough items {Id} on storage", model.ItemId); + throw new ArgumentException("На складе не хватает запчастей"); + } _logger.LogInformation("ItemForRepair. Id: {Id}", model.Id); } } diff --git a/CarService/CarServiceContracts/StorageContracts/IItemStorage.cs b/CarService/CarServiceContracts/StorageContracts/IItemStorage.cs index b4d1780..dede255 100644 --- a/CarService/CarServiceContracts/StorageContracts/IItemStorage.cs +++ b/CarService/CarServiceContracts/StorageContracts/IItemStorage.cs @@ -1,4 +1,5 @@ using CarServiceContracts.BindingModels; +using CarServiceContracts.Models; using CarServiceContracts.SearchModels; using CarServiceContracts.ViewModels; @@ -12,5 +13,6 @@ namespace CarServiceContracts.StorageContracts ItemViewModel? Insert(ItemBindingModel model); ItemViewModel? Update(ItemBindingModel model); ItemViewModel? Delete(ItemBindingModel model); + ItemViewModel? UpdateCount(IItemModel? model, int count); } } diff --git a/CarService/CarServiceDatabase/Implements/ItemStorage.cs b/CarService/CarServiceDatabase/Implements/ItemStorage.cs index 8852953..872afcb 100644 --- a/CarService/CarServiceDatabase/Implements/ItemStorage.cs +++ b/CarService/CarServiceDatabase/Implements/ItemStorage.cs @@ -1,4 +1,5 @@ using CarServiceContracts.BindingModels; +using CarServiceContracts.Models; using CarServiceContracts.SearchModels; using CarServiceContracts.StorageContracts; using CarServiceContracts.ViewModels; @@ -83,5 +84,20 @@ namespace CarServiceDatabase.Implements context.SaveChanges(); return item.GetViewModel; } + public ItemViewModel? UpdateCount(IItemModel? model, int count) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + var item = context.Items.FirstOrDefault(x => x.Id == model.Id); + if (item == null) + { + return null; + } + item.UpdateCount(model, count); + return item.GetViewModel; + } } } diff --git a/CarService/CarServiceDatabase/Models/Item.cs b/CarService/CarServiceDatabase/Models/Item.cs index 780cbf7..ff39c44 100644 --- a/CarService/CarServiceDatabase/Models/Item.cs +++ b/CarService/CarServiceDatabase/Models/Item.cs @@ -48,6 +48,14 @@ namespace CarServiceDatabase.Models Count = model.Count; Worker = context.Workers.First(x => x.Id == model.WorkerId); } + public void UpdateCount(IItemModel? model, int count) + { + if (model == null) + { + return; + } + Count -= model.Count; + } public ItemViewModel GetViewModel => new() { Id = Id, diff --git a/CarService/CarServiceView/Program.cs b/CarService/CarServiceView/Program.cs index 4fd414b..d56fe77 100644 --- a/CarService/CarServiceView/Program.cs +++ b/CarService/CarServiceView/Program.cs @@ -19,7 +19,7 @@ namespace CarServiceView var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); + Application.Run(_serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) {