withdrawal logic in storage p.3

This commit is contained in:
Zakharov_Rostislav 2024-05-26 20:57:20 +04:00
parent 080aeb4058
commit 395654ee04

View File

@ -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");
}
}
}