Доработка логики привязки статьи затрат

This commit is contained in:
Мк Игорь 2023-04-07 08:22:21 +04:00
parent 6c2a55ae40
commit 65a22bb3f6
5 changed files with 59 additions and 2 deletions

View File

@ -11,10 +11,12 @@ namespace CarServiceBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IItemForRepairStorage _itemForRepairStorage;
public ItemForRepairLogic(ILogger<ItemForRepairLogic> logger, IItemForRepairStorage itemForRepairStorage)
private readonly IItemStorage _itemStorage;
public ItemForRepairLogic(ILogger<ItemForRepairLogic> logger, IItemForRepairStorage itemForRepairStorage, IItemStorage itemStorage)
{
_logger = logger;
_itemForRepairStorage = itemForRepairStorage;
_itemStorage = itemStorage;
}
public List<ItemForRepairViewModel>? 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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -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,

View File

@ -19,7 +19,7 @@ namespace CarServiceView
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
Application.Run(_serviceProvider.GetRequiredService<FormRegistrationWorker>());
Application.Run(_serviceProvider.GetRequiredService<FormItemForRepairTest>());
}
private static void ConfigureServices(ServiceCollection services)
{