Beispiel #1
0
        public async Task <Result <decimal> > DepositAsync(LancamentoRequest lancamentoRequest)
        {
            try
            {
                var validation = await ValidateOperation(lancamentoRequest);

                if (!validation.StatusCode.IsSuccessStatusCode())
                {
                    return(validation);
                }

                var targetAccount = await _contaService.GetByIdAsync <ContaResponse>(lancamentoRequest.IdContaDestino);

                var targetAccountRequest = _mapper.Map <ContaResponse, ContaRequest>(targetAccount.Value);
                lancamentoRequest.ContaDestino = targetAccountRequest;

                if (!targetAccount.StatusCode.IsSuccessStatusCode())
                {
                    return(new Result <decimal>(0, targetAccount.StatusCode, targetAccount.Failures));
                }

                var account    = base._mapper.Map <ContaResponse, Conta>(targetAccount.Value);
                var newBalance = await account.IncreaseBalance(lancamentoRequest.Valor);

                if (!newBalance.StatusCode.IsSuccessStatusCode())
                {
                    return(new Result <decimal>(0, newBalance.StatusCode, newBalance.Failures));
                }

                var updateBalance = await _contaService.UpdateBalanceAsync(account.Id, newBalance.Value);

                if (!updateBalance.StatusCode.IsSuccessStatusCode())
                {
                    return(new Result <decimal>(0, updateBalance.StatusCode, updateBalance.Failures));
                }

                return(new Result <decimal>(newBalance.Value, HttpStatusCode.OK, null));
            }
            catch (Exception ex)
            {
                return(new Result <decimal>(0, HttpStatusCode.InternalServerError, Failure.GenerateOneFailure(ex.Message)));
            }
        }