Skip to content

Commit

Permalink
Implementação do endpoint /accounts/transfer.
Browse files Browse the repository at this point in the history
Desenvolvimento do endpoint /accounts/transfer para realização da função de transferência de dinheiro entre contas e suas validações.
  • Loading branch information
GabrielFDuarte authored Oct 3, 2021
1 parent a04d0a7 commit e8828a3
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,4 +82,66 @@ public ResponseEntity<Object> addAccount(@Valid @RequestBody Account jsonAccount
return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).body(jsonAccountReturn.toString());
}

@PostMapping(path = "/transfer", produces = {"application/json"})
@ResponseBody
public ResponseEntity<Object> 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());
}

}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit e8828a3

Please sign in to comment.