public void Transfer(AccountTransferViewModel accountTransferViewModel) { var createTransferCommand = new CreateTransferCommand( accountTransferViewModel.FromAccount, accountTransferViewModel.ToAccount, accountTransferViewModel.TransferAmount ); _bus.SendCommand(createTransferCommand); }
public async Task <IActionResult> Transfer(AccountTransferViewModel model) { if (!ModelState.IsValid) { return(View(model)); } try { var succeeded = await _accountService.Transfer(model.FromAccountId, int.Parse(model.ToAccountId), model.Amount); if (succeeded) { return(RedirectToAction(nameof(AccountDetails), new { id = model.FromAccountId })); } } catch (AccountNotFoundException e) { ModelState.AddModelError(string.Empty, e.Message); model.Balance = _accountService.GetAccount(model.FromAccountId).Balance; return(View(model)); } catch (InsufficientFundsException e) { ModelState.AddModelError(string.Empty, e.Message); model.Balance = _accountService.GetAccount(model.FromAccountId).Balance; return(View(model)); } catch (MatchingAccountsException e) { ModelState.AddModelError(string.Empty, e.Message); model.Balance = _accountService.GetAccount(model.FromAccountId).Balance; return(View(model)); } catch (NoNegativeAmountException e) { ModelState.AddModelError(string.Empty, e.Message); model.Balance = _accountService.GetAccount(model.FromAccountId).Balance; return(View(model)); } return(View(model)); }
public async Task <IActionResult> Transfer(Guid?id) { ViewBag.TransactionType = "Account Transfer"; if (id == null) { return(NotFound()); } var account = await _context.Accounts.SingleOrDefaultAsync(m => m.Id == id && m.UserId == UserId); if (account == null) { return(NotFound()); } AccountTransferViewModel model = new AccountTransferViewModel() { Id = account.Id, }; return(View(model)); }
public async Task <IActionResult> Transfer(Guid id, [Bind("Id,Amount, RecieverAccountNumber, Remarks")] AccountTransferViewModel account) { if (ModelState.IsValid) { try { var record = _context.Accounts.FirstOrDefault(x => x.Id == id && x.UserId == UserId); if (record == null) { ModelState.AddModelError("", "Depositor account not found."); return(View(account)); } if (record.Balance < account.Amount) { ModelState.AddModelError("Amount", "Amount cannot exceed Balance."); return(View(account)); } var reciever = _context.Accounts.FirstOrDefault(x => x.AccountNumber == account.RecieverAccountNumber); if (reciever == null) { ModelState.AddModelError("RecieverAccountNumber", "Account Number does not exists. Please check the number and try again."); return(View(account)); } if (reciever.AccountNumber == record.AccountNumber) { ModelState.AddModelError("RecieverAccountNumber", "Cant Transfer to same account. Please check the number and try again."); return(View(account)); } else { record.Transfer(account.RecieverAccountNumber, account.Amount, account.Remarks); reciever.ReceiveTransfer(record.AccountNumber, account.Amount, account.Remarks); _context.Update(record); _context.AddRange(record.AccountTransactions); _context.Update(reciever); _context.AddRange(reciever.AccountTransactions); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (_context.Accounts.Find(id).Balance < account.Amount) { ModelState.AddModelError("Amount", "Another transaction completed while trying to complete current transsaction."); return(View(account)); } else { var record = _context.Accounts.FirstOrDefault(x => x.Id == id && x.UserId == UserId); var reciever = _context.Accounts.FirstOrDefault(x => x.AccountNumber == account.RecieverAccountNumber); record.Transfer(account.RecieverAccountNumber, account.Amount, account.Remarks); reciever.ReceiveTransfer(record.AccountNumber, account.Amount, account.Remarks); _context.Update(record); _context.AddRange(record.AccountTransactions); _context.Update(reciever); _context.AddRange(reciever.AccountTransactions); await _context.SaveChangesAsync(); } } return(RedirectToAction(nameof(Index))); } return(View(account)); }
public IActionResult Post([FromBody] AccountTransferViewModel accountTransferViewModel) { _accountService.Transfer(accountTransferViewModel); return(Ok(accountTransferViewModel)); }