simplify appointment to single pet

This commit is contained in:
2026-04-06 20:22:26 -06:00
parent 0570758b07
commit beff4c5297
2 changed files with 12 additions and 37 deletions

View File

@@ -21,7 +21,7 @@ public interface AppointmentRepository extends JpaRepository<Appointment, Long>
@Query("SELECT a FROM Appointment a JOIN FETCH a.service WHERE a.store.storeId = :storeId AND a.appointmentDate = :date AND LOWER(a.appointmentStatus) NOT IN ('cancelled', 'missed')") @Query("SELECT a FROM Appointment a JOIN FETCH a.service WHERE a.store.storeId = :storeId AND a.appointmentDate = :date AND LOWER(a.appointmentStatus) NOT IN ('cancelled', 'missed')")
List<Appointment> findByStoreAndDate(@Param("storeId") Long storeId, @Param("date") LocalDate date); List<Appointment> findByStoreAndDate(@Param("storeId") Long storeId, @Param("date") LocalDate date);
@Query("SELECT DISTINCT a FROM Appointment a LEFT JOIN a.pets p WHERE " + @Query("SELECT a FROM Appointment a LEFT JOIN a.pet p WHERE " +
"LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + "LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + "LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + "LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
@@ -30,7 +30,7 @@ public interface AppointmentRepository extends JpaRepository<Appointment, Long>
Page<Appointment> findByCustomerId(Long customerId, Pageable pageable); Page<Appointment> findByCustomerId(Long customerId, Pageable pageable);
@Query("SELECT DISTINCT a FROM Appointment a LEFT JOIN a.pets p WHERE a.customer.id = :customerId AND (" + @Query("SELECT a FROM Appointment a LEFT JOIN a.pet p WHERE a.customer.id = :customerId AND (" +
"LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + "LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + "LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + "LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +

View File

@@ -24,9 +24,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@@ -94,13 +92,7 @@ public class AppointmentService {
com.petshop.backend.entity.Service service = serviceRepository.findById(request.getServiceId()) com.petshop.backend.entity.Service service = serviceRepository.findById(request.getServiceId())
.orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + request.getServiceId())); .orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + request.getServiceId()));
boolean hasPetIds = request.getPetIds() != null && !request.getPetIds().isEmpty(); Pet pet = request.getPetId() != null ? fetchPet(request.getPetId()) : null;
if (!hasPetIds) {
throw new IllegalArgumentException("Please specify at least one pet.");
}
Set<Pet> pets = fetchPets(request.getPetIds());
User employee = resolveAppointmentEmployee(request.getEmployeeId(), store.getStoreId()); User employee = resolveAppointmentEmployee(request.getEmployeeId(), store.getStoreId());
validateStoreAccess(store.getStoreId(), authenticatedUser); validateStoreAccess(store.getStoreId(), authenticatedUser);
@@ -113,7 +105,7 @@ public class AppointmentService {
appointment.setAppointmentDate(request.getAppointmentDate()); appointment.setAppointmentDate(request.getAppointmentDate());
appointment.setAppointmentTime(request.getAppointmentTime()); appointment.setAppointmentTime(request.getAppointmentTime());
appointment.setAppointmentStatus(request.getAppointmentStatus()); appointment.setAppointmentStatus(request.getAppointmentStatus());
appointment.setPets(pets); appointment.setPet(pet);
appointment.setEmployee(employee); appointment.setEmployee(employee);
appointment = appointmentRepository.save(appointment); appointment = appointmentRepository.save(appointment);
@@ -138,13 +130,7 @@ public class AppointmentService {
com.petshop.backend.entity.Service service = serviceRepository.findById(request.getServiceId()) com.petshop.backend.entity.Service service = serviceRepository.findById(request.getServiceId())
.orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + request.getServiceId())); .orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + request.getServiceId()));
boolean hasPetIds = request.getPetIds() != null && !request.getPetIds().isEmpty(); Pet pet = request.getPetId() != null ? fetchPet(request.getPetId()) : null;
if (!hasPetIds) {
throw new IllegalArgumentException("Please specify at least one pet.");
}
Set<Pet> pets = fetchPets(request.getPetIds());
User employee = resolveAppointmentEmployee(request.getEmployeeId(), store.getStoreId()); User employee = resolveAppointmentEmployee(request.getEmployeeId(), store.getStoreId());
validateStoreAccess(store.getStoreId(), authenticatedUser); validateStoreAccess(store.getStoreId(), authenticatedUser);
@@ -156,7 +142,7 @@ public class AppointmentService {
appointment.setAppointmentDate(request.getAppointmentDate()); appointment.setAppointmentDate(request.getAppointmentDate());
appointment.setAppointmentTime(request.getAppointmentTime()); appointment.setAppointmentTime(request.getAppointmentTime());
appointment.setAppointmentStatus(request.getAppointmentStatus()); appointment.setAppointmentStatus(request.getAppointmentStatus());
appointment.setPets(pets); appointment.setPet(pet);
appointment.setEmployee(employee); appointment.setEmployee(employee);
appointment = appointmentRepository.save(appointment); appointment = appointmentRepository.save(appointment);
@@ -227,24 +213,13 @@ public class AppointmentService {
} }
} }
private Set<Pet> fetchPets(List<Long> petIds) { private Pet fetchPet(Long petId) {
Set<Pet> pets = new HashSet<>(); return petRepository.findById(petId)
for (Long petId : petIds) { .orElseThrow(() -> new ResourceNotFoundException("Pet not found with id: " + petId));
Pet pet = petRepository.findById(petId)
.orElseThrow(() -> new ResourceNotFoundException("Pet not found with id: " + petId));
pets.add(pet);
}
return pets;
} }
private AppointmentResponse mapToResponse(Appointment appointment) { private AppointmentResponse mapToResponse(Appointment appointment) {
List<String> petNames = appointment.getPets().stream() Pet pet = appointment.getPet();
.map(Pet::getPetName)
.collect(Collectors.toList());
List<Long> petIds = appointment.getPets().stream()
.map(Pet::getPetId)
.collect(Collectors.toList());
AppointmentResponse response = new AppointmentResponse(); AppointmentResponse response = new AppointmentResponse();
response.setAppointmentId(appointment.getAppointmentId()); response.setAppointmentId(appointment.getAppointmentId());
@@ -259,8 +234,8 @@ public class AppointmentService {
response.setAppointmentStatus(appointment.getAppointmentStatus()); response.setAppointmentStatus(appointment.getAppointmentStatus());
response.setEmployeeId(appointment.getEmployee().getId()); response.setEmployeeId(appointment.getEmployee().getId());
response.setEmployeeName(appointment.getEmployee().getFirstName() + " " + appointment.getEmployee().getLastName()); response.setEmployeeName(appointment.getEmployee().getFirstName() + " " + appointment.getEmployee().getLastName());
response.setPetNames(petNames); response.setPetName(pet != null ? pet.getPetName() : null);
response.setPetIds(petIds); response.setPetId(pet != null ? pet.getPetId() : null);
response.setCreatedAt(appointment.getCreatedAt()); response.setCreatedAt(appointment.getCreatedAt());
response.setUpdatedAt(appointment.getUpdatedAt()); response.setUpdatedAt(appointment.getUpdatedAt());