PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs

171 lines
6.4 KiB
C#
Raw Normal View History

2024-04-04 00:29:16 +04:00
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using Microsoft.Extensions.Logging;
namespace FlowerShopBusinessLogic.BusinessLogics
{
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)
{
_logger.LogInformation("ReadList. Name:{Name}.Id:{Id}", model?.ShopName, model?.Id);
var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ShopViewModel? ReadElement(ShopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
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)
{
CheckModel(model);
if (_shopStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ShopBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_shopStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool MakeSupply(ShopSearchModel shopModel, IFlowerModel flower, int count)
{
if (shopModel == null)
{
throw new ArgumentNullException(nameof(shopModel));
}
if (flower == null)
{
throw new ArgumentNullException(nameof(flower));
}
if (count <= 0)
{
throw new ArgumentException("Количество цветов в поставке должно быть больше нуля", nameof(count));
}
_logger.LogInformation("MakeSupply(GetElement). ShopName: {ShopName}. Id: {Id}", shopModel.ShopName, shopModel.Id);
var shop = _shopStorage.GetElement(shopModel);
if (shop == null)
{
_logger.LogWarning("MakeSupply(GetElement). Element not found");
return false;
}
2024-05-17 01:43:54 +04:00
int CurrentRepairsNum = shop.ShopFlowers.Select(x => x.Value.Item2).Sum();
if (count > (shop.MaximumFlowers - CurrentRepairsNum))
{
_logger.LogWarning("Попытка добавить в магазин число элементов, большее RepairsMaxCount");
return false;
}
2024-04-19 02:23:50 +04:00
if (shop.ShopFlowers.ContainsKey(flower.Id))
2024-04-04 00:29:16 +04:00
{
2024-04-19 02:23:50 +04:00
var shopFlower = shop.ShopFlowers[flower.Id];
2024-04-04 00:29:16 +04:00
shopFlower.Item2 += count;
2024-04-19 02:23:50 +04:00
shop.ShopFlowers[flower.Id] = shopFlower;
2024-04-04 00:29:16 +04:00
_logger.LogInformation("MakeSupply. Added {count} '{flower}' to '{ShopName}' shop", count, flower.FlowerName, shop.ShopName);
}
else
{
2024-04-19 02:23:50 +04:00
shop.ShopFlowers.Add(flower.Id, (flower, count));
2024-04-04 00:29:16 +04:00
_logger.LogInformation("MakeSupply. Added {count} new '{flower}' to '{ShopName}' shop", count, flower.FlowerName, shop.ShopName);
}
if (_shopStorage.Update(new ShopBindingModel()
{
Id = shop.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
2024-04-19 02:23:50 +04:00
ShopFlowers = shop.ShopFlowers,
MaximumFlowers = shop.MaximumFlowers,
2024-04-04 00:29:16 +04:00
}) == null)
{
_logger.LogWarning("MakeSupply. Update 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;
}
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет названия магазина ", nameof(model.ShopName));
}
if (string.IsNullOrEmpty(model.Address))
{
throw new ArgumentNullException("Нет адреса ", nameof(model.Address));
}
_logger.LogInformation("Shop. ShopName:{ShopName}. Address:{Address}. DateOpening:{DateOpening} Id: {Id}", model.ShopName, model.Address, model.DateOpening, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
{
ShopName = model.ShopName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
2024-04-19 02:23:50 +04:00
public bool MakeSale(IFlowerModel model, int count)
{
return _shopStorage.MakeSale(model, count);
}
}
2024-04-04 00:29:16 +04:00
}