From fc18bc2e0d43753b51710a85f275dcfe666b1455 Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Duarte Date: Sat, 2 Oct 2021 22:16:07 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20do=20endpoint=20/acco?= =?UTF-8?q?unts/balance.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Desenvolvimento do endpoint /accounts/balance que retorna o valor em conta bancária para o usuário autenticado e suas demais validações. --- .../bankapi/controller/AccountController.java | 29 +++++++++++++++ .../desafio/bankapi/model/AccountBalance.java | 37 +++++++++++++++++++ .../bankapi/model/AccountBalanceResponse.java | 32 ++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/main/java/br/com/maida/desafio/bankapi/model/AccountBalance.java create mode 100644 src/main/java/br/com/maida/desafio/bankapi/model/AccountBalanceResponse.java 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; + } + +}