PIbd-21_Balberova_D.N._Sush.../SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs

218 lines
8.1 KiB
C#
Raw Normal View History

2023-02-13 20:45:17 +04:00
using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
namespace SushiBarBusinessLogic.BusinessLogics
2023-02-13 20:45:17 +04:00
{
public class ShopLogic : IShopLogic
{
private readonly ILogger _logger;
private readonly IShopStorage _shopStorage;
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage shopStorage)
{
_logger = logger;
_shopStorage = shopStorage;
}
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
2023-02-13 23:13:13 +04:00
_logger.LogInformation("ReadList. ShopName: {ShopName}. Id: {Id}",
model?.ShopName, model?.Id);
var list = model == null ? _shopStorage.GetFullList() :
_shopStorage.GetFilteredList(model);
2023-02-13 20:45:17 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
2023-02-13 23:13:13 +04:00
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
2023-02-13 20:45:17 +04:00
return list;
}
public ShopViewModel? ReadElement(ShopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-02-13 23:13:13 +04:00
_logger.LogInformation("ReadElement. ShopName: {ShopName}. Id: {Id}",
model.ShopName, model.Id);
2023-02-13 20:45:17 +04:00
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
2023-02-13 23:13:13 +04:00
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
2023-02-13 20:45:17 +04:00
return element;
}
public bool Create(ShopBindingModel model)
{
CheckModel(model);
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ShopBindingModel model)
{
2023-02-13 23:13:13 +04:00
CheckModel(model);
2023-02-13 20:45:17 +04:00
if (_shopStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ShopBindingModel model)
{
CheckModel(model, false);
2023-02-13 23:13:13 +04:00
_logger.LogInformation("Delete. Id: {Id}", model.Id);
2023-02-13 20:45:17 +04:00
if (_shopStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2023-02-13 23:13:13 +04:00
if (string.IsNullOrEmpty(model.ShopName))
2023-02-13 20:45:17 +04:00
{
2023-02-13 23:13:13 +04:00
throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName));
2023-02-13 20:45:17 +04:00
}
if (model.MaxCountSushi < 0)
{
throw new ArgumentException(
"Максимальное количество суши в магазине не может быть меньше нуля",
nameof(model.MaxCountSushi));
}
2023-02-13 20:45:17 +04:00
_logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}",
2023-02-13 23:13:13 +04:00
model.ShopName, model.Address, model.Id);
2023-02-13 20:45:17 +04:00
var element = _shopStorage.GetElement(new ShopSearchModel
{
2023-02-13 23:13:13 +04:00
ShopName = model.ShopName
2023-02-13 20:45:17 +04:00
});
2023-02-13 23:13:13 +04:00
if (element != null && element.Id != model.Id && element.ShopName == model.ShopName)
2023-02-13 20:45:17 +04:00
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
public bool AddSushiInShop(ShopSearchModel model, ISushiModel sushi, int count)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (count <= 0)
{
2023-02-13 23:13:13 +04:00
throw new ArgumentException("Количество суши должно быть больше 0", nameof(count));
2023-02-13 20:45:17 +04:00
}
_logger.LogInformation("AddSushiInShop. ShopName:{ShopName}.Id:{ Id}",
2023-02-13 23:13:13 +04:00
model.ShopName, model.Id);
2023-02-13 20:45:17 +04:00
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddSushiInShop element not found");
return false;
}
if (element.MaxCountSushi -
element.ListSushi.Select(x => x.Value.Item2).Sum() < count)
{
throw new ArgumentNullException("Магазин переполнен", nameof(count));
}
2023-02-13 20:45:17 +04:00
_logger.LogInformation("AddSushiInShop find. Id:{Id}", element.Id);
if (element.ListSushi.TryGetValue(sushi.Id, out var pair))
{
2023-02-13 23:13:13 +04:00
element.ListSushi[sushi.Id] = (sushi, count + pair.Item2);
_logger.LogInformation(
"AddSushiInShop. Added {count} {sushi} to '{ShopName}' shop",
count, sushi.SushiName, element.ShopName);
2023-02-13 20:45:17 +04:00
}
else
{
element.ListSushi[sushi.Id] = (sushi, count);
_logger.LogInformation(
2023-02-13 23:13:13 +04:00
"AddSushiInShop. Added {count} new sushi {sushi} to '{ShopName}' shop",
count, sushi.SushiName, element.ShopName);
}
_shopStorage.Update(new() {
Id = element.Id,
Address = element.Address,
ShopName = element.ShopName,
DateOpening = element.DateOpening,
MaxCountSushi = element.MaxCountSushi,
ListSushi = element.ListSushi,
2023-02-13 23:13:13 +04:00
});
2023-02-13 20:45:17 +04:00
return true;
}
public bool AddSushi(ISushiModel 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.MaxCountSushi - x.ListSushi
.Select(p => p.Value.Item2).Sum()).Sum() - count;
if (freeCount < 0)
{
_logger.LogInformation("AddSushi. Не удалось добавить изделия в магазины, они переполнены.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
int countFree = shop.MaxCountSushi - shop.ListSushi.Select(x => x.Value.Item2).Sum();
if (countFree < count)
{
if (!AddSushiInShop(new() { Id = shop.Id }, model, countFree))
{
_logger.LogWarning("AddSushiInShop operation failed.");
return false;
}
count -= countFree;
}
else
{
if (!AddSushiInShop(new() { Id = shop.Id }, model, count))
{
_logger.LogWarning("AddSushiInShop operation failed.");
return false;
}
count = 0;
}
if (count == 0)
{
return true;
}
}
_logger.LogWarning("AddSushi operation failed.");
return false;
}
public bool SellSushi(ISushiModel model, int count)
{
return _shopStorage.SellSushi(model, count);
}
2023-02-13 20:45:17 +04:00
}
}