diff --git a/src/main/java/com/mirna/hospitalmanagementapi/domain/repositories/DoctorRepository.java b/src/main/java/com/mirna/hospitalmanagementapi/domain/repositories/DoctorRepository.java index be7f6d4..147bad8 100644 --- a/src/main/java/com/mirna/hospitalmanagementapi/domain/repositories/DoctorRepository.java +++ b/src/main/java/com/mirna/hospitalmanagementapi/domain/repositories/DoctorRepository.java @@ -1,10 +1,14 @@ package com.mirna.hospitalmanagementapi.domain.repositories; +import java.time.LocalDateTime; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import com.mirna.hospitalmanagementapi.domain.entities.Doctor; +import com.mirna.hospitalmanagementapi.domain.enums.Specialty; /** * Repository interface for retrieving and manipulating all Doctor objects using their unique Long identifier. @@ -20,4 +24,23 @@ public interface DoctorRepository extends JpaRepository { * @return A paginated list with active stored doctors if successful, or null if there is an error */ Page findDoctorsByActiveTrue(Pageable pageable); + + /** + * + * @param specialty Desired specialty for doctor search + * @param consultationDate Date to check if the doctor is free + * @return A random free doctor with the defined specialty if successful, or null if it is non-existent + */ + @Query(""" + select d from Doctor d + where d.active = true + and specialty = :specialty + and d.id not in ( + select c.doctor.id from Consultation c + where c.consultationDate = :consultationDate + ) + order by rand() + limit 1 + """) + Doctor findOneFreeDoctorBySpecialty(Specialty specialty, LocalDateTime consultationDate); }