122 lines
4.2 KiB
C#
122 lines
4.2 KiB
C#
using BankContracts.BindingModels;
|
||
using BankContracts.BusinessLogicsContracts;
|
||
using BankContracts.SearchModels;
|
||
using BankContracts.StoragesContracts;
|
||
using BankContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace BankBusinessLogic.BusinessLogics
|
||
{
|
||
public class CurrencyLogic : ICurrencyLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly ICurrencyStorage _currencyStorage;
|
||
public CurrencyLogic(ILogger<CurrencyLogic> logger,
|
||
ICurrencyStorage currencyStorage)
|
||
{
|
||
_logger = logger;
|
||
_currencyStorage = currencyStorage;
|
||
}
|
||
public bool Create(CurrencyBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_currencyStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(CurrencyBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_currencyStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(CurrencyBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
||
if (_currencyStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public List<CurrencyViewModel>? ReadList(CurrencySearchModel? model)
|
||
{
|
||
_logger.LogInformation(
|
||
"ReadList. CurrencyName: {CurrencyName}. Id: {Id}",
|
||
model?.CurrencyName, model?.Id);
|
||
var list = model == null ? _currencyStorage.GetFullList() :
|
||
_currencyStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
||
return list;
|
||
}
|
||
public CurrencyViewModel? ReadElement(CurrencySearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation(
|
||
"ReadElement. CurrencyName: {CurrencyName}. Id: {Id}",
|
||
model.CurrencyName, model.Id);
|
||
var element = _currencyStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
|
||
return element;
|
||
}
|
||
private void CheckModel(CurrencyBindingModel model,
|
||
bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.CurrencyName))
|
||
{
|
||
throw new ArgumentNullException(
|
||
"Отсутствует название валюты",
|
||
nameof(model.CurrencyName));
|
||
}
|
||
if (model.Course <= 0)
|
||
{
|
||
throw new ArgumentNullException(
|
||
"Курс должен быть больше 0",
|
||
nameof(model.Course));
|
||
}
|
||
_logger.LogInformation("Currency. Id: {Id}. CurrencyName: " +
|
||
"{CurrencyName}. Course: {Course}.",
|
||
model.Id, model.CurrencyName, model.Course);
|
||
var element = _currencyStorage.GetElement(new CurrencySearchModel
|
||
{
|
||
CurrencyName = model.CurrencyName,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException(
|
||
"Курс с таким названием уже существует");
|
||
}
|
||
}
|
||
}
|
||
}
|