public async Task <AddAccountResponse> SaveAsync(AddAccountResource resource) { var account = _mapper.Map <AddAccountResource, Account>(resource); try { if (resource.InitialCredit.HasValue && resource.InitialCredit.GetValueOrDefault() > 0) { account.Transactions = new List <Transaction>(); // Initial credit provided, creating a transaction account.Transactions.Add( new Transaction { Amount = resource.InitialCredit.GetValueOrDefault(), Description = "Initial credit topup", Timestamp = DateTime.UtcNow }); } await _accountRespository.AddAsync(account); await _unitOfWork.CompleteAsync(); return(new AddAccountResponse(_mapper.Map <Account, AccountResource>(account))); } catch (Exception ex) { return(new AddAccountResponse($"An error occurred when saving the account: {ex.Message}")); } }
public async Task <IActionResult> PostAsync([FromBody] AddAccountResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var result = await _accountService.SaveAsync(resource); if (!result.Success) { return(BadRequest(result.Message)); } return(Ok(result.Account)); }