public async Task <IActionResult> Post(DebtHistoryDto debtHistoryDto)
        {
            try{
                DebtHistory debtHistoryModel = _mapper.Map <DebtHistory>(debtHistoryDto);
                if (debtHistoryModel != null)
                {
                    Account account = await _repo.GetAccount(null, debtHistoryModel.AccountId);

                    if (account != null)
                    {
                        debtHistoryModel.BalanceAccountHistory = account.Balance;
                        if (account.Balance < debtHistoryModel.Value)
                        {
                            return(BadRequest("Não autorizado. Limite Disponível  R$" + account.Balance));
                        }
                        account.Balance -= debtHistoryModel.Value;
                        _repo.Update(account);
                    }
                    else
                    {
                        return(NotFound("Conta cliente não encontrada"));
                    }
                }
                debtHistoryModel.CreateDate = DateTime.Now;
                _repo.Add(debtHistoryModel);

                if (await _repo.SaveChangesAsync())
                {
                    // mapper reverse
                    return(Created($"/api/debthistory/{debtHistoryDto.Id}", debtHistoryModel));
                }
            }
            catch (System.Exception ex)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Banco dados falhou {ex.Message}"));
            }

            return(BadRequest());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Get(int?customerId, int?accountId)
        {
            try{
                Account AccountModel = await _repo.GetAccount(customerId, accountId);

                // match date to dto
                AccountDto result = _mapper.Map <AccountDto>(AccountModel);
                return(Ok(result));
            }
            catch (System.Exception ex)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Banco dados falhou {ex.Message}"));
            }
        }