Class BookingService

java.lang.Object
com.equipmentrental.equipment_rental_system.service.BookingService

@Service @Transactional(readOnly=true) public class BookingService extends Object
Service layer for managing Booking entities. This is the central service in the application, orchestrating all booking-related business rules by coordinating with EquipmentService and UserService.

Business rules enforced:

See Also:
  • Constructor Details

  • Method Details

    • findAll

      public List<Booking> findAll()
      Returns all bookings in the system.
      Returns:
      list of all bookings
    • findById

      public Booking findById(Long bookingId)
      Finds a booking by its ID.
      Parameters:
      bookingId - the ID of the booking to find
      Returns:
      the booking
      Throws:
      ResourceNotFoundException - if no booking exists with the given ID
    • findByUserId

      public List<Booking> findByUserId(Long userId)
      Finds all bookings created by a specific user.
      Parameters:
      userId - the ID of the user
      Returns:
      list of bookings for the given user
    • findByEquipmentId

      public List<Booking> findByEquipmentId(Long equipmentId)
      Finds all bookings for a specific equipment item.
      Parameters:
      equipmentId - the ID of the equipment
      Returns:
      list of bookings for the given equipment
    • createBooking

      @Transactional public Booking createBooking(Booking booking)
      Creates a new booking after validating all business rules.

      Checks are performed in the following order:

      1. BR-05: end date must be equal to or later than start date
      2. BR-03: the user must exist in the system
      3. BR-01: the equipment must not be in MAINTENANCE or UNAVAILABLE status
      4. BR-02: no overlapping confirmed bookings may exist for the same equipment

      If all checks pass, the booking is saved with CONFIRMED status and the equipment status is updated to BOOKED (BR-08).

      Parameters:
      booking - the booking to create (must have equipment and user set)
      Returns:
      the saved booking with generated ID and CONFIRMED status
    • cancelBooking

      @Transactional public Booking cancelBooking(Long bookingId)
      Cancels an existing booking and updates the equipment status if appropriate (BR-08).

      After setting the booking status to CANCELLED, this method checks whether any other active (confirmed) bookings remain for the same equipment item. If none remain, the equipment status is reverted to AVAILABLE.

      Parameters:
      bookingId - the ID of the booking to cancel
      Returns:
      the cancelled booking
      Throws:
      ResourceNotFoundException - if no booking exists with the given ID