add withdrawal-request logic

This commit is contained in:
Zakharov_Rostislav 2024-05-26 23:49:43 +04:00
parent 46a7483dc7
commit 12f34ae377
3 changed files with 25 additions and 9 deletions

View File

@ -126,11 +126,24 @@ namespace BankDatabaseImplement.Implements
Withdrawal? withdrawal;
try
{
withdrawal = context.Withdrawals.FirstOrDefault(x => x.Id == withdrawalId);
withdrawal = context.Withdrawals.Include(x => x.Accounts).FirstOrDefault(x => x.Id == withdrawalId);
if (withdrawal == null)
throw new InvalidOperationException("Withdrawal was not found");
var request = context.Requests.FirstOrDefault(x => x.Id == requestId);
if (request == null)
throw new InvalidOperationException("Request was not found");
WithdrawalBindingModel model = new WithdrawalBindingModel
{
Id = withdrawal.Id,
WithdrawalAccounts = withdrawal.WithdrawalAccounts,
Sum = request.Sum,
};
MakeWithdrawal(context, model);
withdrawal.LinkToRequest(requestId);
context.SaveChanges();
if (!request.Execute())
throw new InvalidOperationException("Request was not executed");
withdrawal.UpdateAccounts(context, model);
context.SaveChanges();
}
catch
{
@ -144,16 +157,18 @@ namespace BankDatabaseImplement.Implements
public WithdrawalViewModel? Delete(WithdrawalBindingModel model)
{
using var context = new BankDatabase();
var element = context.Withdrawals
var withdrawal = context.Withdrawals
.Include(x => x.Request)
.Include(x => x.Accounts)
.ThenInclude(x => x.Account)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Withdrawals.Remove(element);
if (withdrawal != null)
{
if (withdrawal.RequestId != null)
throw new InvalidOperationException("Deletion was rejected: withdrawal was done");
context.Withdrawals.Remove(withdrawal);
context.SaveChanges();
return element.GetViewModel;
return withdrawal.GetViewModel;
}
return null;
}

View File

@ -57,11 +57,11 @@ namespace BankDatabaseImplement.Models
Sum = model.Sum;
}
public bool ChangeStatus(RequestStatus status)
public bool Execute()
{
if(Status == RequestStatus.Принята)
{
Status = status;
Status = RequestStatus.Выполнена;
return true;
}
return false;

View File

@ -88,6 +88,7 @@ namespace BankDatabaseImplement.Models
{
context.AccountWithdrawals.RemoveRange(WithdrawalAccounts.Where(rec => !model.WithdrawalAccounts.ContainsKey(rec.AccountId)));
context.SaveChanges();
WithdrawalAccounts = context.AccountWithdrawals.Where(rec => rec.WithdrawalId == model.Id).ToList();
foreach (var wa in WithdrawalAccounts)
{
wa.Sum = model.WithdrawalAccounts[wa.AccountId].Item2;