From c95baa32e2b46e89f1178df9d7e36cf7aa73ea95 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 26 May 2024 19:35:28 +0400 Subject: [PATCH] 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) {