284 lines
9.2 KiB
C#
284 lines
9.2 KiB
C#
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
|
|
{
|
|
public class StoreLogic : IStoreLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IStoreStorage _storeStorage;
|
|
|
|
public StoreLogic(ILogger<StoreLogic> logger, IStoreStorage storeStorage)
|
|
{
|
|
_logger = logger;
|
|
_storeStorage = storeStorage;
|
|
}
|
|
|
|
public bool AddPackage(StoreSearchModel model, ISushiModel sushi, int quantity)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
|
|
if (quantity <= 0)
|
|
{
|
|
throw new ArgumentException("Quantity must be more then zero", nameof(quantity));
|
|
}
|
|
|
|
_logger.LogInformation("AddPackageInStore. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
|
|
|
|
var element = _storeStorage.GetElement(model);
|
|
|
|
if (element == null)
|
|
{
|
|
_logger.LogWarning("AddPackageInStore element not found");
|
|
return false;
|
|
}
|
|
|
|
_logger.LogInformation("AddPackageInStore find. Id:{Id}", element.Id);
|
|
|
|
if (element.Sushis.TryGetValue(sushi.Id, out var pair))
|
|
{
|
|
element.Sushis[sushi.Id] = (sushi, quantity + pair.Item2);
|
|
_logger.LogInformation("AddPackageInStore. Has been added {quantity} {package} in {StoreName}", quantity, sushi.SushiName, element.StoreName);
|
|
}
|
|
else
|
|
{
|
|
element.Sushis[sushi.Id] = (sushi, quantity);
|
|
_logger.LogInformation("AddPastryInShop. Has been added {quantity} new Package {package} in {StoreName}", quantity, sushi.SushiName, element.StoreName);
|
|
}
|
|
|
|
_storeStorage.Update(new()
|
|
{
|
|
Id = element.Id,
|
|
StoreAddress = element.StoreAddress,
|
|
StoreName = element.StoreName,
|
|
OpeningDate = element.OpeningDate,
|
|
Sushis = element.Sushis,
|
|
maxSushi = element.maxSushi
|
|
});
|
|
return true;
|
|
}
|
|
|
|
public bool Create(StoreBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
model.Sushis = new();
|
|
|
|
if (_storeStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool Delete(StoreBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
if (_storeStorage.Delete(model) == null)
|
|
{
|
|
_logger.LogWarning("Delete operation failed");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool SupplySushi(StoreSearchModel model, ISushiModel sushi, int quantity)
|
|
{
|
|
if (model == null || sushi == null || quantity <= 0)
|
|
{
|
|
throw new ArgumentNullException();
|
|
}
|
|
|
|
var store = _storeStorage.GetElement(model);
|
|
if (store == null)
|
|
{
|
|
_logger.LogWarning("Required store element not found in storage");
|
|
return false;
|
|
}
|
|
|
|
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}",
|
|
store.Id, store.StoreName);
|
|
var countSushi = store.Sushis.Sum(s => s.Value.Item2);
|
|
if (store.maxSushi - countSushi < quantity)
|
|
{
|
|
_logger.LogWarning("Required shop is overflowed");
|
|
return false;
|
|
}
|
|
|
|
if (store.Sushis.TryGetValue(sushi.Id, out var pair))
|
|
{
|
|
store.Sushis[sushi.Id] = (sushi, quantity + pair.Item2);
|
|
_logger.LogInformation("AddPackageInStore. Has been added {quantity} {package} in {StoreName}",
|
|
quantity, sushi.SushiName, store.StoreName);
|
|
}
|
|
else
|
|
{
|
|
store.Sushis[sushi.Id] = (sushi, quantity);
|
|
_logger.LogInformation(
|
|
"AddPastryInShop. Has been added {quantity} new Package {package} in {StoreName}",
|
|
quantity, sushi.SushiName, store.StoreName);
|
|
}
|
|
|
|
_storeStorage.Update(new()
|
|
{
|
|
Id = store.Id,
|
|
StoreAddress = store.StoreAddress,
|
|
StoreName = store.StoreName,
|
|
OpeningDate = store.OpeningDate,
|
|
Sushis = store.Sushis,
|
|
maxSushi = store.maxSushi
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool CheckToSupply(ISushiModel sushi, int quantity)
|
|
{
|
|
if (sushi == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(sushi));
|
|
}
|
|
|
|
if (quantity <= 0)
|
|
{
|
|
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
|
|
}
|
|
|
|
var freePlaces = _storeStorage.GetFullList()
|
|
.Select(x => x.maxSushi - x.Sushis
|
|
.Select(p => p.Value.Item2).Sum()).Sum() - quantity;
|
|
|
|
if (freePlaces < 0)
|
|
{
|
|
_logger.LogInformation("AddReinforced. Failed to add reinforced to shop. It's full.");
|
|
return false;
|
|
}
|
|
|
|
foreach (var store in _storeStorage.GetFullList())
|
|
{
|
|
var temp = Math
|
|
.Min(quantity, store.maxSushi - store.Sushis.Select(x => x.Value.Item2)
|
|
.Sum());
|
|
|
|
if (temp <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!SupplySushi(new StoreSearchModel { Id = store.Id }, sushi, temp))
|
|
{
|
|
_logger.LogWarning("An error occurred while adding reinforced to shops");
|
|
return false;
|
|
}
|
|
|
|
quantity -= temp;
|
|
|
|
if (quantity == 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool SellSushi(ISushiModel model, int quantity)
|
|
{
|
|
return _storeStorage.SellSushi(model, quantity);
|
|
}
|
|
|
|
public StoreViewModel? ReadElement(StoreSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
|
|
_logger.LogInformation("ReadElement. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
|
|
var element = _storeStorage.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<StoreViewModel>? ReadList(StoreSearchModel? model)
|
|
{
|
|
_logger.LogInformation("ReadList. StoreName:{StoreName}.Id:{ Id} ", model?.StoreName, model?.Id);
|
|
|
|
var list = model == null ? _storeStorage.GetFullList() : _storeStorage.GetFilteredList(model);
|
|
|
|
if (list == null)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return null;
|
|
}
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
return list;
|
|
}
|
|
|
|
public bool Update(StoreBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
|
|
if (string.IsNullOrEmpty(model.StoreName))
|
|
{
|
|
throw new ArgumentNullException("Store name is empty!", nameof(model.StoreName));
|
|
}
|
|
|
|
if (_storeStorage.Update(model) == null)
|
|
{
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void CheckModel(StoreBindingModel model, bool withParams=true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(model.StoreName))
|
|
{
|
|
throw new ArgumentNullException("Store name is empty!", nameof(model.StoreName));
|
|
}
|
|
|
|
_logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAddress, model.Id);
|
|
var element = _storeStorage.GetElement(new StoreSearchModel
|
|
{
|
|
StoreName = model.StoreName
|
|
});
|
|
|
|
if (element != null && element.Id != model.Id && element.StoreName == model.StoreName)
|
|
{
|
|
throw new InvalidOperationException("This name of store is already exists!");
|
|
}
|
|
}
|
|
}
|
|
}
|