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

115 lines
4.0 KiB
C#
Raw Normal View History

using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BankBusinessLogic.BusinessLogics
{
public class RefillLogic : IRefillLogic
{
private readonly ILogger _logger;
private readonly IRefillStorage _refillStorage;
public RefillLogic(ILogger<RefillLogic> logger,
IRefillStorage refillStorage)
{
_logger = logger;
_refillStorage = refillStorage;
}
public bool Create(RefillBindingModel model)
{
CheckModel(model);
if (_refillStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(RefillBindingModel model)
{
CheckModel(model);
if (_refillStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(RefillBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_refillStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public List<RefillViewModel>? ReadList(RefillSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _refillStorage.GetFullList() :
_refillStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public RefillViewModel? ReadElement(RefillSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _refillStorage.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(RefillBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.DepositId <= 0)
{
throw new ArgumentNullException(
"Идентификатор вклада должен быть больше 0",
nameof(model.DepositId));
}
if (model.WorkerId <= 0)
{
throw new ArgumentNullException(
"Идентификатор работника должен быть больше 0",
nameof(model.WorkerId));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException(
"Сумма должна быть больше 0",
nameof(model.Sum));
}
_logger.LogInformation("Refill. Id: {Id}. DepositId: " +
"{DepositId}. WorkerId: {WorkerId}. Sum: {Sum}. " +
"RefillDate: {RefillDate}.", model.Id, model.DepositId,
model.WorkerId, model.Sum, model.RefillDate);
}
}
}