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 1a5652b..a12c5a9 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 @@ -7,9 +7,12 @@ import br.com.maida.desafio.bankapi.model.Account; import br.com.maida.desafio.bankapi.model.AccountResponse; +import br.com.maida.desafio.bankapi.model.AccountTransfer; +import br.com.maida.desafio.bankapi.model.AccountTransferResponse; import br.com.maida.desafio.bankapi.model.User; import br.com.maida.desafio.bankapi.service.AccountService; import br.com.maida.desafio.bankapi.service.AuthService; +import java.math.BigDecimal; import javax.validation.Valid; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; @@ -79,4 +82,66 @@ public ResponseEntity addAccount(@Valid @RequestBody Account jsonAccount return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).body(jsonAccountReturn.toString()); } + @PostMapping(path = "/transfer", produces = {"application/json"}) + @ResponseBody + public ResponseEntity transfer(@Valid @RequestBody AccountTransfer jsonAccountTransferString) { + + if (accountService.validateNumberExists(jsonAccountTransferString.getSource_account_number())) { + if (accountService.validateAccountEmail(jsonAccountTransferString.getSource_account_number(), + SecurityContextHolder.getContext().getAuthentication().getName())) { + + if (!accountService.validateNumberExists(jsonAccountTransferString.getDestination_account_number())) { + return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body( + new JSONObject() + .put("erro", "Conta destino não encontrada") + .toString()); + } + + Account sourceAccount = accountService.getAccountByNumber(jsonAccountTransferString.getSource_account_number()); + + if (sourceAccount.getBalance().compareTo(jsonAccountTransferString.getAmount()) == -1) { + return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body( + new JSONObject() + .put("erro", "Saldo insuficiente na conta de origem") + .toString()); + } + + Account destinyAccount = accountService.getAccountByNumber(jsonAccountTransferString.getDestination_account_number()); + + BigDecimal sourceAccountBalance = sourceAccount.getBalance(); + BigDecimal destinyAccountBalance = destinyAccount.getBalance(); + + sourceAccount.setBalance(sourceAccountBalance.subtract(jsonAccountTransferString.getAmount())); + destinyAccount.setBalance(destinyAccountBalance.add(jsonAccountTransferString.getAmount())); + + AccountTransferResponse accountTransferResponse = new AccountTransferResponse( + jsonAccountTransferString.getAmount(), + jsonAccountTransferString.getSource_account_number(), + jsonAccountTransferString.getDestination_account_number(), + sourceAccount.getUserEmail(), + sourceAccount.getUserName()); + + JSONObject jsonAccountTransferReturn = new JSONObject(); + jsonAccountTransferReturn.put("amount", accountTransferResponse.getAmount()); + jsonAccountTransferReturn.put("source_account_number", accountTransferResponse.getSource_account_number()); + jsonAccountTransferReturn.put("destination_account_number", accountTransferResponse.getDestination_account_number()); + jsonAccountTransferReturn.put("user_transfer", accountTransferResponse.getUser_transfer()); + + return ResponseEntity.status(HttpStatus.OK).body(jsonAccountTransferReturn.toString()); + + } else { + return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body( + new JSONObject() + .put("erro", "Conta de origem não encontrada para o usuário informado") + .toString()); + } + } else { + return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body( + new JSONObject() + .put("erro", "Conta de origem não encontrada para o usuário informado") + .toString()); + } + + } + } diff --git a/src/main/java/br/com/maida/desafio/bankapi/model/AccountTransfer.java b/src/main/java/br/com/maida/desafio/bankapi/model/AccountTransfer.java new file mode 100644 index 0000000..3e046bf --- /dev/null +++ b/src/main/java/br/com/maida/desafio/bankapi/model/AccountTransfer.java @@ -0,0 +1,67 @@ +/* + * 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; +import javax.validation.constraints.DecimalMin; +import javax.validation.constraints.Digits; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** + * + * @author Gabriel Duarte + */ +public class AccountTransfer { + + @NotNull(message = "Campo source_account_number ausente.") + @NotEmpty(message = "Campo source_account_number vazio.") + private String source_account_number; + + @NotNull(message = "Campo destination_account_number ausente.") + @NotEmpty(message = "Campo destination_account_number vazio.") + private String destination_account_number; + + @NotNull(message = "Campo amount ausente.") + @DecimalMin(value = "0.0", inclusive = false, message = "Valor deve ser maior que zero.") + @Digits(integer=3, fraction=2) + private BigDecimal amount; + + public AccountTransfer() { + + } + + public AccountTransfer(String source_account_number, String destination_account_number, BigDecimal amount) { + this.source_account_number = source_account_number; + this.destination_account_number = destination_account_number; + this.amount = amount; + } + + public String getSource_account_number() { + return source_account_number; + } + + public void setSource_account_number(String source_account_number) { + this.source_account_number = source_account_number; + } + + public String getDestination_account_number() { + return destination_account_number; + } + + public void setDestination_account_number(String destination_account_number) { + this.destination_account_number = destination_account_number; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + +} diff --git a/src/main/java/br/com/maida/desafio/bankapi/model/AccountTransferResponse.java b/src/main/java/br/com/maida/desafio/bankapi/model/AccountTransferResponse.java new file mode 100644 index 0000000..b8dae20 --- /dev/null +++ b/src/main/java/br/com/maida/desafio/bankapi/model/AccountTransferResponse.java @@ -0,0 +1,51 @@ +/* + * 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; +import org.json.JSONObject; + +/** + * + * @author Gabriel Duarte + */ +public class AccountTransferResponse { + + private BigDecimal amount; + private String source_account_number; + private String destination_account_number; + private JSONObject user_transfer; + + public AccountTransferResponse(BigDecimal amount, String source_account_number, String destination_account_number, String email, String name) { + this.amount = amount; + this.source_account_number = source_account_number; + this.destination_account_number = destination_account_number; + + JSONObject jsonUserTransferAccount = new JSONObject(); + + jsonUserTransferAccount.put("email", email); + jsonUserTransferAccount.put("name", name); + + this.user_transfer = jsonUserTransferAccount; + } + + public BigDecimal getAmount() { + return amount; + } + + public String getSource_account_number() { + return source_account_number; + } + + public String getDestination_account_number() { + return destination_account_number; + } + + public JSONObject getUser_transfer() { + return user_transfer; + } + +}