From 395654ee04621e55dddf3e1ce84572dd4f97032e Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 26 May 2024 20:57:20 +0400 Subject: [PATCH] withdrawal logic in storage p.3 --- .../Implements/WithdrawalStorage.cs | 94 +++++++++---------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs index a39ac0e..b8525ff 100644 --- a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs @@ -77,44 +77,6 @@ namespace BankDatabaseImplement.Implements return newWithdrawal.GetViewModel; } - private void MakeWithdrawal(BankDatabase context, WithdrawalBindingModel model) - { - if (model == null) - throw new ArgumentNullException(nameof(model)); - int count = model.Sum; - if (count == 0) - return; - var dictionary = model.WithdrawalAccounts; - var keys = dictionary.Keys; - foreach (int key in keys) - { - (IAccountModel, int) value = new(); - bool result = dictionary.TryGetValue(key, out value); - if (!result) - throw new InvalidOperationException("Value was not got"); - Account account = context.Accounts.FirstOrDefault(x => x.Id == key) ?? - throw new InvalidOperationException("Needed account was not found"); - int dif = count; - if (account.Money < dif) - dif = account.Money; - value.Item2 = dif; - dictionary[key] = value; - count -= dif; - account.Update(new AccountBindingModel - { - Id = account.Id, - Number = account.Number, - ReleaseDate = account.ReleaseDate, - ManagerId = account.ManagerId, - Money = account.Money - dif, - }); - if (count == 0) - break; - } - if (count > 0) - throw new InvalidOperationException("The accounts did not have enough money"); - } - public WithdrawalViewModel? Update(WithdrawalBindingModel model) { using var context = new BankDatabase(); @@ -122,22 +84,20 @@ namespace BankDatabaseImplement.Implements Withdrawal? withdrawal; try { - withdrawal = context.Withdrawals.FirstOrDefault(rec => - rec.Id == model.Id); + withdrawal = context.Withdrawals.FirstOrDefault(x => x.Id == model.Id); if (withdrawal == null) - { - return null; - } - withdrawal.Update(model); - context.SaveChanges(); + throw new InvalidOperationException("Updating withdrawal was not found"); + withdrawal.Update(model); + MakeWithdrawal(context, model); withdrawal.UpdateAccounts(context, model); - transaction.Commit(); } catch { transaction.Rollback(); throw; } + context.SaveChanges(); + transaction.Commit(); return withdrawal.GetViewModel; } @@ -180,6 +140,44 @@ namespace BankDatabaseImplement.Implements return element.GetViewModel; } return null; - } - } + } + + private void MakeWithdrawal(BankDatabase context, WithdrawalBindingModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + int count = model.Sum; + if (count == 0) + return; + var dictionary = model.WithdrawalAccounts; + var keys = dictionary.Keys; + foreach (int key in keys) + { + (IAccountModel, int) value = new(); + bool result = dictionary.TryGetValue(key, out value); + if (!result) + throw new InvalidOperationException("Value was not got"); + Account account = context.Accounts.FirstOrDefault(x => x.Id == key) ?? + throw new InvalidOperationException("Needed account was not found"); + int dif = count; + if (account.Money < dif) + dif = account.Money; + value.Item2 = dif; + dictionary[key] = value; + count -= dif; + account.Update(new AccountBindingModel + { + Id = account.Id, + Number = account.Number, + ReleaseDate = account.ReleaseDate, + ManagerId = account.ManagerId, + Money = account.Money - dif, + }); + if (count == 0) + break; + } + if (count > 0) + throw new InvalidOperationException("The accounts did not have enough money"); + } + } }