Class BookingService
java.lang.Object
com.equipmentrental.equipment_rental_system.service.BookingService
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:
- BR-01: equipment in MAINTENANCE or UNAVAILABLE status cannot be booked
(delegated to
EquipmentService.verifyAvailableForBooking(Equipment)) - BR-02: overlapping bookings for the same equipment are rejected
(via
BookingRepository.findConflictingBookings(Long, LocalDate, LocalDate)) - BR-03: every booking must reference a registered user
- BR-05: end date must be equal to or later than start date
- BR-08: equipment status is updated when bookings are created or cancelled
(delegated to
EquipmentService.updateStatus(Long, EquipmentStatus))
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBookingService(BookingRepository bookingRepository, EquipmentService equipmentService, UserService userService) -
Method Summary
Modifier and TypeMethodDescriptioncancelBooking(Long bookingId) Cancels an existing booking and updates the equipment status if appropriate (BR-08).createBooking(Booking booking) Creates a new booking after validating all business rules.findAll()Returns all bookings in the system.findByEquipmentId(Long equipmentId) Finds all bookings for a specific equipment item.Finds a booking by its ID.findByUserId(Long userId) Finds all bookings created by a specific user.
-
Constructor Details
-
BookingService
public BookingService(BookingRepository bookingRepository, EquipmentService equipmentService, UserService userService)
-
-
Method Details
-
findAll
-
findById
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
-
findByEquipmentId
-
createBooking
Creates a new booking after validating all business rules.Checks are performed in the following order:
- BR-05: end date must be equal to or later than start date
- BR-03: the user must exist in the system
- BR-01: the equipment must not be in MAINTENANCE or UNAVAILABLE status
- 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
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
-