From d9a276856db0f89ff919596d6825e625d1847353 Mon Sep 17 00:00:00 2001 From: Mirna Gama Date: Wed, 10 Jan 2024 14:40:47 -0300 Subject: [PATCH] [R7] Update method in Patient Service class --- .../services/PatientServiceImpl.java | 61 +++++++++++++++++++ .../domain/services/PatientService.java | 8 +++ 2 files changed, 69 insertions(+) diff --git a/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java b/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java index 0710aa7..4e773e3 100644 --- a/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java +++ b/src/main/java/com/mirna/hospitalmanagementapi/application/services/PatientServiceImpl.java @@ -8,8 +8,11 @@ import org.springframework.stereotype.Service; import com.mirna.hospitalmanagementapi.application.usecase.patient.FindPatientByIdUseCase; import com.mirna.hospitalmanagementapi.application.usecase.patient.FindPatientsUseCase; import com.mirna.hospitalmanagementapi.application.usecase.patient.SavePatientUseCase; +import com.mirna.hospitalmanagementapi.domain.dtos.AddressDTO; import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientDTO; import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientPublicDataDTO; +import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientUpdatedDataDTO; +import com.mirna.hospitalmanagementapi.domain.entities.Address; import com.mirna.hospitalmanagementapi.domain.entities.Patient; import com.mirna.hospitalmanagementapi.domain.services.PatientService; import com.mirna.hospitalmanagementapi.infra.handlers.EntityNotFoundErrorHandler; @@ -79,4 +82,62 @@ public class PatientServiceImpl implements PatientService { return findPatients.execute(pageable).map(PatientPublicDataDTO::new); } + /** + * Updates an existing patient record + * @param patientUpdatedDataDTO Data transfer object containing the patient updated data along with their corresponding id + * + * @return The updated patient if successful, or null if there is an error. + */ + @Override + public Patient updatePatient(PatientUpdatedDataDTO patientUpdatedDataDTO) { + + Patient patient = findPatientById.execute(patientUpdatedDataDTO.id()); + + if (patient == null) throw new EntityNotFoundException(); + + if (patientUpdatedDataDTO.name() != null) patient.setName(patientUpdatedDataDTO.name()); + + if (patientUpdatedDataDTO.telephone() != null) patient.setTelephone(patientUpdatedDataDTO.telephone()); + + if (patientUpdatedDataDTO.address() != null) { + + AddressDTO addressUpdatedDataDTO = patientUpdatedDataDTO.address(); + Address address = patient.getAddress(); + + if (addressUpdatedDataDTO.street() != null) { + address.setStreet(addressUpdatedDataDTO.street()); + } + + if (addressUpdatedDataDTO.neighborhood() != null) { + address.setNeighborhood(addressUpdatedDataDTO.neighborhood()); + } + + if (addressUpdatedDataDTO.city() != null) { + address.setCity(addressUpdatedDataDTO.city()); + } + + if (addressUpdatedDataDTO.zipCode() != null) { + address.setZipCode(addressUpdatedDataDTO.zipCode()); + } + + if (addressUpdatedDataDTO.state() != null) { + address.setState(addressUpdatedDataDTO.state()); + } + + if (addressUpdatedDataDTO.additionalDetails() != null) { + address.setAdditionalDetails(addressUpdatedDataDTO.additionalDetails()); + } + + if (addressUpdatedDataDTO.houseNumber() != null) { + address.setHouseNumber(addressUpdatedDataDTO.houseNumber()); + } + + patient.setAddress(address); + } + + patient = savePatient.execute(patient); + + 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 index 8770899..13ffa5d 100644 --- a/src/main/java/com/mirna/hospitalmanagementapi/domain/services/PatientService.java +++ b/src/main/java/com/mirna/hospitalmanagementapi/domain/services/PatientService.java @@ -5,6 +5,7 @@ import org.springframework.data.domain.Pageable; import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientDTO; import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientPublicDataDTO; +import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientUpdatedDataDTO; import com.mirna.hospitalmanagementapi.domain.entities.Patient; /** @@ -41,4 +42,11 @@ public interface PatientService { */ public Page findPatients(Pageable pageable); + /** + * Updates an existing patient record + * @param patientUpdatedDataDTO Data transfer object containing the patient updated data along with their corresponding id + * + * @return The updated patient if successful, or null if there is an error. + */ + public Patient updatePatient(PatientUpdatedDataDTO patientUpdatedDataDTO); }