using System.Text.Json;
using BankContracts.BusinessLogicContracts;
using BankContracts.DataModels;
using BankContracts.Exceptions;
using BankContracts.Extensions;
using BankContracts.StorageContracts;
using Microsoft.Extensions.Logging;
namespace BankBusinessLogic.Implementations;
///
/// реализация бизнес логики для валюты
///
/// контракт валюты
/// логгер
internal class CurrencyBusinessLogicContract(
ICurrencyStorageContract currencyStorageContract,
ILogger logger
) : ICurrencyBusinessLogicContract
{
private readonly ICurrencyStorageContract _currencyStorageContract = currencyStorageContract;
private readonly ILogger _logger = logger;
public List GetAllCurrencies()
{
_logger.LogInformation("get all currencys programs");
return _currencyStorageContract.GetList();
}
public CurrencyDataModel GetCurrencyByData(string data)
{
_logger.LogInformation($"Get currencys program by data: {data}");
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _currencyStorageContract.GetElementById(data)
?? throw new ElementNotFoundException($"element not found: {data}");
}
return _currencyStorageContract.GetElementByAbbreviation(data)
?? throw new ElementNotFoundException($"element not found: {data}");
}
public List GetCurrencyByStorekeeper(string storekeeperId)
{
_logger.LogInformation("GetCurrencyByStorekeeper params: {storekeeperId}", storekeeperId);
if (storekeeperId.IsEmpty())
{
throw new ArgumentNullException(nameof(storekeeperId));
}
if (!storekeeperId.IsGuid())
{
throw new ValidationException(
"The value in the field storekeeperId is not a unique identifier."
);
}
return _currencyStorageContract.GetList(storekeeperId: storekeeperId)
?? throw new NullListException($"{storekeeperId}");
}
public void InsertCurrency(CurrencyDataModel currencyDataModel)
{
_logger.LogInformation(
"Insert currency: {currency}",
JsonSerializer.Serialize(currencyDataModel)
);
ArgumentNullException.ThrowIfNull(currencyDataModel);
currencyDataModel.Validate();
_currencyStorageContract.AddElement(currencyDataModel);
}
public void UpdateCurrency(CurrencyDataModel currencyDataModel)
{
_logger.LogInformation(
"Update currency: {currency}",
JsonSerializer.Serialize(currencyDataModel)
);
ArgumentNullException.ThrowIfNull(currencyDataModel);
currencyDataModel.Validate();
_currencyStorageContract.UpdElement(currencyDataModel);
}
}