PIbd-23_Zargarov_M.A._Pizzeria/Pizzeria/PizzeriaBusinessLogic/PizzaLogic.cs

117 lines
4.2 KiB
C#
Raw Normal View History

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