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) {