ISEbd-21_Kuklew_M.I._Chegod.../CarCenterBusinessLogics/AccountLogic.cs
2024-04-29 22:39:08 +04:00

117 lines
4.9 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 CarCenterContracts.BusinessLogicContracts;
using CarCenterContracts.BindingModels;
using CarCenterContracts.SearchModels;
using CarCenterContracts.ViewModels;
using Microsoft.Extensions.Logging;
using CarCenterContracts.StoragesContracts;
namespace CarCenterBusinessLogics.BusinessLogics
{
public class AccountLogic : IAccountLogic
{
private readonly ILogger _logger;
private readonly IAccountStorage _accountStorage;
private readonly ICarLogic _CarStorage;
public AccountLogic(ILogger<AccountLogic> logger, IAccountStorage accountStorage, ICarLogic CarStorage)
{
_logger = logger;
_accountStorage = accountStorage;
_CarStorage = CarStorage;
}
public List<AccountViewModel> ReadList(AccountSearchModel? model)
{
try
{
var results = model != null ? _accountStorage.GetFilteredList(model) : _accountStorage.GetFullList();
_logger.LogDebug("Список полученных счетов: {@accounts}", results);
_logger.LogInformation("Извлечение списка в количестве {Count} c счетовой по модели: {@AccountSearchModel}", results.Count, model);
return results;
}
catch (Exception e)
{
_logger.LogError(e, "Произошла ошибка при попытки получить список по модели: {@AccountSearchModel}", model);
throw;
}
}
public AccountViewModel ReadElement(AccountSearchModel model)
{
try
{
var result = _accountStorage.GetElement(model);
if (result == null)
{
throw new ArgumentNullException($"Результат получения элемента с айди {model.Id} оказался нулевым");
}
_logger.LogInformation("Извлечение счета {@AccountViewModel} по модели: {@AccountSearchModel}", result, model);
return result;
}
catch (Exception e)
{
_logger.LogError(e, "Произошла ошибка при попытки получить элемент по модели: {@AccountSearchModel}", model);
throw;
}
}
public bool Create(AccountBindingModel model)
{
try
{
CheckModel(model);
var result = _accountStorage.Insert(model);
if (result == null)
{
throw new ArgumentNullException($"Результат создания счета оказался нулевым");
}
_logger.LogInformation("Была создана сущность: {@AccountViewModel}", result);
return true;
}
catch (Exception e)
{
_logger.LogError(e, "Произошла ошибка при попытки создать элемент по модели: {@AccountBindingModel}", model);
throw;
}
}
private void CheckModel(AccountBindingModel model, bool withParams = true)
{
const string txt = "Произошла ошибка на уровне проверки AccountBindingModel.";
if (model == null)
{
throw new ArgumentNullException(nameof(model), txt);
}
if (!withParams)
{
return;
}
if (model.Price < 0)
{
throw new ArgumentNullException(nameof(model.Price), txt + "Оплаченная стоимость не может быть отрицательной");
}
}
public bool GetAccountInfo(AccountSearchModel model,
out double currentBalance,
out double paidPrice)
{
try
{
var accounts = ReadList(model);
paidPrice = 0;
if (accounts == null || accounts.Count == 0)
{
currentBalance = 0;
return true;
}
currentBalance = accounts.Sum(x => x.Price);
_logger.LogInformation(
"По автомобилю({Id}) и клиенту({Id}) получена полная стоимость {fullPrice} и оплаченная стоимость {paidPrice}",
model.CarId, model.ClientId, currentBalance, paidPrice);
return true;
}
catch (Exception e)
{
_logger.LogError(e, "При попытке получения счетов по {@searchModel} произошла ошибка", model);
throw;
}
}
}
}