Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

error quando nao passa o id do medico

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

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

2 respostas

2023-08-29T13:25:10.594-03:00 WARN 3532 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null]

solução!

Oi!

O problema está na primeira linha do método:

boolean doctorExists = doctorRepository.existsById(data.doctorId());

Não pode chamar o método existsById passando null. Por isso que no curso foi feito assim:

if (dados.idMedico() != null && !medicoRepository.existsById(dados.idMedico())) {
    throw new ValidacaoException("Id do médico informado não existe!");
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software