From c95baa32e2b46e89f1178df9d7e36cf7aa73ea95 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 26 May 2024 19:35:28 +0400 Subject: [PATCH 1/3] fix withdrawal logic in storage p.1 --- .../Implements/WithdrawalStorage.cs | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs index 575684d..1eec9b5 100644 --- a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs @@ -59,13 +59,54 @@ namespace BankDatabaseImplement.Implements using var context = new BankDatabase(); var newWithdrawal = Withdrawal.Create(context, model); if (newWithdrawal == null) - { return null; + using var transaction = context.Database.BeginTransaction(); + try + { + //MakeWithdrawal(context, model); + context.Withdrawals.Add(newWithdrawal); } - context.Withdrawals.Add(newWithdrawal); + catch + { + transaction.Rollback(); + throw; + } context.SaveChanges(); - return newWithdrawal.GetViewModel; - } + transaction.Commit(); + return newWithdrawal.GetViewModel; + } + + //private bool MakeWithdrawal(BankDatabase context, WithdrawalBindingModel model) + //{ + // if (model == null) + // return false; + // var dictionary = model.WithdrawalAccounts; + // int count = model.Sum; + // foreach (var el in dictionary) + // { + // int dif = count; + // if (el.Count < dif) + // dif = el.Count; + // el.Count -= dif; + // count -= dif; + // if (el.Count == 0) + // { + // dictionary.Add(el); + // } + // if (count == 0) + // break; + // } + // if (count > 0) + // { + // transaction.Rollback(); + // return false; + // } + // foreach (var el in dictionary) + // { + // context.ShopManufactures.Remove(el); + // } + // return true; + //} public WithdrawalViewModel? Update(WithdrawalBindingModel model) { From 85103d634567e1d5863603e99e7f74bd667b0432 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 26 May 2024 20:34:43 +0400 Subject: [PATCH 2/3] private void MakeWithdrawal(BankDatabase context, WithdrawalBindingModel model) --- .../Implements/WithdrawalStorage.cs | 79 ++++++++++--------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs index 1eec9b5..a39ac0e 100644 --- a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs @@ -3,6 +3,7 @@ using BankContracts.SearchModels; using BankContracts.StoragesContracts; using BankContracts.ViewModels; using BankDatabaseImplement.Models; +using BankDataModels.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -57,14 +58,14 @@ namespace BankDatabaseImplement.Implements public WithdrawalViewModel? Insert(WithdrawalBindingModel model) { using var context = new BankDatabase(); - var newWithdrawal = Withdrawal.Create(context, model); - if (newWithdrawal == null) - return null; using var transaction = context.Database.BeginTransaction(); + Withdrawal? newWithdrawal; try { - //MakeWithdrawal(context, model); - context.Withdrawals.Add(newWithdrawal); + newWithdrawal = Withdrawal.Create(context, model); + if (newWithdrawal == null) + throw new InvalidOperationException("Error during creating new withdrawal"); + context.Withdrawals.Add(newWithdrawal); } catch { @@ -76,37 +77,43 @@ namespace BankDatabaseImplement.Implements return newWithdrawal.GetViewModel; } - //private bool MakeWithdrawal(BankDatabase context, WithdrawalBindingModel model) - //{ - // if (model == null) - // return false; - // var dictionary = model.WithdrawalAccounts; - // int count = model.Sum; - // foreach (var el in dictionary) - // { - // int dif = count; - // if (el.Count < dif) - // dif = el.Count; - // el.Count -= dif; - // count -= dif; - // if (el.Count == 0) - // { - // dictionary.Add(el); - // } - // if (count == 0) - // break; - // } - // if (count > 0) - // { - // transaction.Rollback(); - // return false; - // } - // foreach (var el in dictionary) - // { - // context.ShopManufactures.Remove(el); - // } - // return true; - //} + 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) { From 080aeb4058003f2c2746e4e251303cb6797c130b Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 26 May 2024 20:48:53 +0400 Subject: [PATCH 3/3] fix transfer filtration --- .../Controllers/HomeController.cs | 10 ++++++---- .../Views/Home/TransferCreate.cshtml | 4 ++-- .../Views/Home/TransferUpdate.cshtml | 4 ++-- Bank/BankRestApi/Controllers/AccountController.cs | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Bank/BankManagersClientApp/Controllers/HomeController.cs b/Bank/BankManagersClientApp/Controllers/HomeController.cs index 46a5af9..d02bd69 100644 --- a/Bank/BankManagersClientApp/Controllers/HomeController.cs +++ b/Bank/BankManagersClientApp/Controllers/HomeController.cs @@ -226,8 +226,9 @@ namespace BankManagersClientApp.Controllers { return Redirect("~/Home/Enter"); } - ViewBag.Accounts = APIClient.GetRequest>($"api/account/getaccountlist?managerid={APIClient.Client.Id}"); - return View(); + ViewBag.SenderAccounts = APIClient.GetRequest>($"api/account/getaccountlist?managerid={APIClient.Client.Id}"); + ViewBag.RecipientAccounts = APIClient.GetRequest>("api/account/getaccountlist"); + return View(); } [HttpPost] @@ -256,8 +257,9 @@ namespace BankManagersClientApp.Controllers return Redirect("~/Home/Enter"); } ViewBag.Transfers = APIClient.GetRequest>($"api/transfer/gettransferlist?managerid={APIClient.Client.Id}"); - ViewBag.Accounts = APIClient.GetRequest>($"api/account/getaccountlist?managerid={APIClient.Client.Id}"); - return View(); + ViewBag.SenderAccounts = APIClient.GetRequest>($"api/account/getaccountlist?managerid={APIClient.Client.Id}"); + ViewBag.RecipientAccounts = APIClient.GetRequest>("api/account/getaccountlist"); + return View(); } [HttpPost] diff --git a/Bank/BankManagersClientApp/Views/Home/TransferCreate.cshtml b/Bank/BankManagersClientApp/Views/Home/TransferCreate.cshtml index 9ccb9b2..78ea932 100644 --- a/Bank/BankManagersClientApp/Views/Home/TransferCreate.cshtml +++ b/Bank/BankManagersClientApp/Views/Home/TransferCreate.cshtml @@ -15,14 +15,14 @@
Номер счета отправителя:
+ asp-items="@(new SelectList(@ViewBag.SenderAccounts, "Id", "Number"))">
Номер счета получателя:
+ asp-items="@(new SelectList(@ViewBag.RecipientAccounts, "Id", "Number"))">
diff --git a/Bank/BankManagersClientApp/Views/Home/TransferUpdate.cshtml b/Bank/BankManagersClientApp/Views/Home/TransferUpdate.cshtml index da60061..d821e64 100644 --- a/Bank/BankManagersClientApp/Views/Home/TransferUpdate.cshtml +++ b/Bank/BankManagersClientApp/Views/Home/TransferUpdate.cshtml @@ -25,13 +25,13 @@
Отправитель:
- +
Получатель:
- +
diff --git a/Bank/BankRestApi/Controllers/AccountController.cs b/Bank/BankRestApi/Controllers/AccountController.cs index a5f8afc..f31362f 100644 --- a/Bank/BankRestApi/Controllers/AccountController.cs +++ b/Bank/BankRestApi/Controllers/AccountController.cs @@ -21,7 +21,7 @@ namespace BankRestApi.Controllers } [HttpGet] - public List? GetAccountList(int managerId) + public List? GetAccountList(int? managerId) { try {