public async Task <LocalTransfer> Create(LocalTransfer localTransfer) { _context.LocalTransfers.Add(localTransfer); await _context.SaveChangesAsync(); return(localTransfer); }
public async Task <ActionResult <LocalTransfer> > PostLocalTransfer(LocalTransfer localTransfer, [FromServices] AccountService _accountService) { try { var account = await _accountService.Get(localTransfer.AccountId); var receiverAccount = await _accountService.Get(localTransfer.ReceiverAccountId); if (account == null) { log.Warn($"Account {localTransfer.AccountId} not found."); return(BadRequest("Account not found.")); // use proper error obj } if (receiverAccount == null) { log.Warn($"Receiver Account {localTransfer.AccountId} not found."); return(BadRequest("Receiver Account not found.")); // use proper error obj } account.UpdateBalance(localTransfer.Amount * -1); await _accountService.Update(account.Id, account); var accountNewBalance = account.Balance; receiverAccount.UpdateBalance(localTransfer.Amount); await _accountService.Update(receiverAccount.Id, receiverAccount); var receiverAccountNewBalance = receiverAccount.Balance; LocalTransfer createdLocalTransfer = await _transactionService.Create(localTransfer); log.Info($"Local Transfer {createdLocalTransfer.Id} has been created."); return(createdLocalTransfer); } catch (System.Exception e) { log.Error(e); return(BadRequest(new { error = new { message = e.Message } })); } }