Search in sources :

Example 1 with BankAccount

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

the class AccountResource method deposit.

@PUT
@Path("{id}/deposit")
@Transactional
public BankAccount deposit(@PathParam Long id, DepositAmountInBankAccount deposit) {
    if (deposit.id == null) {
        throw new WebApplicationException("Bank Account ID was not set on request.", Response.Status.BAD_REQUEST);
    }
    if (deposit.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);
    }
    entity.balance = entity.balance + deposit.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 2 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 3 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)

Example 4 with BankAccount

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

the class NewBankAccountConsumer method processMessage.

// TODO: Create the consumer implementation
@Incoming("new-bank-account-in")
@Blocking
@Transactional
public void processMessage(NewBankAccount message) {
    BankAccount entity = BankAccount.findById(message.getId());
    if (entity != null) {
        entity.profile = message.getBalance() < 100000 ? "regular" : "premium";
        LOGGER.info("Updated Bank Account - ID: " + message.getId() + " - Type: " + entity.profile);
    } else {
        LOGGER.info("Bank Account not found!");
    }
}
Also used : BankAccount(com.redhat.training.bank.model.BankAccount) NewBankAccount(com.redhat.training.bank.message.NewBankAccount) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) Blocking(io.smallrye.common.annotation.Blocking) Transactional(javax.transaction.Transactional)

Example 5 with BankAccount

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

the class AccountResource method depositAmountInBankAccount.

@Transactional
public BankAccount depositAmountInBankAccount(Long bankAccountId, DepositAmountInBankAccount deposit) {
    if (deposit.bankAccountId == null || !deposit.bankAccountId.equals(bankAccountId)) {
        throw new WebApplicationException("Bank Account ID was not set on request.", Response.Status.BAD_REQUEST);
    }
    if (deposit.amount == null || deposit.amount <= 0) {
        throw new WebApplicationException("Unprocessable amount on request.", Response.Status.BAD_REQUEST);
    }
    BankAccount entity = BankAccount.findById(bankAccountId);
    if (entity == null) {
        throw new WebApplicationException("Bank Account with id of " + bankAccountId + " does not exist.", Response.Status.NOT_FOUND);
    }
    entity.balance = entity.balance + deposit.amount;
    return entity;
}
Also used : BankAccount(com.redhat.training.bank.model.BankAccount) WithdrawAmountFromBankAccount(com.redhat.training.bank.command.WithdrawAmountFromBankAccount) DepositAmountInBankAccount(com.redhat.training.bank.command.DepositAmountInBankAccount) Transactional(javax.transaction.Transactional)

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