From 12f34ae377f2d17293c7794cd11bfc02b52ebd38 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 26 May 2024 23:49:43 +0400 Subject: [PATCH] add withdrawal-request logic --- .../Implements/WithdrawalStorage.cs | 29 ++++++++++++++----- Bank/BankDatabaseImplement/Models/Request.cs | 4 +-- .../Models/Withdrawal.cs | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs index fc39deb..629304e 100644 --- a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs @@ -126,11 +126,24 @@ namespace BankDatabaseImplement.Implements Withdrawal? withdrawal; try { - withdrawal = context.Withdrawals.FirstOrDefault(x => x.Id == withdrawalId); + withdrawal = context.Withdrawals.Include(x => x.Accounts).FirstOrDefault(x => x.Id == withdrawalId); if (withdrawal == null) throw new InvalidOperationException("Withdrawal was not found"); + var request = context.Requests.FirstOrDefault(x => x.Id == requestId); + if (request == null) + throw new InvalidOperationException("Request was not found"); + WithdrawalBindingModel model = new WithdrawalBindingModel + { + Id = withdrawal.Id, + WithdrawalAccounts = withdrawal.WithdrawalAccounts, + Sum = request.Sum, + }; + MakeWithdrawal(context, model); withdrawal.LinkToRequest(requestId); - context.SaveChanges(); + if (!request.Execute()) + throw new InvalidOperationException("Request was not executed"); + withdrawal.UpdateAccounts(context, model); + context.SaveChanges(); } catch { @@ -144,16 +157,18 @@ namespace BankDatabaseImplement.Implements public WithdrawalViewModel? Delete(WithdrawalBindingModel model) { using var context = new BankDatabase(); - var element = context.Withdrawals + var withdrawal = context.Withdrawals .Include(x => x.Request) .Include(x => x.Accounts) .ThenInclude(x => x.Account) .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Withdrawals.Remove(element); + if (withdrawal != null) + { + if (withdrawal.RequestId != null) + throw new InvalidOperationException("Deletion was rejected: withdrawal was done"); + context.Withdrawals.Remove(withdrawal); context.SaveChanges(); - return element.GetViewModel; + return withdrawal.GetViewModel; } return null; } diff --git a/Bank/BankDatabaseImplement/Models/Request.cs b/Bank/BankDatabaseImplement/Models/Request.cs index 764f083..66743e2 100644 --- a/Bank/BankDatabaseImplement/Models/Request.cs +++ b/Bank/BankDatabaseImplement/Models/Request.cs @@ -57,11 +57,11 @@ namespace BankDatabaseImplement.Models Sum = model.Sum; } - public bool ChangeStatus(RequestStatus status) + public bool Execute() { if(Status == RequestStatus.Принята) { - Status = status; + Status = RequestStatus.Выполнена; return true; } return false; diff --git a/Bank/BankDatabaseImplement/Models/Withdrawal.cs b/Bank/BankDatabaseImplement/Models/Withdrawal.cs index 6afb579..83bb6f9 100644 --- a/Bank/BankDatabaseImplement/Models/Withdrawal.cs +++ b/Bank/BankDatabaseImplement/Models/Withdrawal.cs @@ -88,6 +88,7 @@ namespace BankDatabaseImplement.Models { context.AccountWithdrawals.RemoveRange(WithdrawalAccounts.Where(rec => !model.WithdrawalAccounts.ContainsKey(rec.AccountId))); context.SaveChanges(); + WithdrawalAccounts = context.AccountWithdrawals.Where(rec => rec.WithdrawalId == model.Id).ToList(); foreach (var wa in WithdrawalAccounts) { wa.Sum = model.WithdrawalAccounts[wa.AccountId].Item2;