110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
using PizzeriaContracts.BindingModels;
|
||
using PizzeriaContracts.BusinessLogicsContracts;
|
||
using PizzeriaContracts.SearchModels;
|
||
using PizzeriaContracts.StoragesContracts;
|
||
using PizzeriaContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace PizzeriaBusinessLogic
|
||
{
|
||
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);
|
||
var list = model == null ? _pizzaStorage.GetFullList() : _pizzaStorage.GetFilteredList(model);
|
||
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("Component. 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("Компонент с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|