Compare commits
No commits in common. "0ab06cb865bc2ef84ef3326fab68238be93a250c" and "504469416d04b75b244c36b9527ccfe4cc432158" have entirely different histories.
0ab06cb865
...
504469416d
@ -77,6 +77,44 @@ 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();
|
||||
@ -84,20 +122,22 @@ namespace BankDatabaseImplement.Implements
|
||||
Withdrawal? withdrawal;
|
||||
try
|
||||
{
|
||||
withdrawal = context.Withdrawals.FirstOrDefault(x => x.Id == model.Id);
|
||||
withdrawal = context.Withdrawals.FirstOrDefault(rec =>
|
||||
rec.Id == model.Id);
|
||||
if (withdrawal == null)
|
||||
throw new InvalidOperationException("Updating withdrawal was not found");
|
||||
withdrawal.Update(model);
|
||||
MakeWithdrawal(context, model);
|
||||
{
|
||||
return null;
|
||||
}
|
||||
withdrawal.Update(model);
|
||||
context.SaveChanges();
|
||||
withdrawal.UpdateAccounts(context, model);
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return withdrawal.GetViewModel;
|
||||
}
|
||||
|
||||
@ -140,44 +180,6 @@ 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user