Class TransactionService

java.lang.Object
com.paymybuddy.paymybuddy.service.TransactionService

@Service public class TransactionService extends Object
The `TransactionService` class provides services for creating and managing transactions between users, including validating transaction details, checking balances, and updating user accounts. It uses the `TransactionRepository` and `UserRepository` for persistence and a `BalanceService` for updating the balances of users involved in a transaction.
  • Constructor Details

    • TransactionService

      public TransactionService()
  • Method Details

    • createTransaction

      @Transactional public Transaction createTransaction(TransactionRequestDTO transactionDTO)
      Creates a new transaction based on the information provided in the TransactionRequestDTO. This method performs validations, checks if the sender has enough funds, updates their balances, and saves the transaction.
      Parameters:
      transactionDTO - The transaction details, including sender and receiver emails, amount, and description.
      Returns:
      The created Transaction object after being saved to the database.
      Throws:
      NotFoundException - If the sender or receiver user is not found.
      NotEnoughMoneyException - If the sender does not have enough money for the transaction.
      SelfSendException - If the sender tries to send money to themselves.
      NegativeTransactionException - If the transaction amount is zero or negative.
    • addToBalance

      public void addToBalance(BalanceOperationDTO operation)
      Adds the specified amount to the user's balance.
      Parameters:
      operation - The balance operation containing the user's email and the amount to be added.
    • subtractFromBalance

      public void subtractFromBalance(BalanceOperationDTO operation)
      Subtracts the specified amount from the user's balance.
      Parameters:
      operation - The balance operation containing the user's email and the amount to be subtracted.
    • validateEnoughMoney

      public void validateEnoughMoney(BalanceOperationDTO operation)
      Validates whether the user has enough money for a transaction.
      Parameters:
      operation - The balance operation containing the user's email and the transaction amount.
      Throws:
      NotEnoughMoneyException - If the user does not have enough money to complete the transaction.
    • validateNotSelfSend

      public void validateNotSelfSend(TransactionRequestDTO transactionDTO)
      Validates that the sender is not trying to send money to himself.
      Parameters:
      transactionDTO - The transaction details, including the sender and receiver emails.
      Throws:
      SelfSendException - If the sender and receiver emails are the same.
    • validatePositiveTransaction

      public void validatePositiveTransaction(TransactionRequestDTO transactionDTO)
      Validates that the transaction amount is positive (greater than zero).
      Parameters:
      transactionDTO - The transaction details, including the amount to be validated.
      Throws:
      NegativeTransactionException - If the transaction amount is zero or negative.