CourseWork_Bank/Bank/BankBusinessLogic/BusinessLogics/CurrencyLogic.cs

101 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 List<CurrencyViewModel>? ReadList(CurrencySearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", 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. Id:{ Id}", 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;
}
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;
}
private void CheckModel(CurrencyBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия валюты!", nameof(model.Name));
}
_logger.LogInformation("Name:{ Name}.Id: { Id}", model.Name, model.Id);
}
}
}