2023-04-01 10:24:54 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using BankYouBankruptContracts.BindingModels;
|
|
|
|
|
using BankYouBankruptContracts.BusinessLogicsContracts;
|
|
|
|
|
using BankYouBankruptContracts.SearchModels;
|
|
|
|
|
using BankYouBankruptContracts.StoragesContracts;
|
|
|
|
|
using BankYouBankruptContracts.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankYouBankruptBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class DebitingLogic : IDebitingLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IDebitingStorage _debitingStorage;
|
|
|
|
|
|
|
|
|
|
public DebitingLogic(ILogger<DebitingLogic> logger, IDebitingStorage debitingStorage) {
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_debitingStorage = debitingStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(DebitingBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_debitingStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(DebitingBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
if (_debitingStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DebitingViewModel? ReadElement(DebitingSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. DebitingId:{ Id }", model.Id);
|
|
|
|
|
var element = _debitingStorage.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<DebitingViewModel>? ReadList(DebitingSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. DebitingId:{Id}", model?.Id);
|
|
|
|
|
var list = model == null ? _debitingStorage.GetFullList() : _debitingStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(DebitingBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2023-05-14 20:22:27 +04:00
|
|
|
|
|
2023-04-01 10:24:54 +04:00
|
|
|
|
if (_debitingStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-05-14 20:22:27 +04:00
|
|
|
|
|
2023-04-01 10:24:54 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-05-14 20:22:27 +04:00
|
|
|
|
|
2023-04-01 10:24:54 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(DebitingBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма операции должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
2023-05-14 15:14:30 +04:00
|
|
|
|
if (model.DateClose < model.DateOpen)
|
2023-04-01 10:24:54 +04:00
|
|
|
|
{
|
2023-05-14 15:14:30 +04:00
|
|
|
|
throw new ArgumentNullException("Дата закрытия не может быть меньше даты открытия", nameof(model.DateClose));
|
2023-04-01 10:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 15:14:30 +04:00
|
|
|
|
_logger.LogInformation("Debiting. Sum:{Sum}.CardId:{CardId}.DateOpen:{DateOpen}.DateOpen:{DateOpen}.Id:{Id}",
|
|
|
|
|
model.Sum, model.CardId, model.DateOpen.ToString(), model.DateClose.ToString(), model.Id);
|
2023-04-01 10:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|