2024-02-05 18:10:41 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
2024-02-09 17:38:25 +04:00
|
|
|
|
public class SushiLogic : ISushiLogic
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-02-09 17:38:25 +04:00
|
|
|
|
private readonly ISushiStorage _sushiStorage;
|
2024-02-22 10:56:17 +04:00
|
|
|
|
public SushiLogic(ILogger<SushiLogic> logger, ISushiStorage sushiStorage)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2024-02-09 17:38:25 +04:00
|
|
|
|
_sushiStorage = sushiStorage;
|
2024-02-05 18:10:41 +04:00
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
public List<SushiViewModel>? ReadList(SushiSearchModel? model)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
2024-02-09 17:38:25 +04:00
|
|
|
|
_logger.LogInformation("ReadList. SushiName:{SushiName}. Id:{ Id}", model?.SushiName, model?.Id);
|
|
|
|
|
var list = model == null ? _sushiStorage.GetFullList() :
|
|
|
|
|
_sushiStorage.GetFilteredList(model);
|
2024-02-05 18:10:41 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
public SushiViewModel? ReadElement(SushiSearchModel model)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
if(model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
_logger.LogInformation("ReadElement. SushiName:{SushiName}. Id:{ Id}", model.SushiName, model.Id);
|
|
|
|
|
var element = _sushiStorage.GetElement(model);
|
2024-02-05 18:10:41 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return element;
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
public bool Create(SushiBindingModel model)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-02-09 17:38:25 +04:00
|
|
|
|
if (_sushiStorage.Insert(model) == null)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
public bool Update(SushiBindingModel model)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-02-09 17:38:25 +04:00
|
|
|
|
if (_sushiStorage.Update(model) == null)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
public bool Delete(SushiBindingModel model)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
2024-02-09 17:38:25 +04:00
|
|
|
|
if (_sushiStorage.Delete(model) == null)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
private void CheckModel(SushiBindingModel model, bool withParams = true)
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.SushiName))
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
2024-02-09 17:38:25 +04:00
|
|
|
|
throw new ArgumentException("Нет названия продукта", nameof(model.SushiName));
|
2024-02-05 18:10:41 +04:00
|
|
|
|
}
|
|
|
|
|
if (model.Price <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
|
|
|
|
|
}
|
2024-02-09 17:38:25 +04:00
|
|
|
|
var element = _sushiStorage.GetElement(new SushiSearchModel
|
2024-02-05 18:10:41 +04:00
|
|
|
|
{
|
2024-02-09 17:38:25 +04:00
|
|
|
|
SushiName = model.SushiName
|
2024-02-05 18:10:41 +04:00
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Продукт с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|