diff --git a/backend/src/main/java/com/petshop/backend/service/AppointmentService.java b/backend/src/main/java/com/petshop/backend/service/AppointmentService.java index 9abcf831..ef5f9cdb 100644 --- a/backend/src/main/java/com/petshop/backend/service/AppointmentService.java +++ b/backend/src/main/java/com/petshop/backend/service/AppointmentService.java @@ -169,6 +169,7 @@ public class AppointmentService { } @Transactional(readOnly = true) + public List checkAvailability(Long storeId, Long serviceId, LocalDate date) { storeRepository.findById(storeId) .orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + storeId)); @@ -176,7 +177,16 @@ public class AppointmentService { com.petshop.backend.entity.Service service = serviceRepository.findById(serviceId) .orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + serviceId)); - List existingAppointments = appointmentRepository.findByStoreAndDate(storeId, date); + + //-------------------------------------------------------------------------- + // CHANGED: filter by serviceId too + List existingAppointments = appointmentRepository + .findByStoreAndDate(storeId, date) + .stream() + .filter(a -> a.getService().getServiceId().equals(serviceId)) + .collect(Collectors.toList()); + // ------------------------------------------------------- + List availableSlots = new ArrayList<>(); LocalTime startTime = LocalTime.of(9, 0); @@ -240,13 +250,21 @@ public class AppointmentService { ); } + //------------------------------------ private void validateAvailability(StoreLocation store, com.petshop.backend.entity.Service service, LocalDate date, LocalTime time, Long appointmentIdToIgnore) { - List existingAppointments = appointmentRepository.findByStoreAndDate(store.getStoreId(), date); + // Filter by same service only - different services can run at same time + List existingAppointments = appointmentRepository + .findByStoreAndDate(store.getStoreId(), date) + .stream() + .filter(a -> a.getService().getServiceId().equals(service.getServiceId())) + .collect(Collectors.toList()); if (!isSlotAvailable(existingAppointments, service, time, appointmentIdToIgnore)) { throw new IllegalArgumentException("Appointment time is not available for the selected store and service"); } } + //------------------------------------------------ + private boolean isSlotAvailable(List existingAppointments, com.petshop.backend.entity.Service requestedService, LocalTime requestedStart, Long appointmentIdToIgnore) { LocalTime requestedEnd = requestedStart.plusMinutes(requestedService.getServiceDuration()); for (Appointment existingAppointment : existingAppointments) {