CourseWork_Bank/Bank/BankBusinessLogic/BusinessLogics/CurrencyPurchaseLogic.cs

113 lines
4.0 KiB
C#

using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using BankContracts.StoragesContracts;
namespace BankBusinessLogic.BusinessLogics
{
public class CurrencyPurchaseLogic : ICurrencyPurchaseLogic
{
private readonly ILogger _logger;
private readonly ICurrencyPurchaseStorage _CurrencyPurchaseStorage;
public CurrencyPurchaseLogic(ILogger<CurrencyPurchaseLogic> logger, ICurrencyPurchaseStorage CurrencyPurchaseStorage)
{
_logger = logger;
_CurrencyPurchaseStorage = CurrencyPurchaseStorage;
}
public CurrencyPurchaseViewModel? ReadElement(CurrencyPurchaseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _CurrencyPurchaseStorage.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<CurrencyPurchaseViewModel>? ReadList(CurrencyPurchaseSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _CurrencyPurchaseStorage.GetFullList() : _CurrencyPurchaseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Create(CurrencyPurchaseBindingModel model)
{
CheckModel(model);
if (_CurrencyPurchaseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CurrencyPurchaseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_CurrencyPurchaseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(CurrencyPurchaseBindingModel model)
{
CheckModel(model);
if (_CurrencyPurchaseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CurrencyPurchaseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.BankOperatorId < 0)
{
throw new InvalidOperationException("Id оператора банка меньше нуля!");
}
if(model.CurrencyId < 0)
{
throw new InvalidOperationException("Id валюты меньше нуля!");
}
if (model.Amount <= 0) {
throw new InvalidOperationException("Количество закупленной валюты неположительно!");
}
_logger.LogInformation("Id: {Id}. Amount:{ Amount}. BankOperatorId: { BankOperatorId}. CurrencyId: {CurrencyId}.", model.Id, model.Amount, model.BankOperatorId, model.CurrencyId);
}
}
}