From a4c94379d8c42893dd531d85e0d6de6901cd3192 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 28 Apr 2024 22:39:14 +0400 Subject: [PATCH] add businessLogic --- .../BusinessLogic/AccountLogic.cs | 106 +++++++++++++++++ .../BusinessLogic/CardLogic.cs | 2 +- .../BusinessLogic/ClientLogic.cs | 2 +- .../BusinessLogic/ManagerLogic.cs | 109 ++++++++++++++++++ .../BusinessLogic/OperationLogic.cs | 2 +- .../BusinessLogic/RequestLogic.cs | 5 +- .../BusinessLogic/TransferLogic.cs | 106 +++++++++++++++++ .../BusinessLogic/WithdrawalLogic.cs | 97 ++++++++++++++++ Bank/BankDatabaseImplement/Models/Account.cs | 4 +- Bank/BankDatabaseImplement/Models/Transfer.cs | 2 +- .../Models/Withdrawal.cs | 2 +- 11 files changed, 428 insertions(+), 9 deletions(-) create mode 100644 Bank/BankBusinessLogic/BusinessLogic/AccountLogic.cs create mode 100644 Bank/BankBusinessLogic/BusinessLogic/ManagerLogic.cs create mode 100644 Bank/BankBusinessLogic/BusinessLogic/TransferLogic.cs create mode 100644 Bank/BankBusinessLogic/BusinessLogic/WithdrawalLogic.cs diff --git a/Bank/BankBusinessLogic/BusinessLogic/AccountLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/AccountLogic.cs new file mode 100644 index 0000000..678a0b8 --- /dev/null +++ b/Bank/BankBusinessLogic/BusinessLogic/AccountLogic.cs @@ -0,0 +1,106 @@ +using BankContracts.BindingModels; +using BankContracts.BusinessLogicsContracts; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankBusinessLogic.BusinessLogic +{ + public class AccountLogic : IAccountLogic + { + private readonly ILogger _logger; + private readonly IAccountStorage _accountStorage; + public AccountLogic(ILogger logger, IAccountStorage accountStorage) + { + _logger = logger; + _accountStorage = accountStorage; + } + + public List? ReadList(AccountSearchModel? model) + { + _logger.LogInformation("ReadList. Account. Number: {Number}, Id: {Id}", + model?.Number, model?.Id); + var list = model == null ? + _accountStorage.GetFullList() : _accountStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("readList return null list"); + return null; + } + _logger.LogInformation("ReadList Count: {Count}", list.Count); + return list; + } + + public AccountViewModel? ReadElement(AccountSearchModel model) + { + _logger.LogInformation("ReadElement. Account. Number: {Number}, Id: {Id}", + model?.Number, model?.Id); + if (model == null) + throw new ArgumentNullException(nameof(model)); + var element = _accountStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find Id:{ Id}", element.Id); + return element; + } + + public bool Create(AccountBindingModel model) + { + CheckModel(model); + if (_accountStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(AccountBindingModel model) + { + CheckModel(model); + if (_accountStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(AccountBindingModel model) + { + CheckModel(model, false); + if (_accountStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(AccountBindingModel model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Number)) + throw new ArgumentNullException("Нет номера счёта", nameof(model.Number)); + if (model.ManagerId <= 0) + throw new ArgumentNullException("Нет id менеджера", nameof(model.ManagerId)); + _logger.LogInformation("Account. Number: {Number}. ManagerId: {ManagerId}. Id: {Id} ", + model.Number, model.ManagerId, model.Id); + var element = _accountStorage.GetElement(new AccountSearchModel { Number = model.Number }); + if (element != null) + throw new InvalidOperationException("Счет с таким номером уже есть"); + } + } +} diff --git a/Bank/BankBusinessLogic/BusinessLogic/CardLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/CardLogic.cs index ba1951f..99df11a 100644 --- a/Bank/BankBusinessLogic/BusinessLogic/CardLogic.cs +++ b/Bank/BankBusinessLogic/BusinessLogic/CardLogic.cs @@ -36,8 +36,8 @@ namespace BankBusinessLogic.BusinessLogic } public CardViewModel? ReadElement(CardSearchModel model) { - if (model == null) throw new ArgumentNullException(nameof(model)); _logger.LogInformation("ReadElement Number: {Number}, Id: {Id}", model?.Number, model?.Id); + if (model == null) throw new ArgumentNullException(nameof(model)); var element = _cardStorage.GetElement(model); if (element == null) { diff --git a/Bank/BankBusinessLogic/BusinessLogic/ClientLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/ClientLogic.cs index 4a1d194..5713da0 100644 --- a/Bank/BankBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/Bank/BankBusinessLogic/BusinessLogic/ClientLogic.cs @@ -37,8 +37,8 @@ namespace BankBusinessLogic.BusinessLogic } public ClientViewModel? ReadElement(ClientSearchModel model) { - if (model == null) throw new ArgumentNullException(nameof(model)); _logger.LogInformation("ReadElement. Fio: {Fio}. Id: {Id}", model?.Fio, model?.Id); + if (model == null) throw new ArgumentNullException(nameof(model)); var element = _clientStorage.GetElement(model); if (element == null) { diff --git a/Bank/BankBusinessLogic/BusinessLogic/ManagerLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/ManagerLogic.cs new file mode 100644 index 0000000..595e248 --- /dev/null +++ b/Bank/BankBusinessLogic/BusinessLogic/ManagerLogic.cs @@ -0,0 +1,109 @@ +using BankContracts.BindingModels; +using BankContracts.BusinessLogicsContracts; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankBusinessLogic.BusinessLogic +{ + public class ManagerLogic : IManagerLogic + { + private readonly ILogger _logger; + private readonly IManagerStorage _managerStorage; + + public ManagerLogic(ILogger logger, IManagerStorage managerStorage) + { + _logger = logger; + _managerStorage = managerStorage; + } + + public List? ReadList(ManagerSearchModel? model) + { + _logger.LogInformation("ReadList. Managers. Fio:{ Fio}. Id: {Id}", + model?.Fio, model?.Id); + var list = model == null ? + _managerStorage.GetFullList() : _managerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList Count: {Count}", list.Count); + return list; + } + + public ManagerViewModel? ReadElement(ManagerSearchModel model) + { + _logger.LogInformation("ReadElement. Fio: {Fio}. Id: {Id}", + model?.Fio, model?.Id); + if (model == null) + throw new ArgumentNullException(nameof(model)); + var element = _managerStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); + return element; + } + + public bool Create(ManagerBindingModel model) + { + CheckModel(model); + if (_managerStorage.Insert(model) == null) + { + _logger.LogWarning("insert operation failed"); + return false; + } + return true; + } + + public bool Update(ManagerBindingModel model) + { + CheckModel(model); + if (_managerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ManagerBindingModel model) + { + CheckModel(model, false); + if (_managerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ManagerBindingModel model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Fio)) + throw new ArgumentNullException("Нет ФИО менеджера", nameof(model.Fio)); + if (string.IsNullOrEmpty(model.Email)) + throw new ArgumentNullException("Нет email менеджера", nameof(model.Email)); + if (string.IsNullOrEmpty(model.Password)) + throw new ArgumentNullException("Нет пароля менеджера", nameof(model.Password)); + _logger.LogInformation("Manager. FIO:{FIO}. Email:{ Email}. Password:{ Password}. Id: { Id} ", + model.Fio, model.Email, model.Password, model.Id); + var element = _managerStorage.GetElement(new ManagerSearchModel { Email = model.Email }); + if (element != null) + throw new InvalidOperationException("Менеджер с таким Email уже есть"); + } + } +} diff --git a/Bank/BankBusinessLogic/BusinessLogic/OperationLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/OperationLogic.cs index 22ed10e..42e46d9 100644 --- a/Bank/BankBusinessLogic/BusinessLogic/OperationLogic.cs +++ b/Bank/BankBusinessLogic/BusinessLogic/OperationLogic.cs @@ -36,8 +36,8 @@ namespace BankBusinessLogic.BusinessLogic } public OperationViewModel? ReadElement(OperationSearchModel model) { - if (model == null) throw new ArgumentNullException(nameof(model)); _logger.LogInformation("ReadElement. Id: {Id}", model?.Id); + if (model == null) throw new ArgumentNullException(nameof(model)); var element = _operationStorage.GetElement(model); if (element == null) { diff --git a/Bank/BankBusinessLogic/BusinessLogic/RequestLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/RequestLogic.cs index 04585a1..224edb7 100644 --- a/Bank/BankBusinessLogic/BusinessLogic/RequestLogic.cs +++ b/Bank/BankBusinessLogic/BusinessLogic/RequestLogic.cs @@ -35,10 +35,11 @@ namespace BankBusinessLogic.BusinessLogic _logger.LogInformation("ReadList Count: {Count}", list.Count); return list; } - public RequestViewModel? ReadElement(RequestSearchModel model) + public RequestViewModel? ReadElement(RequestSearchModel model) { - if (model == null) throw new ArgumentNullException(nameof(model)); _logger.LogInformation("ReadElement. Id: {Id}", model?.Id); + if (model == null) + throw new ArgumentNullException(nameof(model)); var element = _requestStorage.GetElement(model); if (element == null) { diff --git a/Bank/BankBusinessLogic/BusinessLogic/TransferLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/TransferLogic.cs new file mode 100644 index 0000000..eece54e --- /dev/null +++ b/Bank/BankBusinessLogic/BusinessLogic/TransferLogic.cs @@ -0,0 +1,106 @@ +using BankContracts.BindingModels; +using BankContracts.BusinessLogicsContracts; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankBusinessLogic.BusinessLogic +{ + public class TransferLogic : ITransferLogic + { + private readonly ILogger _logger; + private readonly ITransferStorage _transferStorage; + public TransferLogic(ILogger logger, ITransferStorage transferStorage) + { + _logger = logger; + _transferStorage = transferStorage; + } + + public List? ReadList(TransferSearchModel? model) + { + _logger.LogInformation("ReadList. Transfer. Id: {Id}", model?.Id); + var list = model == null ? + _transferStorage.GetFullList() : _transferStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList Count: {Count}", list.Count); + return list; + } + + public TransferViewModel? ReadElement(TransferSearchModel model) + { + _logger.LogInformation("ReadElement. Transfer. Id: {Id}", model?.Id); + if (model == null) throw new ArgumentNullException(nameof(model)); + var element = _transferStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); + return element; + } + + public bool Create(TransferBindingModel model) + { + CheckModel(model); + if (_transferStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(TransferBindingModel model) + { + CheckModel(model); + if (_transferStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(TransferBindingModel model) + { + CheckModel(model, false); + if (_transferStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(TransferBindingModel model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (model.Sum <= 0) + throw new ArgumentNullException("Неверная сумма перевода", + nameof(model.Sum)); + if (model.SenderAccountId <= 0) + throw new ArgumentNullException("Неверный id счёта отправителя", + nameof(model.SenderAccountId)); + if (model.RecipientAccountId <= 0) + throw new ArgumentNullException("Неверный id счёта получателя", + nameof(model.RecipientAccountId)); + _logger.LogInformation("Transfer. Sum:{Sum}. SenderAccountId: {SenderAccountId}. " + + "RecipientAccountId: {RecipientAccountId}. Id: { Id} ", + model.Sum, model.SenderAccountId, model.RecipientAccountId, model.Id); + } + } +} diff --git a/Bank/BankBusinessLogic/BusinessLogic/WithdrawalLogic.cs b/Bank/BankBusinessLogic/BusinessLogic/WithdrawalLogic.cs new file mode 100644 index 0000000..4240ed3 --- /dev/null +++ b/Bank/BankBusinessLogic/BusinessLogic/WithdrawalLogic.cs @@ -0,0 +1,97 @@ +using BankContracts.BindingModels; +using BankContracts.BusinessLogicsContracts; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankBusinessLogic.BusinessLogic +{ + public class WithdrawalLogic : IWithdrawalLogic + { + private readonly ILogger _logger; + private readonly IWithdrawalStorage _withdrawalStorage; + public WithdrawalLogic(ILogger logger, IWithdrawalStorage withdrawalStorage) + { + _logger = logger; + _withdrawalStorage = withdrawalStorage; + } + + public List? ReadList(WithdrawalSearchModel? model) + { + _logger.LogInformation("ReadList. Withdrawal. Id: {Id}", model?.Id); + var list = model == null ? + _withdrawalStorage.GetFullList() : _withdrawalStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList Count: {Count}", list.Count); + return list; + } + + public WithdrawalViewModel? ReadElement(WithdrawalSearchModel model) + { + _logger.LogInformation("ReadElement. Withdrawal. Id: {Id}", model?.Id); + if (model == null) + throw new ArgumentNullException(nameof(model)); + var element = _withdrawalStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); + return element; + } + + public bool Create(WithdrawalBindingModel model) + { + CheckModel(model); + if (_withdrawalStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(WithdrawalBindingModel model) + { + CheckModel(model); + if (_withdrawalStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(WithdrawalBindingModel model) + { + CheckModel(model, false); + if (_withdrawalStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(WithdrawalBindingModel model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + _logger.LogInformation("Withdrawal. Id: { Id} ", model.Id); + } + } +} diff --git a/Bank/BankDatabaseImplement/Models/Account.cs b/Bank/BankDatabaseImplement/Models/Account.cs index 2a6bec8..df2408f 100644 --- a/Bank/BankDatabaseImplement/Models/Account.cs +++ b/Bank/BankDatabaseImplement/Models/Account.cs @@ -39,9 +39,9 @@ namespace BankDatabaseImplement.Models { Id = model.Id, Number = model.Number, - ReleaseDate = model.ReleaseDate, + ReleaseDate = DateOnly.FromDateTime(DateTime.Now), ManagerId = model.ManagerId, - Money = model.Money, + Money = 0, }; } diff --git a/Bank/BankDatabaseImplement/Models/Transfer.cs b/Bank/BankDatabaseImplement/Models/Transfer.cs index 33c71be..04ec37e 100644 --- a/Bank/BankDatabaseImplement/Models/Transfer.cs +++ b/Bank/BankDatabaseImplement/Models/Transfer.cs @@ -34,7 +34,7 @@ namespace BankDatabaseImplement.Models { Id = model.Id, Sum = model.Sum, - TransferTime = model.TransferTime, + TransferTime = DateTime.Now, SenderAccountId = model.SenderAccountId, RecipientAccountId = model.RecipientAccountId, }; diff --git a/Bank/BankDatabaseImplement/Models/Withdrawal.cs b/Bank/BankDatabaseImplement/Models/Withdrawal.cs index 27146c7..403f1c0 100644 --- a/Bank/BankDatabaseImplement/Models/Withdrawal.cs +++ b/Bank/BankDatabaseImplement/Models/Withdrawal.cs @@ -43,7 +43,7 @@ namespace BankDatabaseImplement.Models return new Withdrawal { Id = model.Id, - WithdrawalTime = model.WithdrawalTime, + WithdrawalTime = DateTime.Now, Accounts = model.WithdrawalAccounts.Select(x => new AccountWithdrawal { Account = context.Accounts.First(y => y.Id == x.Key)