From 22ba9ab9ec657431967b396b471a7f6c8dde5693 Mon Sep 17 00:00:00 2001 From: Mirna Gama Date: Sat, 13 Jan 2024 17:28:24 -0300 Subject: [PATCH] [R9] Custom method in doctor jpa repository to find one free doctor by specialty --- .../domain/repositories/DoctorRepository.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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); }