2023-03-12 01:50:01 +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 ShopViewModel? ReadElement(ShopSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. Shop Name:{0}, ID:{1}", model.Name, 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 List<ShopViewModel>? ReadList(ShopSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Shop Name:{0}, ID:{1} ", model?.Name, 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 bool SupplyBouquets(ShopSearchModel model, IBouquetModel bouquet, int count)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bouquet == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(bouquet));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Count of bouquets in supply must be more than 0", nameof(count));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var shopElement = _shopStorage.GetElement(model);
|
|
|
|
|
if (shopElement == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Required shop element not found in storage");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
|
|
|
|
|
|
2023-03-26 01:51:51 +04:00
|
|
|
|
var countBouquets = 0;
|
|
|
|
|
foreach (var bouq in shopElement.ShopBouquets)
|
|
|
|
|
{
|
|
|
|
|
countBouquets += bouq.Value.Item2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shopElement.MaxCountBouquets - countBouquets > count)
|
|
|
|
|
{
|
|
|
|
|
if (shopElement.ShopBouquets.TryGetValue(bouquet.Id, out var sameBouquet))
|
|
|
|
|
{
|
|
|
|
|
shopElement.ShopBouquets[bouquet.Id] = (bouquet, sameBouquet.Item2 + count);
|
|
|
|
|
_logger.LogInformation("Same bouquet found by supply. Added {0} of {1} in {2} shop", count, bouquet.BouquetName, shopElement.Name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
shopElement.ShopBouquets[bouquet.Id] = (bouquet, count);
|
|
|
|
|
_logger.LogInformation("New bouquet added by supply. Added {0} of {1} in {2} shop", count, bouquet.BouquetName, shopElement.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_shopStorage.Update(new()
|
|
|
|
|
{
|
|
|
|
|
Id = shopElement.Id,
|
|
|
|
|
Name = shopElement.Name,
|
|
|
|
|
Address = shopElement.Address,
|
|
|
|
|
OpeningDate = shopElement.OpeningDate,
|
|
|
|
|
ShopBouquets = shopElement.ShopBouquets
|
|
|
|
|
});
|
2023-03-12 01:50:01 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-03-26 01:51:51 +04:00
|
|
|
|
_logger.LogWarning("Required shop is overflowed");
|
|
|
|
|
return false;
|
2023-03-12 01:50:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (_shopStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 02:14:04 +04:00
|
|
|
|
public bool SellBouquet(IBouquetModel bouquet, int count)
|
2023-03-26 01:51:51 +04:00
|
|
|
|
{
|
|
|
|
|
return _shopStorage.SellBouquet(bouquet, count);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 01:50:01 +04:00
|
|
|
|
private void CheckModel(ShopBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия магазина!", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 01:51:51 +04:00
|
|
|
|
if (model.MaxCountBouquets < 0)
|
|
|
|
|
{
|
2023-03-26 02:14:04 +04:00
|
|
|
|
throw new InvalidOperationException("Магазин с отрицательным числом максимального количества букетов!");
|
2023-03-26 01:51:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 01:50:01 +04:00
|
|
|
|
_logger.LogInformation("Shop. Name: {0}, Address: {1}, ID: {2}", model.Name, model.Address, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _shopStorage.GetElement(new ShopSearchModel
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Магазин с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|