2023-04-08 19:57:10 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.BusinessLogicsContracts;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public class AdditionsLogic : IAdditionsLogic
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2023-04-08 22:43:54 +04:00
|
|
|
|
private readonly IAdditionsPlanStorage _mealPlanStorage;
|
2023-04-08 19:57:10 +04:00
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public AdditionsLogic(ILogger<AdditionsLogic> logger, IAdditionsPlanStorage mealPlanStorage)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_mealPlanStorage = mealPlanStorage;
|
|
|
|
|
}
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public bool Create(AdditionsBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_mealPlanStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public bool Delete(AdditionsBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_mealPlanStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public AdditionsPlanViewModel? ReadElement(AdditionsSearchModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. MealPlanName:{MealPlanName}.Id:{Id}", model.MealPlanName, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _mealPlanStorage.GetElement(model);
|
|
|
|
|
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public List<AdditionsPlanViewModel>? ReadList(AdditionsSearchModel? model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. MealPlanName:{MealPlanName}.Id:{ Id}", model?.MealPlanName, model?.Id);
|
|
|
|
|
|
|
|
|
|
var list = model == null ? _mealPlanStorage.GetFullList() : _mealPlanStorage.GetFilteredList(model);
|
|
|
|
|
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public bool Update(AdditionsBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_mealPlanStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
private void CheckModel(AdditionsBindingModel model, bool withParams = true)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.MealPlanName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия плана питания", nameof(model.MealPlanName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.MealPlanPrice<0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Стоимость плана питания не может быть меньше 0", nameof(model.MealPlanPrice));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("MealPlan. MealPlanName:{MealPlanName}.MealPlanPrice:{ MealPlanPrice}. Id: { Id}", model.MealPlanName, model.MealPlanPrice, model.Id);
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
var element = _mealPlanStorage.GetElement(new AdditionsSearchModel
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
MealPlanName = model.MealPlanName
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("План питания с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|