Search in sources :

Example 6 with BankAccount

use of com.redhat.training.bank.model.BankAccount in project AD482-apps by RedHatTraining.

the class AccountResource method withdraw.

@PUT
@Path("{id}/withdraw")
@Transactional
public BankAccount withdraw(@PathParam Long id, WithdrawAmountFromBankAccount withdraw) {
    if (withdraw.id == null) {
        throw new WebApplicationException("Bank Account ID was not set on request.", Response.Status.BAD_REQUEST);
    }
    if (withdraw.amount <= 0) {
        throw new WebApplicationException("Unprocessable amount on request.", Response.Status.BAD_REQUEST);
    }
    BankAccount entity = BankAccount.findById(id);
    if (entity == null) {
        throw new WebApplicationException("Bank Account with id of " + id + " does not exist.", Response.Status.NOT_FOUND);
    }
    if (entity.balance < withdraw.amount) {
        throw new WebApplicationException("Insufficient funds for withdraw.", Response.Status.CONFLICT);
    }
    entity.balance = entity.balance - withdraw.amount;
    return entity;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BankAccount(com.redhat.training.bank.model.BankAccount) WithdrawAmountFromBankAccount(com.redhat.training.bank.command.WithdrawAmountFromBankAccount) DepositAmountInBankAccount(com.redhat.training.bank.command.DepositAmountInBankAccount) NewBankAccount(com.redhat.training.bank.message.NewBankAccount) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 7 with BankAccount

use of com.redhat.training.bank.model.BankAccount in project AD482-apps by RedHatTraining.

the class AccountResource method delete.

@DELETE
@Path("{id}")
@Transactional
public Response delete(@PathParam Long id) {
    BankAccount entity = BankAccount.findById(id);
    if (entity == null) {
        throw new WebApplicationException("Bank Account with id of " + id + " does not exist.", Response.Status.NOT_FOUND);
    }
    entity.delete();
    LOGGER.info("Deleted bank account - ID: " + id);
    return Response.status(Response.Status.NO_CONTENT).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BankAccount(com.redhat.training.bank.model.BankAccount) WithdrawAmountFromBankAccount(com.redhat.training.bank.command.WithdrawAmountFromBankAccount) DepositAmountInBankAccount(com.redhat.training.bank.command.DepositAmountInBankAccount) NewBankAccount(com.redhat.training.bank.message.NewBankAccount) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

BankAccount (com.redhat.training.bank.model.BankAccount)7 DepositAmountInBankAccount (com.redhat.training.bank.command.DepositAmountInBankAccount)5 WithdrawAmountFromBankAccount (com.redhat.training.bank.command.WithdrawAmountFromBankAccount)5 NewBankAccount (com.redhat.training.bank.message.NewBankAccount)4 Transactional (javax.transaction.Transactional)4 Path (javax.ws.rs.Path)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 PUT (javax.ws.rs.PUT)2 Blocking (io.smallrye.common.annotation.Blocking)1 DELETE (javax.ws.rs.DELETE)1 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)1