diff --git a/src/main/java/br/com/maida/desafio/bankapi/controller/AccountController.java b/src/main/java/br/com/maida/desafio/bankapi/controller/AccountController.java index a12c5a9..4380145 100644 --- a/src/main/java/br/com/maida/desafio/bankapi/controller/AccountController.java +++ b/src/main/java/br/com/maida/desafio/bankapi/controller/AccountController.java @@ -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; @@ -144,4 +146,31 @@ public ResponseEntity transfer(@Valid @RequestBody AccountTransfer jsonA } + @PostMapping(path = "/balance", produces = {"application/json"}) + @ResponseBody + public ResponseEntity 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()); + } + + } + } diff --git a/src/main/java/br/com/maida/desafio/bankapi/model/AccountBalance.java b/src/main/java/br/com/maida/desafio/bankapi/model/AccountBalance.java new file mode 100644 index 0000000..dacb7f4 --- /dev/null +++ b/src/main/java/br/com/maida/desafio/bankapi/model/AccountBalance.java @@ -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; + } + +} diff --git a/src/main/java/br/com/maida/desafio/bankapi/model/AccountBalanceResponse.java b/src/main/java/br/com/maida/desafio/bankapi/model/AccountBalanceResponse.java new file mode 100644 index 0000000..be58f99 --- /dev/null +++ b/src/main/java/br/com/maida/desafio/bankapi/model/AccountBalanceResponse.java @@ -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; + } + +}