2024-04-28 21:02:22 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.BusinessLogicContracts;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-04-28 21:55:25 +04:00
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogics.BusinessLogic
|
|
|
|
|
{
|
2024-04-28 21:02:22 +04:00
|
|
|
|
public class CostLogic : ICostLogic
|
2024-04-28 21:55:25 +04:00
|
|
|
|
{
|
2024-04-28 21:02:22 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ICostStorage _costStorage;
|
|
|
|
|
|
|
|
|
|
public CostLogic(ILogger<CostLogic> logger, ICostStorage costStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_costStorage = costStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CheckModel(CostBindingModel model, bool checkParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (checkParams is false)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.NameOfCost))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Имя затраты не должно быть пустым");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.Price <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Стоимость должна быть больше 0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(CostBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
var result = _costStorage.Insert(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Затрата не создана");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Была создана сущность {@CostViewModel}", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки создать элемент по {@CostBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(CostBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
var result = _costStorage.Delete(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Удалить затрату не удалось");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Была удалена сущность {@CostViewModel}", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки удалить элемент по {@CostBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CostViewModel ReadElement(CostSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _costStorage.GetElement(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Не получилось получить id {model.Id}");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Извлечение элемента {@CostViewModel} c затрат по {@CostSearchModel} модели", result, model);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки получить элемент по {@CostSearchModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CostViewModel> ReadList(CostSearchModel? model = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var results = model != null ? _costStorage.GetFilteredList(model) : _costStorage.GetFullList();
|
|
|
|
|
_logger.LogDebug("Список полученных статей затрат {@costs}", results);
|
|
|
|
|
_logger.LogInformation("Извлечение списка c затрат по {@CostSearchModel} модели", model);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки получить список по {@CostSearchModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(CostBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//CheckModel(model);
|
|
|
|
|
var result = _costStorage.Update(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Не получилось обновить затраты");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Обновлена сущность на {@CostViewModel}", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки обновить элемент по {@CostBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-28 21:55:25 +04:00
|
|
|
|
}
|
|
|
|
|
}
|