CourseWork_Bank/Bank/BankBuisinessLogics/BuisinessLogics/AdditionsLogic.cs
2023-04-09 12:14:52 +04:00

136 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BankBusinessLogic.BusinessLogics
{
public class AdditionsLogic : IAdditionsLogic
{
private readonly ILogger _logger;
private readonly IAdditionsPlanStorage _additionsStorage;
public AdditionsLogic(ILogger<AdditionsLogic> logger, IAdditionsPlanStorage mealPlanStorage)
{
_logger = logger;
_additionsStorage = mealPlanStorage;
}
public bool Create(AdditionsBindingModel model)
{
CheckModel(model);
if (_additionsStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(AdditionsBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_additionsStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public AdditionsPlanViewModel? ReadElement(AdditionsSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. MealPlanName:{MealPlanName}.Id:{Id}", model.AdditionsName, model.Id);
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;
}
public List<AdditionsPlanViewModel>? ReadList(AdditionsSearchModel? model)
{
_logger.LogInformation("ReadList. MealPlanName:{MealPlanName}.Id:{ Id}", model?.AdditionsName, model?.Id);
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;
}
public bool Update(AdditionsBindingModel model)
{
CheckModel(model);
if (_additionsStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(AdditionsBindingModel model, bool withParams = true)
{
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);
var element = _additionsStorage.GetElement(new AdditionsSearchModel
{
AdditionsName = model.MealPlanName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("План питания с таким названием уже есть");
}
}
}
}