From 8dbc0f4f0136ec6a96336ed7d2829f027a8437fb Mon Sep 17 00:00:00 2001 From: Mirna Gama Date: Sat, 13 Jan 2024 17:31:57 -0300 Subject: [PATCH] [R9] Consultation service with add consultation method --- .../services/ConsultationServiceImpl.java | 97 +++++++++++++++++++ .../domain/services/ConsultationService.java | 18 ++++ 2 files changed, 115 insertions(+) create mode 100644 src/main/java/com/mirna/hospitalmanagementapi/application/services/ConsultationServiceImpl.java create mode 100644 src/main/java/com/mirna/hospitalmanagementapi/domain/services/ConsultationService.java diff --git a/src/main/java/com/mirna/hospitalmanagementapi/application/services/ConsultationServiceImpl.java b/src/main/java/com/mirna/hospitalmanagementapi/application/services/ConsultationServiceImpl.java new file mode 100644 index 0000000..4987273 --- /dev/null +++ b/src/main/java/com/mirna/hospitalmanagementapi/application/services/ConsultationServiceImpl.java @@ -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); + } + +} diff --git a/src/main/java/com/mirna/hospitalmanagementapi/domain/services/ConsultationService.java b/src/main/java/com/mirna/hospitalmanagementapi/domain/services/ConsultationService.java new file mode 100644 index 0000000..60bc5c0 --- /dev/null +++ b/src/main/java/com/mirna/hospitalmanagementapi/domain/services/ConsultationService.java @@ -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; + +}