Olá!
Estou com um problema ao tentar algumas validações. As que verificam se um id de paciente e médico existem estão rodando, mas ao tentar de fato criar uma consulta ao tentar validar se o médico ou paciente estão ativos, ocorre o erro:
2024-07-12T23:53:45.262-03:00 ERROR 39357 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.ClassCastException: class med.voll.api.model.Doctor cannot be cast to class java.lang.Boolean (med.voll.api.model.Doctor is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @7836d93e; java.lang.Boolean is in module java.base of loader 'bootstrap')] with root cause
Pelo que entendi não há um erro na tentativa de conversão de um objeto do tipo Doctor para o tipo Boolean.
Segue código abaixo:
package med.voll.api.validation;
import jakarta.validation.ValidationException;
import med.voll.api.dto.appointment.AppointmentCreateDTO;
import med.voll.api.repository.DoctorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DoctorActiveValidator implements AppointmentValidator {
@Autowired
private DoctorRepository doctorRepository;
public void validate(AppointmentCreateDTO appointmentCreateDTO) {
if (appointmentCreateDTO.doctorId() == null) {
return;
}
var activeDoctor = doctorRepository.findActiveDoctorById(appointmentCreateDTO.doctorId());
if (!activeDoctor) {
throw new ValidationException("Médico inativo");
}
}
}
package med.voll.api.repository;
import med.voll.api.model.Doctor;
import med.voll.api.model.Specialty;
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 java.time.LocalDateTime;
public interface DoctorRepository extends JpaRepository<Doctor, Long> {
Page<Doctor> findByActiveTrue(Pageable page);
@Query("""
SELECT d FROM Doctor d
WHERE d.active = true
AND d.specialty = :specialty
AND
d.id NOT IN (
SELECT a.doctor.id FROM Appointment a
WHERE a.appointmentDate = :appointmentDate
)
ORDER BY RAND()
LIMIT 1
""")
Doctor getRandomDoctorBySpecialty(Specialty specialty, LocalDateTime appointmentDate);
@Query("""
SELECT d FROM Doctor d
WHERE d.active = true
AND d.id = :id
""")
Boolean findActiveDoctorById(Long id);
}