private void MakeWithdrawal(BankDatabase context, WithdrawalBindingModel model)
This commit is contained in:
parent
6e9e819b8d
commit
85103d6345
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user