[R9] Custom method in doctor jpa repository to find one free doctor by specialty

This commit is contained in:
Mirna Gama 2024-01-13 17:28:24 -03:00 committed by Mirna Gama
parent e91b7b4038
commit 22ba9ab9ec

View File

@ -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<Doctor, Long> {
* @return A paginated list with active stored doctors if successful, or null if there is an error
*/
Page<Doctor> 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);
}