mercredi 16 décembre 2020

How to design Restful service request when there is multiple subclasses?

I have got 2 end points which are

  • http://localhost:8080/account/v1/credit/{accountNumber} (POST -> TransactionDTO)
  • http://localhost:8080/account/v1/debit/{accountNumber} (POST -> TransactionDTO)

And my TransactionDTO:

public class TransactionDTO {

    @NotNull
    private Double amount;

    public TransactionDTO(@NotNull Double amount) {
        super();
        this.amount = amount;
    }
... Getters Setters
}

Basically, when credit is called, the amount of dto is added to the account's balance associated with the given accountNumber, so far no problem.

However, when debit is called, as you can see below, there are multiple withdraw transaction types(and more withdraw transaction types can be added as the app gets bigger) such as BillPaymentTransaction, WithdrawTransaction.

My problem is how should I design the request class(TransactionDTO) so that when it comes to the controller I can understand which type of withdraw transaction came and do the specific action for it.

My controller's methods:

@PostMapping(path = "/credit/{accountNumber}")
public ResponseEntity<TransactionStatus> credit(@PathVariable String accountNumber, @RequestBody TransactionDTO depositTransaction) throws InsufficientBalanceException {
    TransactionStatus response = accountService.credit(accountNumber, depositTransaction);
    return new ResponseEntity<>(response,HttpStatus.OK);
}

@PostMapping(path = "/debit/{accountNumber}")
public ResponseEntity<TransactionStatus> debit(@PathVariable String accountNumber, @RequestBody TransactionDTO withdrawalTransaction) throws InsufficientBalanceException {
    return new ResponseEntity<>(accountService.debit(accountNumber, withdrawalTransaction),HttpStatus.OK);
}

Transaction Architecture

Aucun commentaire:

Enregistrer un commentaire