PIbd-22_Katysheva_N.E._Pizz.../Pizzeria/PizzeriaBusinessLogic/BusinessLogic/PizzaLogic.cs

106 lines
3.9 KiB
C#
Raw Normal View History

2024-03-11 00:29:35 +04:00
using Microsoft.Extensions.Logging;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
namespace PizzeriaBusinessLogic.BusinessLogic
{
public class PizzaLogic : IPizzaLogic
{
private readonly ILogger _logger;
private readonly IPizzaStorage _PizzaStorage;
public PizzaLogic(ILogger<PizzaLogic> logger, IPizzaStorage PizzaStorage)
{
_logger = logger;
_PizzaStorage = PizzaStorage;
}
public List<PizzaViewModel>? ReadList(PizzaSearchModel? model)
{
_logger.LogInformation("ReadList. PizzaName:{PizzaName}.Id:{ Id}", model?.PizzaName, model?.Id);
2024-03-12 19:51:37 +04:00
var list = model == null ? _PizzaStorage.GetFullList() : _PizzaStorage.GetFilteredList(model);
2024-03-11 00:29:35 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public PizzaViewModel? ReadElement(PizzaSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PizzaName:{PizzaName}.Id:{ Id} ", model.PizzaName, model.Id);
var element = _PizzaStorage.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(PizzaBindingModel model)
{
CheckModel(model);
if (_PizzaStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PizzaBindingModel model)
{
CheckModel(model);
if (_PizzaStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PizzaBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_PizzaStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PizzaBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PizzaName))
{
throw new ArgumentNullException("Нет названия продукта", nameof(model.PizzaName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Pizza. PizzaName:{PizzaName}.Price:{ Price}. Id: { Id}", model.PizzaName, model.Price, model.Id);
var element = _PizzaStorage.GetElement(new PizzaSearchModel { PizzaName = model.PizzaName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}
}