public async Task Charge(ChargeGiftcardCommand command, CancellationToken cancellationToken = default) { try { await _accountsCommandHandler.ExecuteAsync(command, cancellationToken); } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> ChargeGiftcard([FromBody] ChargeGiftcardRequest request, CancellationToken cancellationToken = default) { ChargeGiftcardResponse response = new ChargeGiftcardResponse(); try { var command = new ChargeGiftcardCommand(request.Company, request.CPF, request.AccountId, request.Value) { Location = request.Location }; await _accountsAppService.Charge(command, cancellationToken); return(NoContent()); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task ExecuteAsync(ChargeGiftcardCommand command, CancellationToken cancellationToken = default) { try { // validate command ValidateCommand(command); // get only giftcards var accounts = await _accountRepository.GetGiftcards(ac => ac.Company == command.Company && ac.CPF == command.CPF && ac.AccountId == command.AccountId); if (!accounts.Any()) { throw new InvalidOperationException("invalid account."); } // get the selected account var account = accounts.Single(); // validate account for change ValidateAccountForChange(account); // charge giftcard account.Charge(command.Value, command.Location); // update selected account _accountRepository.Update(account); await _accountRepository.SaveChangesAsync(cancellationToken); // add the account transaction into the blockchain await AddBlockToBlockchain(account); } catch (Exception ex) { throw ex; } }