136 lines
4.1 KiB
C#
Raw Normal View History

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
{
private readonly ILogger _logger;
2023-04-09 12:14:52 +04:00
private readonly IAdditionsPlanStorage _additionsStorage;
2023-04-08 22:43:54 +04:00
public AdditionsLogic(ILogger<AdditionsLogic> logger, IAdditionsPlanStorage mealPlanStorage)
{
_logger = logger;
2023-04-09 12:14:52 +04:00
_additionsStorage = mealPlanStorage;
}
2023-04-08 22:43:54 +04:00
public bool Create(AdditionsBindingModel model)
{
CheckModel(model);
2023-04-09 12:14:52 +04:00
if (_additionsStorage.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)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
2023-04-09 12:14:52 +04:00
if (_additionsStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2023-05-21 17:01:50 +04:00
public AdditionsViewModel? ReadElement(AdditionsSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-04-09 12:14:52 +04:00
_logger.LogInformation("ReadElement. MealPlanName:{MealPlanName}.Id:{Id}", model.AdditionsName, model.Id);
2023-04-09 12:14:52 +04:00
var element = _additionsStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2023-05-21 17:01:50 +04:00
public List<AdditionsViewModel>? ReadList(AdditionsSearchModel? model)
{
2023-04-09 12:14:52 +04:00
_logger.LogInformation("ReadList. MealPlanName:{MealPlanName}.Id:{ Id}", model?.AdditionsName, model?.Id);
2023-04-09 12:14:52 +04:00
var list = model == null ? _additionsStorage.GetFullList() : _additionsStorage.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)
{
CheckModel(model);
2023-04-09 12:14:52 +04:00
if (_additionsStorage.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)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2023-05-21 17:01:50 +04:00
if (string.IsNullOrEmpty(model.AdditionsName))
{
2023-05-21 17:01:50 +04:00
throw new ArgumentNullException("Нет названия плана питания", nameof(model.AdditionsName));
}
2023-05-21 17:01:50 +04:00
if (model.AdditionsPrice<0)
{
2023-05-21 17:01:50 +04:00
throw new ArgumentNullException("Стоимость плана питания не может быть меньше 0", nameof(model.AdditionsPrice));
}
2023-05-21 17:01:50 +04:00
_logger.LogInformation("MealPlan. MealPlanName:{MealPlanName}.MealPlanPrice:{ MealPlanPrice}. Id: { Id}", model.AdditionsName, model.AdditionsPrice, model.Id);
2023-04-09 12:14:52 +04:00
var element = _additionsStorage.GetElement(new AdditionsSearchModel
{
2023-05-21 17:01:50 +04:00
AdditionsName = model.AdditionsName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("План питания с таким названием уже есть");
}
}
}
}