public async Task <IActionResult> Post(ClientOperationDto clientOperation) { try { decimal currentBalance; switch (clientOperation.Type) { case OperationType.Enroll: currentBalance = await FinanceService.EnrollAsync(clientOperation); return(Ok($"Money was succesfully enrolled. Current Balance = {currentBalance}")); case OperationType.Withdraw: currentBalance = await FinanceService.WithdrawAsync(clientOperation); return(Ok($"Money was succesfully withdrawed. Current Balance = {currentBalance}")); default: return(BadRequest("Operation type not recognized")); } } catch (KeyNotFoundException ex) { return(NotFound(ex.Message)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public async Task <decimal> EnrollAsync(ClientOperationDto clientOperation) { var client = await GetClientAsync(clientOperation.Id); if (clientOperation.TransactionAmount <= 0) { throw new ArgumentException("The enrollment amount must be greater than zero"); } client.AccountBalance += clientOperation.TransactionAmount; await DbContext.SaveChangesAsync(); return(client.AccountBalance); }
public async Task <decimal> WithdrawAsync(ClientOperationDto clientOperation) { var client = await GetClientAsync(clientOperation.Id); if (clientOperation.TransactionAmount <= 0) { throw new ArgumentException("The withdrawal amount must be greater than zero"); } if (clientOperation.TransactionAmount > client.AccountBalance) { throw new ArgumentException("Not enough money in the account"); } client.AccountBalance -= clientOperation.TransactionAmount; await DbContext.SaveChangesAsync(); return(client.AccountBalance); }