PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopBusinessLogic/BusinessLogics/ShopLogic.cs

233 lines
8.0 KiB
C#
Raw Normal View History

2024-05-18 17:53:11 +04:00
using Microsoft.Extensions.Logging;
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopBusinessLogic.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 bool AddGift(ShopSearchModel model, IGiftModel gift, int quantity)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (quantity <= 0)
{
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
}
_logger.LogInformation("AddGiftInShop. ShopName:{ShopName}.Id:{Id}", model.ShopName, model.Id);
var element = _ShopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddGiftInShop element not found");
return false;
}
if (element.MaxCapacity - element.ShopGifts.Select(x => x.Value.Item2).Sum() < quantity)
{
throw new ArgumentNullException("Магазин переполнен", nameof(quantity));
}
_logger.LogInformation("AddGiftInShop find. Id:{Id}", element.Id);
if (element.ShopGifts.TryGetValue(gift.Id, out var pair))
{
element.ShopGifts[gift.Id] = (gift, quantity + pair.Item2);
_logger.LogInformation("AddGiftInShop. Has been added {quantity} {gift} in {ShopName}", quantity, gift.GiftName, element.ShopName);
}
else
{
element.ShopGifts[gift.Id] = (gift, quantity);
_logger.LogInformation("AddGiftInShop. Has been added {quantity} new gift {gift} in {ShopName}", quantity, gift.GiftName, element.ShopName);
}
_ShopStorage.Update(new()
{
Id = element.Id,
ShopAdress = element.ShopAdress,
ShopName = element.ShopName,
OpeningDate = element.OpeningDate,
ShopGifts = element.ShopGifts
});
return true;
}
public bool Create(ShopBindingModel model)
{
CheckModel(model);
model.ShopGifts = new();
if (_ShopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert 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;
}
//2
public bool MakeSell(IGiftModel gift, int count)
{
return _ShopStorage.SellGifts(gift, count);
}
public ShopViewModel? ReadElement(ShopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ShopName:{ShopName}.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 List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
_logger.LogInformation("ReadList. ShopName:{ShopName}.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 bool MakeSupply(ShopSearchModel model, IGiftModel gift, int count)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (gift == null)
throw new ArgumentNullException(nameof(gift));
if (count <= 0)
throw new ArgumentNullException("Количество должно быть положительным числом");
ShopViewModel curModel = _ShopStorage.GetElement(model);
if (curModel == null)
throw new ArgumentNullException(nameof(curModel));
var countItems = curModel.ShopGifts.Select(x => x.Value.Item2).Sum();
if (curModel.MaxCapacity - countItems >= count)
{
if (curModel.ShopGifts.TryGetValue(gift.Id, out var sameDocument))
{
curModel.ShopGifts[gift.Id] = (gift, sameDocument.Item2 + count);
_logger.LogInformation("Same gift found by supply. Added {0} of {1} in {2} shop", count, gift.GiftName, curModel.ShopName);
}
else
{
curModel.ShopGifts[gift.Id] = (gift, count);
_logger.LogInformation("New gift added by supply. Added {0} of {1} in {2} shop", count, gift.GiftName, curModel.ShopName);
}
_ShopStorage.Update(new()
{
Id = curModel.Id,
ShopName = curModel.ShopName,
ShopAdress = curModel.ShopAdress,
OpeningDate = curModel.OpeningDate,
ShopGifts = curModel.ShopGifts,
MaxCapacity = curModel.MaxCapacity
});
}
else
{
_logger.LogWarning("Required shop is overflowed");
return false;
}
return true;
}
public bool Update(ShopBindingModel model)
{
CheckModel(model, false);
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName));
}
if (_ShopStorage.Update(model) == null)
{
_logger.LogWarning("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));
}
_logger.LogInformation("Shop. ShopName:{0}.ShopAddress:{1}. Id: {2}", model.ShopName, model.ShopAdress, model.Id);
var element = _ShopStorage.GetElement(new ShopSearchModel
{
ShopName = model.ShopName
});
if (element != null && element.Id != model.Id && element.ShopName == model.ShopName)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
}
}