2024-04-29 21:23:40 +04:00
|
|
|
|
using BankContracts.BindingModels.Client;
|
|
|
|
|
using BankContracts.BusinessLogicsContracts.Cashier;
|
|
|
|
|
using BankContracts.BusinessLogicsContracts.Client;
|
|
|
|
|
using BankContracts.SearchModels.Cashier;
|
|
|
|
|
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 CardLogic : ICardLogic
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
private readonly ICardStorage _cardStorage;
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
private readonly IAccountLogic _accountLogic;
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
private readonly IDebitingLogic _debitingLogic;
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
private readonly ICreditingLogic _creditingLogic;
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Конструктор
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public CardLogic(ILogger<CardLogic> logger, ICardStorage cardStorage, IAccountLogic accountLogic,
|
|
|
|
|
IDebitingLogic debitingLogic, ICreditingLogic creditingLogic)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_cardStorage = cardStorage;
|
|
|
|
|
_accountLogic = accountLogic;
|
|
|
|
|
_debitingLogic = debitingLogic;
|
|
|
|
|
_creditingLogic = creditingLogic;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Вывод конкретной банковской карты
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public CardViewModel? ReadElement(CardSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
_logger.LogInformation("ReadElement. CardNumber:{Number}.Id:{ Id}", model.Number, model.Id);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
var element = _cardStorage.GetElement(model);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Вывод всего списка банковских карт
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public List<CardViewModel>? ReadList(CardSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. CardId:{Id}", model?.Id);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
|
|
|
|
// list хранит весь список в случае, если model пришло со значением null на вход метода
|
|
|
|
|
var list = model == null ? _cardStorage.GetFullList() : _cardStorage.GetFilteredList(model);
|
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-04-29 21:23:40 +04:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Создание банковской карты
|
|
|
|
|
public bool Create(CardBindingModel model)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
|
|
|
|
if (_cardStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Обновление банковской карты
|
|
|
|
|
public bool Update(CardBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_cardStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Удаление банковской карты
|
|
|
|
|
public bool Delete(CardBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_cardStorage.Delete(model) == null)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
2024-04-30 16:37:32 +04:00
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
2024-04-29 21:23:40 +04:00
|
|
|
|
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(CardBindingModel 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
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
|
|
|
|
// Проверка на наличие номера у банковской карты
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Number) || model.Number.Length != 16)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Неправильный номер карты", nameof(model.Number));
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Проверка баланса банковской карты (чтобы не было отрицательным)
|
|
|
|
|
if (model.Balance < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Изначальный баланс карты не может быть < 0", nameof(model.Balance));
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Проверка на конкретный период действия карты
|
|
|
|
|
if (model.Period < DateTime.Now)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет периода действия", nameof(model.Period));
|
|
|
|
|
}
|
2024-04-30 16:37:32 +04:00
|
|
|
|
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Проверка на наличие id клиента, получившего карту (лишним не будет)
|
2024-04-30 16:37:32 +04:00
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный Id кассира, открывшего счёт", nameof(model.ClientId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Для проверка на наличие такой же банковской карты
|
|
|
|
|
var cardElement = _cardStorage.GetElement(new CardSearchModel
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
Number = model.Number,
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Если элемент найден и его Id не совпадает с Id переданного объекта
|
|
|
|
|
if (cardElement != null && cardElement.Id != model.Id)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Карта с таким ноиером уже есть");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var accountElement = _accountLogic.ReadElement(new AccountSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.AccountId,
|
|
|
|
|
ClientId = model.ClientId
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Проверка привязан ли счёт к данному клиенту
|
|
|
|
|
if (accountElement != null && accountElement.ClientId != model.ClientId)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Это не счёт данного клиента");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Card. Number:{Number}.ClientId:{ClientID}.Patronymic:{Period}.Id:{Id}",
|
|
|
|
|
model.Number, model.Period.ToString(), model.ClientId, model.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|