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 { public class CardLogic : ICardLogic { private readonly ILogger _logger; private readonly ICardStorage _cardStorage; private readonly IAccountLogic _accountLogic; private readonly IDebitingLogic _debitingLogic; private readonly ICreditingLogic _creditingLogic; // Конструктор public CardLogic(ILogger logger, ICardStorage cardStorage, IAccountLogic accountLogic, IDebitingLogic debitingLogic, ICreditingLogic creditingLogic) { _logger = logger; _cardStorage = cardStorage; _accountLogic = accountLogic; _debitingLogic = debitingLogic; _creditingLogic = creditingLogic; } // Вывод конкретной банковской карты public CardViewModel? ReadElement(CardSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. CardNumber:{Number}.Id:{ Id}", model.Number, model.Id); var element = _cardStorage.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? ReadList(CardSearchModel? model) { _logger.LogInformation("ReadList. CardId:{Id}", model?.Id); // list хранит весь список в случае, если model пришло со значением null на вход метода var list = model == null ? _cardStorage.GetFullList() : _cardStorage.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(CardBindingModel model) { CheckModel(model); 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) { _logger.LogWarning("Delete operation failed"); return false; } return true; } // Проверка входного аргумента для методов Insert, Update и Delete private void CheckModel(CardBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } // Так как при удалении передаём как параметр false if (!withParams) { return; } // Проверка на наличие номера у банковской карты if (string.IsNullOrEmpty(model.Number) || model.Number.Length != 16) { throw new ArgumentNullException("Неправильный номер карты", nameof(model.Number)); } // Здесь стоит обсудить про баланс !!!!!! И про добавление ID клиента ниже! // Проверка на конкретный период действия карты if (model.Period < DateTime.Now) { throw new ArgumentNullException("Нет периода действия", nameof(model.Period)); } // Проверка на наличие id клиента, получеившего карту if (model.ClientId < 0) { throw new ArgumentNullException("Некорректный Id кассира, открывшего счёт", nameof(model.ClientId)); } // Для проверка на наличие такой же банковской карты var cardElement = _cardStorage.GetElement(new CardSearchModel { Number = model.Number, }); // Если элемент найден и его Id не совпадает с Id переданного объекта if (cardElement != null && cardElement.Id != model.Id) { throw new InvalidOperationException("Карта с таким ноиером уже есть"); } var accountElement = _accountLogic.ReadElement(new AccountSearchModel { Id = model.AccountId, ClientId = model.ClientId }); if (accountElement != null && accountElement.ClientId != model.ClientId) { throw new InvalidOperationException("Это не счёт данного клиента"); } _logger.LogInformation("Card. Number:{Number}.ClientId:{ClientID}.Patronymic:{Period}.Id:{Id}", model.Number, model.Period.ToString(), model.ClientId, model.Id); } } }