Бизнес логика прописана

This commit is contained in:
ArtemEmelyanov 2023-05-03 19:58:59 +04:00
parent 6396dcaf90
commit cb55b7703c
2 changed files with 99 additions and 23 deletions

View File

@ -17,10 +17,14 @@ namespace FishFactoryBusinessLogic.BusinessLogics
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) private readonly IShopLogic _shopLogic;
private readonly ICannedStorage _cannedStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, ICannedStorage cannedStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
_shopLogic = shopLogic;
_cannedStorage = cannedStorage;
} }
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
{ {
@ -41,31 +45,42 @@ namespace FishFactoryBusinessLogic.BusinessLogics
} }
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
{ {
CheckModel(model, false); var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
var element = _orderStorage.GetElement(new OrderSearchModel() if (viewModel == null)
{ {
Id = model.Id throw new ArgumentNullException(nameof(model));
});
if (element == null)
{
throw new ArgumentNullException(nameof(element));
} }
model.DateImplement = element.DateImplement; if (viewModel.Status + 1 != newStatus)
model.Status = element.Status;
if (newStatus - model.Status == 1)
{ {
model.Status = newStatus; _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
if (model.Status == OrderStatus.Выдан) return false;
model.DateImplement = DateTime.Now; }
if (_orderStorage.Update(model) == null) model.Status = newStatus;
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
var canned = _cannedStorage.GetElement(new() { Id = viewModel.CannedId });
if (canned == null)
{ {
_logger.LogWarning("Update operation failed"); throw new ArgumentNullException(nameof(canned));
return false; }
if (!_shopLogic.AddCanneds(canned, viewModel.Count))
{
throw new Exception($"AddCanneds operation failed. Shop is full.");
} }
return true;
} }
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, newStatus); else
throw new ArgumentException($"Невозможно приствоить статус {newStatus} заказу с текущим статусом {model.Status}"); {
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
} }
public bool DeliveryOrder(OrderBindingModel model) public bool DeliveryOrder(OrderBindingModel model)
{ {
@ -83,8 +98,7 @@ namespace FishFactoryBusinessLogic.BusinessLogics
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
_orderStorage.GetFilteredList(model);
if (list == null) if (list == null)
{ {
_logger.LogWarning("ReadList return null list"); _logger.LogWarning("ReadList return null list");

View File

@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -39,6 +40,10 @@ namespace FishFactoryBusinessLogic
_logger.LogWarning("AddCannedInShop element not found"); _logger.LogWarning("AddCannedInShop element not found");
return false; return false;
} }
if (element.MaxCountCanneds - element.ListCanneds.Select(x => x.Value.Item2).Sum() < count)
{
throw new ArgumentNullException("Магазин переполнен", nameof(count));
}
_logger.LogInformation("AddCannedInShop find. Id:{Id}", element.Id); _logger.LogInformation("AddCannedInShop find. Id:{Id}", element.Id);
if (element.ListCanneds.TryGetValue(canned.Id, out var pair)) if (element.ListCanneds.TryGetValue(canned.Id, out var pair))
@ -57,7 +62,8 @@ namespace FishFactoryBusinessLogic
Address = element.Address, Address = element.Address,
ShopName = element.ShopName, ShopName = element.ShopName,
DateOpening = element.DateOpening, DateOpening = element.DateOpening,
ListCanneds = element.ListCanneds ListCanneds = element.ListCanneds,
MaxCountCanneds = element.MaxCountCanneds,
}); });
return true; return true;
} }
@ -140,6 +146,10 @@ namespace FishFactoryBusinessLogic
{ {
throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName));
} }
if (model.MaxCountCanneds < 0)
{
throw new ArgumentException("Максимальное количество консерв в магазине не может быть меньше нуля", nameof(model.MaxCountCanneds));
}
_logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.ShopName, model.Address, model.Id); _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.ShopName, model.Address, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel var element = _shopStorage.GetElement(new ShopSearchModel
{ {
@ -150,5 +160,57 @@ namespace FishFactoryBusinessLogic
throw new InvalidOperationException("Магазин с таким названием уже есть"); throw new InvalidOperationException("Магазин с таким названием уже есть");
} }
} }
public bool SellCanneds(ICannedModel model, int count)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (count <= 0)
{
throw new ArgumentException("Количество консерв должно быть больше 0", nameof(count));
}
var freeCount = _shopStorage.GetFullList()
.Select(x => x.MaxCountCanneds - x.ListCanneds
.Select(p => p.Value.Item2).Sum()).Sum() - count;
if (freeCount < 0)
{
_logger.LogInformation("AddCanned. Не удалось добавить изделия в магазины, они переполнены.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
int countFree = shop.MaxCountCanneds - shop.ListCanneds.Select(x => x.Value.Item2).Sum();
if (countFree < count)
{
if (!AddCannedInShop(new() { Id = shop.Id }, model, countFree))
{
_logger.LogWarning("AddCannedInShop operation failed.");
return false;
}
count -= countFree;
}
else
{
if (!AddCannedInShop(new() { Id = shop.Id }, model, count))
{
_logger.LogWarning("AddCannedInShop operation failed.");
return false;
}
count = 0;
}
if (count == 0)
{
return true;
}
}
return false;
}
public bool AddCanneds(ICannedModel model, int count)
{
return _shopStorage.SellCanneds(model, count);
}
} }
} }