2024-04-29 21:23:40 +04:00
|
|
|
|
using BankContracts.BindingModels.Client;
|
|
|
|
|
using BankContracts.BusinessLogicsContracts.Client;
|
|
|
|
|
using BankContracts.SearchModels.Client;
|
|
|
|
|
using BankContracts.StoragesModels.Client;
|
|
|
|
|
using BankContracts.ViewModels.Client.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogic.BusinessLogic.Client
|
|
|
|
|
{
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Класс, реализующий бизнес-логику для снятия наличных с карты
|
|
|
|
|
public class DebitingLogic : IDebitingLogic
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IDebitingStorage _debitingStorage;
|
|
|
|
|
|
|
|
|
|
private readonly ICardStorage _cardStorage;
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Конструктор
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public DebitingLogic(ILogger<DebitingLogic> logger, IDebitingStorage debitingStorage, ICardStorage cardStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_debitingStorage = debitingStorage;
|
|
|
|
|
_cardStorage = cardStorage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Вывод конкретной операции на снятие наличных
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// list хранит весь список в случае, если model пришло со значением null на вход метода
|
|
|
|
|
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 Create(DebitingBindingModel model)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_debitingStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Обновление операции на снятие наличных
|
|
|
|
|
public bool Update(DebitingBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_debitingStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Удаление операции на снятие наличных
|
|
|
|
|
public bool Delete(DebitingBindingModel model)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (_debitingStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка входного аргумента для методов Insert, Update и Delete
|
|
|
|
|
private void CheckModel(DebitingBindingModel model, bool withParams = true)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Так как при удалении передаём как параметр false
|
|
|
|
|
if (!withParams)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
2024-04-30 16:37:32 +04:00
|
|
|
|
return;
|
2024-04-29 21:23:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Проверка на корректность Id клиента банковской карты
|
2024-04-30 16:37:32 +04:00
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
{
|
2024-05-01 03:18:08 +04:00
|
|
|
|
throw new ArgumentNullException("Некорректный Id клиента", nameof(model.ClientId));
|
2024-04-30 16:37:32 +04:00
|
|
|
|
}
|
2024-04-29 21:23:40 +04:00
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка на корректность снятия суммы
|
|
|
|
|
if (model.Sum <= 0)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма операции должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка на корректную дату операции
|
|
|
|
|
if (model.DateDebit > DateTime.Now)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Дата не может быть меньше текущего времени", nameof(model.DateDebit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Debiting. Sum:{Sum}.CardId:{CardId}.DateDebit:{DateDebit}.Id:{Id}",
|
2024-04-29 21:23:40 +04:00
|
|
|
|
model.Sum, model.CardId, model.DateDebit.ToString(), model.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|