Skip to content

Commit

Permalink
Implementação do endpoint /accounts/balance.
Browse files Browse the repository at this point in the history
Desenvolvimento do endpoint /accounts/balance que retorna o valor em conta bancária para o usuário autenticado e suas demais validações.
  • Loading branch information
GabrielFDuarte authored Oct 3, 2021
1 parent e8828a3 commit fc18bc2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package br.com.maida.desafio.bankapi.controller;

import br.com.maida.desafio.bankapi.model.Account;
import br.com.maida.desafio.bankapi.model.AccountBalance;
import br.com.maida.desafio.bankapi.model.AccountBalanceResponse;
import br.com.maida.desafio.bankapi.model.AccountResponse;
import br.com.maida.desafio.bankapi.model.AccountTransfer;
import br.com.maida.desafio.bankapi.model.AccountTransferResponse;
Expand Down Expand Up @@ -144,4 +146,31 @@ public ResponseEntity<Object> transfer(@Valid @RequestBody AccountTransfer jsonA

}

@PostMapping(path = "/balance", produces = {"application/json"})
@ResponseBody
public ResponseEntity<Object> balance(@Valid @RequestBody AccountBalance jsonAccountBalanceString) {

if (accountService.validateNumberExists(jsonAccountBalanceString.getAccount_number())) {
if (accountService.validateAccountEmail(jsonAccountBalanceString.getAccount_number(),
SecurityContextHolder.getContext().getAuthentication().getName())) {

Account balanceAccount = accountService.getAccountByNumber(jsonAccountBalanceString.getAccount_number());

return ResponseEntity.status(HttpStatus.OK).body(new AccountBalanceResponse(balanceAccount.getNumber(), balanceAccount.getBalance()));

} else {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(
new JSONObject()
.put("erro", "Conta não encontrada para o usuário informado")
.toString());
}
} else {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(
new JSONObject()
.put("erro", "Conta não encontrada para o usuário informado")
.toString());
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.maida.desafio.bankapi.model;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
*
* @author Gabriel Duarte
*/
public class AccountBalance {

@NotNull(message = "Campo account_number ausente.")
@NotEmpty(message = "Campo account_number vazio.")
private String account_number;

public AccountBalance() {

}

public AccountBalance(String account_number) {
this.account_number = account_number;
}

public String getAccount_number() {
return account_number;
}

public void setAccount_number(String account_number) {
this.account_number = account_number;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.maida.desafio.bankapi.model;

import java.math.BigDecimal;

/**
*
* @author Gabriel Duarte
*/
public class AccountBalanceResponse {

private String account_number;
private BigDecimal balance;

public AccountBalanceResponse(String account_number, BigDecimal balance) {
this.account_number = account_number;
this.balance = balance;
}

public String getAccount_number() {
return account_number;
}

public BigDecimal getBalance() {
return balance;
}

}

0 comments on commit fc18bc2

Please sign in to comment.