mesmo tendo feito toda a logica de escolha de médico: @Service public class ScheduleAppointmentsUseCase { @Autowired private AppointmentRepository appointmentRepository;
@Autowired
private DoctorRepository doctorRepository;
@Autowired
private PatientRepository patientRepository;
@Autowired
private List<ValidatorScheduleAppointments> validators;
public AppointmentDetaillingData schedule(AppointmentDto data) {
boolean doctorExists = doctorRepository.existsById(data.doctorId());
boolean patientExists = patientRepository.existsById(data.patientId());
if (!patientExists) {
throw new ValidationException("patient Id not found in our database.");
}
if(data.doctorId() != null && !doctorExists) {
throw new ValidationException("doctor Id not found in our database.");
}
validators.forEach(validator -> validator.validate(data));
var patient = patientRepository.getReferenceById(data.patientId());
var doctor = chooseDoctor(data);
if (doctor == null) {
throw new ValidationException("No doctors are available for the requested date.");
}
var appointment = new Appointment(null, doctor, patient, data.date(), null);
appointmentRepository.save(appointment);
return new AppointmentDetaillingData(appointment);
}
private Doctor chooseDoctor(AppointmentDto data) {
if (data.doctorId() != null) {
var doctor = doctorRepository.getReferenceById(data.doctorId());
return doctor;
}
if (data.specialty() == null) {
throw new ValidationException("Specialty not informed and no doctor was chosen.");
}
return doctorRepository.chooseRandomDoctorAvailableAtTheDate(data.specialty(), data.date());
}
}
ao disparar a requisição: { "patientId": 1, "date": "21/11/2023 20:00" }
retorna o erro: Internal Server Error: The given id must not be null