diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs index 9a13751..9ea6438 100644 --- a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs @@ -68,35 +68,28 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics //метод, отвечающий за изменение баланса счёта public bool ChangeBalance(AccountSearchModel? model, int sum) { - try + //ищем счёт + var account = ReadElement(model); + + if (account == null) { - //ищем счёт - var account = ReadElement(model); - - if (account == null) - { - throw new ArgumentNullException("Счёт не найден", nameof(account)); - } - - //проверяем возможность операции снятия (sum может быть отрицательной) - if (sum + account.Balance < 0) - { - throw new ArgumentNullException("Операция невозможна. Недостаточно средств", nameof(account)); - } - - //обновляем балланс счёта - _accountStorage.Update(new AccountBindingModel - { - Id = account.Id, - Balance = account.Balance + sum - }); - - return true; + throw new ArgumentNullException("Счёт не найден", nameof(account)); } - catch (Exception ex) + + //проверяем возможность операции снятия (sum может быть отрицательной) + if (sum + account.Balance < 0) { - return false; + throw new ArgumentNullException("Операция невозможна. Недостаточно средств", nameof(account)); } + + //обновляем балланс счёта + _accountStorage.Update(new AccountBindingModel + { + Id = account.Id, + Balance = account.Balance + sum + }); + + return true; } public bool Create(AccountBindingModel model) diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/MoneyTransferLogic.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/MoneyTransferLogic.cs index 5dd522e..e481ac1 100644 --- a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/MoneyTransferLogic.cs +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/MoneyTransferLogic.cs @@ -3,6 +3,7 @@ using BankYouBankruptContracts.BusinessLogicsContracts; using BankYouBankruptContracts.SearchModels; using BankYouBankruptContracts.StoragesContracts; using BankYouBankruptContracts.ViewModels; +using BankYouBankruptDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -18,10 +19,13 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics private readonly IMoneyTransferStorage _moneyTransferStorage; - public MoneyTransferLogic(ILogger logger, IMoneyTransferStorage moneyTransferStorage) + public readonly ICreditingStorage _creditingStorage; + + public MoneyTransferLogic(ILogger logger, IMoneyTransferStorage moneyTransferStorage, ICreditingStorage creditingStorage) { _logger = logger; _moneyTransferStorage = moneyTransferStorage; + _creditingStorage = creditingStorage; } public MoneyTransferViewModel? ReadElement(MoneyTransferSearchModel model) @@ -78,6 +82,18 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics return false; } + if (!model.CreditingId.HasValue) + { + throw new Exception(); + } + + _creditingStorage.Update(new CreditingBindingModel + { + Id = model.CreditingId.Value, + DateClose = DateTime.Now, + Status = StatusEnum.Закрыта + }); + return true; } diff --git a/BankYouBankrupt/BankYouBankruptContracts/BindingModels/CreditingBindingModel.cs b/BankYouBankrupt/BankYouBankruptContracts/BindingModels/CreditingBindingModel.cs index cfb3242..a98005d 100644 --- a/BankYouBankrupt/BankYouBankruptContracts/BindingModels/CreditingBindingModel.cs +++ b/BankYouBankrupt/BankYouBankruptContracts/BindingModels/CreditingBindingModel.cs @@ -20,6 +20,6 @@ namespace BankYouBankruptContracts.BindingModels public DateTime? DateClose { get; set; } - public StatusEnum Status { get; set; } = StatusEnum.Открыта; + public StatusEnum Status { get; set; } } } diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs index da3d8c4..6d58574 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs @@ -34,6 +34,15 @@ namespace BankYouBankruptDatabaseImplement.Implements using var context = new BankYouBancruptDatabase(); + if (model.Status.HasValue) + { + return context.Creditings + .Include(x => x.Card) + .Where(x => x.Status == model.Status) + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Creditings .Include(x => x.Card) .Select(x => x.GetViewModel) @@ -90,8 +99,11 @@ namespace BankYouBankruptDatabaseImplement.Implements context.SaveChanges(); transaction.Commit(); - return crediting.GetViewModel; - } + return context.Creditings + .Include(x => x.Card) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } catch { transaction.Rollback(); diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs index 4827b59..00f74c5 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs @@ -43,7 +43,9 @@ namespace BankYouBankruptDatabaseImplement.Models CardId = CardId, CardNumber = Card.Number, Sum = Sum, - DateOpen = DateOpen + DateOpen = DateOpen, + DateClose = DateClose, + Status = Status }; public static Crediting Create(BankYouBancruptDatabase context, CreditingBindingModel model) diff --git a/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs b/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs index 8d95d34..9b10ae7 100644 --- a/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs +++ b/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs @@ -187,11 +187,6 @@ namespace BankYouBankruptRestApi.Controllers { _moneyTransferLogic.Create(moneyTransfer); - _accountLogic.ChangeBalance(new AccountSearchModel - { - Id = moneyTransfer.AccountPayeeId - }, moneyTransfer.Sum); - //если нет отправителя, т. е. операция на перевод денег из нала в виртуал на карту if (moneyTransfer.AccountSenderId.HasValue) { @@ -200,6 +195,11 @@ namespace BankYouBankruptRestApi.Controllers Id = moneyTransfer.AccountSenderId }, moneyTransfer.Sum * -1); } + + _accountLogic.ChangeBalance(new AccountSearchModel + { + Id = moneyTransfer.AccountPayeeId + }, moneyTransfer.Sum); } catch (Exception ex) {