PIbd-23_Elatomtsev_L.K._Cou.../Bank/BankBusinessLogic/BusinessLogics/DepositLogic.cs

109 lines
3.7 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 DepositLogic : IDepositLogic
{
private readonly ILogger _logger;
private readonly IDepositStorage _depositStorage;
public DepositLogic(ILogger<DepositLogic> logger,
IDepositStorage depositStorage)
{
_logger = logger;
_depositStorage = depositStorage;
}
public bool Create(DepositBindingModel model)
{
CheckModel(model);
if (_depositStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DepositBindingModel model)
{
CheckModel(model);
if (_depositStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DepositBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_depositStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public List<DepositViewModel>? ReadList(DepositSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _depositStorage.GetFullList() :
_depositStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public DepositViewModel? ReadElement(DepositSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _depositStorage.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(DepositBindingModel model,
bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.WorkerId <= 0)
{
throw new ArgumentNullException(
"Идентификатор работника должен быть больше 0",
nameof(model.WorkerId));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException(
"Сумма должна быть больше 0",
nameof(model.Sum));
}
_logger.LogInformation("Deposit. Id: {Id}. WorkerId: {WorkerId}." +
"Sum: {Sum}. OpeningDate: {OpeningDate}.",
model.Id, model.WorkerId, model.Sum, model.OpeningDate);
}
}
}