PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs
2024-04-19 02:23:50 +04:00

165 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
if (shop.ShopFlowers.ContainsKey(flower.Id))
{
var shopFlower = shop.ShopFlowers[flower.Id];
shopFlower.Item2 += count;
shop.ShopFlowers[flower.Id] = shopFlower;
_logger.LogInformation("MakeSupply. Added {count} '{flower}' to '{ShopName}' shop", count, flower.FlowerName, shop.ShopName);
}
else
{
shop.ShopFlowers.Add(flower.Id, (flower, count));
_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,
ShopFlowers = shop.ShopFlowers,
MaximumFlowers = shop.MaximumFlowers,
}) == 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("Магазин с таким названием уже есть");
}
}
public bool MakeSale(IFlowerModel model, int count)
{
return _shopStorage.MakeSale(model, count);
}
}
}