Доработка логики привязки статьи затрат
This commit is contained in:
parent
6c2a55ae40
commit
65a22bb3f6
@ -11,10 +11,12 @@ namespace CarServiceBusinessLogic.BusinessLogics
|
|||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IItemForRepairStorage _itemForRepairStorage;
|
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;
|
_logger = logger;
|
||||||
_itemForRepairStorage = itemForRepairStorage;
|
_itemForRepairStorage = itemForRepairStorage;
|
||||||
|
_itemStorage = itemStorage;
|
||||||
}
|
}
|
||||||
public List<ItemForRepairViewModel>? ReadList(ItemForRepairSearchModel? model)
|
public List<ItemForRepairViewModel>? ReadList(ItemForRepairSearchModel? model)
|
||||||
{
|
{
|
||||||
@ -47,6 +49,13 @@ namespace CarServiceBusinessLogic.BusinessLogics
|
|||||||
public bool Create(ItemForRepairBindingModel model)
|
public bool Create(ItemForRepairBindingModel model)
|
||||||
{
|
{
|
||||||
CheckModel(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)
|
if (_itemForRepairStorage.Insert(model) == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Insert operation failed");
|
_logger.LogWarning("Insert operation failed");
|
||||||
@ -57,6 +66,20 @@ namespace CarServiceBusinessLogic.BusinessLogics
|
|||||||
public bool Update(ItemForRepairBindingModel model)
|
public bool Update(ItemForRepairBindingModel model)
|
||||||
{
|
{
|
||||||
CheckModel(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)
|
if (_itemForRepairStorage.Update(model) == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Update operation failed");
|
_logger.LogWarning("Update operation failed");
|
||||||
@ -85,6 +108,14 @@ namespace CarServiceBusinessLogic.BusinessLogics
|
|||||||
{
|
{
|
||||||
return;
|
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);
|
_logger.LogInformation("ItemForRepair. Id: {Id}", model.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using CarServiceContracts.BindingModels;
|
using CarServiceContracts.BindingModels;
|
||||||
|
using CarServiceContracts.Models;
|
||||||
using CarServiceContracts.SearchModels;
|
using CarServiceContracts.SearchModels;
|
||||||
using CarServiceContracts.ViewModels;
|
using CarServiceContracts.ViewModels;
|
||||||
|
|
||||||
@ -12,5 +13,6 @@ namespace CarServiceContracts.StorageContracts
|
|||||||
ItemViewModel? Insert(ItemBindingModel model);
|
ItemViewModel? Insert(ItemBindingModel model);
|
||||||
ItemViewModel? Update(ItemBindingModel model);
|
ItemViewModel? Update(ItemBindingModel model);
|
||||||
ItemViewModel? Delete(ItemBindingModel model);
|
ItemViewModel? Delete(ItemBindingModel model);
|
||||||
|
ItemViewModel? UpdateCount(IItemModel? model, int count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using CarServiceContracts.BindingModels;
|
using CarServiceContracts.BindingModels;
|
||||||
|
using CarServiceContracts.Models;
|
||||||
using CarServiceContracts.SearchModels;
|
using CarServiceContracts.SearchModels;
|
||||||
using CarServiceContracts.StorageContracts;
|
using CarServiceContracts.StorageContracts;
|
||||||
using CarServiceContracts.ViewModels;
|
using CarServiceContracts.ViewModels;
|
||||||
@ -83,5 +84,20 @@ namespace CarServiceDatabase.Implements
|
|||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
return item.GetViewModel;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,14 @@ namespace CarServiceDatabase.Models
|
|||||||
Count = model.Count;
|
Count = model.Count;
|
||||||
Worker = context.Workers.First(x => x.Id == model.WorkerId);
|
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()
|
public ItemViewModel GetViewModel => new()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
|
@ -19,7 +19,7 @@ namespace CarServiceView
|
|||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
ConfigureServices(services);
|
ConfigureServices(services);
|
||||||
_serviceProvider = services.BuildServiceProvider();
|
_serviceProvider = services.BuildServiceProvider();
|
||||||
Application.Run(_serviceProvider.GetRequiredService<FormRegistrationWorker>());
|
Application.Run(_serviceProvider.GetRequiredService<FormItemForRepairTest>());
|
||||||
}
|
}
|
||||||
private static void ConfigureServices(ServiceCollection services)
|
private static void ConfigureServices(ServiceCollection services)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user