diff --git a/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java b/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java new file mode 100644 index 0000000..335a32f --- /dev/null +++ b/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java @@ -0,0 +1,63 @@ +package com.mirna.hospitalmanagementapi.application.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.mirna.hospitalmanagementapi.application.usecase.patient.FindPatientByIdUseCase; +import com.mirna.hospitalmanagementapi.application.usecase.patient.SavePatientUseCase; +import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientDTO; +import com.mirna.hospitalmanagementapi.domain.entities.Patient; +import com.mirna.hospitalmanagementapi.domain.services.PatientService; +import com.mirna.hospitalmanagementapi.infra.handlers.EntityNotFoundErrorHandler; + +import jakarta.persistence.EntityNotFoundException; + +/** + * This class is an implementation of the PatientService interface. + * + * This class provides methods to perform operations on patients + * + * @author Mirna Gama + * @version 1.0 + */ +@Service +public class PatientServiceImpl implements PatientService { + + @Autowired + private SavePatientUseCase savePatient; + + @Autowired + private FindPatientByIdUseCase findPatientById; + + /** + * Adds a new patient to the database. + * + * @param patientDTO A data transfer object containing the data for Patient + * entity. + * @return The saved patient if successful, or null if there is an error. + */ + @Override + public Patient addPatient(PatientDTO patientDTO) { + Patient patient = new Patient(patientDTO); + + return this.savePatient.execute(patient); + } + + /** + * Finds a stored patient by id. + * + * @param id A long representing the patient's unique identifier + * @return The corresponding patient if successful, or throws an EntityNotFoundException if it is + * non-existent. + * @throws EntityNotFoundException When patient with id provided is non-existent + */ + @Override + public Patient findPatientById(Long id) throws EntityNotFoundException { + Patient patient = this.findPatientById.execute(id); + + if (patient == null) throw new EntityNotFoundException(); + + return patient; + } + +} diff --git a/src/main/java/com/mirna/hospitalmanagementapi/domain/services/PatientService.java b/src/main/java/com/mirna/hospitalmanagementapi/domain/services/PatientService.java new file mode 100644 index 0000000..32ca3b4 --- /dev/null +++ b/src/main/java/com/mirna/hospitalmanagementapi/domain/services/PatientService.java @@ -0,0 +1,31 @@ +package com.mirna.hospitalmanagementapi.domain.services; + +import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientDTO; +import com.mirna.hospitalmanagementapi.domain.entities.Patient; + +/** + * Patient service interface for managing patient information. +* + * @see Patient + * @author Mirna Gama + * @version 1.0 +*/ +public interface PatientService { + + /** + * Adds a new patient to the repository. + * + * @param patientDTO A data transfer object representing a patient to add. + * @return The saved patient if successful, or null if there is an error. + */ + public Patient addPatient(PatientDTO patientDTO); + + /** + * Finds a stored patient by id. + * + * @param id A long representing the patient's unique identifier + * @return The corresponding patient if successful, or null if it is non-existent. + */ + public Patient findPatientById(Long id); + +}