Backend a small change
small change in appointment service to validate time for appointments.
This commit is contained in:
@@ -169,6 +169,7 @@ public class AppointmentService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
|
||||
public List<String> 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<Appointment> existingAppointments = appointmentRepository.findByStoreAndDate(storeId, date);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// CHANGED: filter by serviceId too
|
||||
List<Appointment> existingAppointments = appointmentRepository
|
||||
.findByStoreAndDate(storeId, date)
|
||||
.stream()
|
||||
.filter(a -> a.getService().getServiceId().equals(serviceId))
|
||||
.collect(Collectors.toList());
|
||||
// -------------------------------------------------------
|
||||
|
||||
|
||||
List<String> 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<Appointment> existingAppointments = appointmentRepository.findByStoreAndDate(store.getStoreId(), date);
|
||||
// Filter by same service only - different services can run at same time
|
||||
List<Appointment> 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<Appointment> existingAppointments, com.petshop.backend.entity.Service requestedService, LocalTime requestedStart, Long appointmentIdToIgnore) {
|
||||
LocalTime requestedEnd = requestedStart.plusMinutes(requestedService.getServiceDuration());
|
||||
for (Appointment existingAppointment : existingAppointments) {
|
||||
|
||||
Reference in New Issue
Block a user