[R9] Consultation service with add consultation method
This commit is contained in:
parent
ffabff23c1
commit
8dbc0f4f01
@ -0,0 +1,97 @@
|
||||
package com.mirna.hospitalmanagementapi.application.services;
|
||||
|
||||
import org.apache.coyote.BadRequestException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.mirna.hospitalmanagementapi.application.usecase.consultation.FindConsultationByDoctorAndDateUseCase;
|
||||
import com.mirna.hospitalmanagementapi.application.usecase.consultation.FindConsultationByPatientAndDateUseCase;
|
||||
import com.mirna.hospitalmanagementapi.application.usecase.consultation.SaveConsultationUseCase;
|
||||
import com.mirna.hospitalmanagementapi.application.usecase.doctor.FindDoctorByIdUseCase;
|
||||
import com.mirna.hospitalmanagementapi.application.usecase.doctor.FindOneFreeDoctorBySpecialtyUseCase;
|
||||
import com.mirna.hospitalmanagementapi.application.usecase.patient.FindPatientByIdUseCase;
|
||||
import com.mirna.hospitalmanagementapi.domain.dtos.consultation.ConsultationDTO;
|
||||
import com.mirna.hospitalmanagementapi.domain.entities.Consultation;
|
||||
import com.mirna.hospitalmanagementapi.domain.entities.Doctor;
|
||||
import com.mirna.hospitalmanagementapi.domain.entities.Patient;
|
||||
import com.mirna.hospitalmanagementapi.domain.exceptions.ConsultationValidationException;
|
||||
import com.mirna.hospitalmanagementapi.domain.services.ConsultationService;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
|
||||
/**
|
||||
* This class is an implementation of the ConsultationService interface.
|
||||
*
|
||||
* This class provides methods to perform operations on consultations
|
||||
*
|
||||
* @author Mirna Gama
|
||||
* @version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class ConsultationServiceImpl implements ConsultationService {
|
||||
|
||||
@Autowired
|
||||
private SaveConsultationUseCase saveConsultation;
|
||||
|
||||
@Autowired
|
||||
private FindConsultationByDoctorAndDateUseCase findConsultationByDoctorAndDate;
|
||||
|
||||
@Autowired
|
||||
private FindConsultationByPatientAndDateUseCase findConsultationByPatientAndDate;
|
||||
|
||||
@Autowired
|
||||
private FindPatientByIdUseCase findPatientById;
|
||||
|
||||
@Autowired
|
||||
private FindDoctorByIdUseCase findDoctorById;
|
||||
|
||||
@Autowired
|
||||
private FindOneFreeDoctorBySpecialtyUseCase findOneFreeDoctorBySpecialty;
|
||||
|
||||
/**
|
||||
* Adds a new consultation to the repository.
|
||||
*
|
||||
* @param consultationDTO A data transfer object representing a consultation to add.
|
||||
* @return The saved consultation if successful, or throws an exception if there is an error.
|
||||
* @throws ConsultationValidationException if there is a validation error
|
||||
*/
|
||||
@Override
|
||||
public Consultation addConsultation(ConsultationDTO consultationDTO) throws ConsultationValidationException {
|
||||
|
||||
Patient patient = findPatientById.execute(consultationDTO.patientId());
|
||||
|
||||
if (!patient.getActive())
|
||||
throw new ConsultationValidationException("This patient is not active");
|
||||
|
||||
if (findConsultationByPatientAndDate.execute(patient.getId(), consultationDTO.consultationDate()) != null)
|
||||
throw new ConsultationValidationException("This patient is not free on this date");
|
||||
|
||||
Doctor doctor = null;
|
||||
|
||||
if (consultationDTO.doctorId() != null) {
|
||||
|
||||
doctor = findDoctorById.execute(consultationDTO.doctorId());
|
||||
|
||||
if (!doctor.getActive())
|
||||
throw new ConsultationValidationException("This doctor is not active");
|
||||
|
||||
if (findConsultationByDoctorAndDate.execute(doctor.getId(), consultationDTO.consultationDate()) != null)
|
||||
throw new ConsultationValidationException("This doctor is not free on this date");
|
||||
|
||||
} else if (consultationDTO.specialty() != null) {
|
||||
|
||||
doctor = findOneFreeDoctorBySpecialty.execute(consultationDTO.specialty(),
|
||||
consultationDTO.consultationDate());
|
||||
|
||||
if (doctor == null) throw new EntityNotFoundException("There is no free doctor for this date with this specialty");
|
||||
|
||||
} else {
|
||||
throw new ConsultationValidationException("At least the specialty or doctor ID must be filled in");
|
||||
}
|
||||
|
||||
Consultation consultation = new Consultation(patient, doctor, consultationDTO.consultationDate());
|
||||
|
||||
return saveConsultation.execute(consultation);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.mirna.hospitalmanagementapi.domain.services;
|
||||
|
||||
import com.mirna.hospitalmanagementapi.domain.dtos.consultation.ConsultationDTO;
|
||||
import com.mirna.hospitalmanagementapi.domain.entities.Consultation;
|
||||
import com.mirna.hospitalmanagementapi.domain.exceptions.ConsultationValidationException;
|
||||
|
||||
public interface ConsultationService {
|
||||
|
||||
/**
|
||||
* Adds a new consultation to the repository.
|
||||
*
|
||||
* @param consultationDTO A data transfer object representing a consultation to add.
|
||||
* @return The saved consultation if successful, or throws an exception if there is an error.
|
||||
* @throws ConsultationValidationException if there is a validation error
|
||||
*/
|
||||
public Consultation addConsultation(ConsultationDTO consultationDTO) throws ConsultationValidationException;
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user