From 527afe033eea81318acacf2f72fba65be6a16044 Mon Sep 17 00:00:00 2001 From: Liza Medina Date: Tue, 17 Sep 2024 22:39:05 -0500 Subject: [PATCH] from multiPart to json, use just the json request to create the ComplementaryStudy and Editing --- .../ComplementaryStudyController.java | 27 +++------- .../Service/ComplementaryStudyService.java | 50 ++----------------- 2 files changed, 12 insertions(+), 65 deletions(-) diff --git a/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Controller/ComplementaryStudyController.java b/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Controller/ComplementaryStudyController.java index 0b7560f..b6af9ab 100644 --- a/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Controller/ComplementaryStudyController.java +++ b/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Controller/ComplementaryStudyController.java @@ -1,17 +1,12 @@ package com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Controller; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.complementaryStudy.FileRequest; import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.complementaryStudy.StudyRequest; import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.complementaryStudy.StudyCreatedResponse; import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.complementaryStudy.StudyResponse; -import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.ComplementaryStudy; import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.Enum.EnumStudyState; import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Service.ComplementaryStudyService; import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Service.FileStorageService; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -22,8 +17,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; - -import java.io.IOException; import java.util.List; @RestController @@ -34,7 +27,7 @@ public class ComplementaryStudyController { private final ComplementaryStudyService complementaryStudyService; private final FileStorageService fileStorageService; - @PostMapping(value = "/add", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE}) + @PostMapping(value = "/add") @Operation( summary = "Add a new Complementary Study", description = "Add a new complementary study", @@ -50,7 +43,7 @@ public class ComplementaryStudyController { " CANCELADOS, //The study was scheduled but then cancelled.\n" + " FALLIDOS, // The study could not be completed due to technical or other issues.", content = @Content( - mediaType = MediaType.MULTIPART_FORM_DATA_VALUE, + mediaType = "application/json", schema = @Schema(implementation = StudyRequest.class) ) ), @@ -64,17 +57,16 @@ public class ComplementaryStudyController { ) } ) - public ResponseEntity addStudy(@RequestBody StudyRequest studyRequest, @RequestPart(value = "file", required = false) MultipartFile file) { - //return ResponseEntity.ok(complementaryStudyService.addComplementaryStudy(studyRequest, file)); + public ResponseEntity addStudy(@RequestBody StudyRequest studyRequest) { try { - StudyCreatedResponse response = complementaryStudyService.addComplementaryStudy(studyRequest, file); + StudyCreatedResponse response = complementaryStudyService.addComplementaryStudy(studyRequest); return new ResponseEntity<>(response, HttpStatus.OK); } catch (Exception e) { return ResponseEntity.badRequest().body(new StudyCreatedResponse("Error: " + e.getMessage() + " // request: " + studyRequest, null)); } } - @PutMapping(value = "/update/{studyId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + @PutMapping(value = "/update/{studyId}") @Operation( summary = "Update Complementary Study", description = "Fully update a Complementary Study", @@ -82,7 +74,7 @@ public ResponseEntity addStudy(@RequestBody StudyRequest s requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody( description = "Complementary Study object with fields to update", content = @Content( - mediaType = "multipart/form-data", + mediaType = "application/json", schema = @Schema(implementation = StudyRequest.class) ) ), @@ -96,11 +88,8 @@ public ResponseEntity addStudy(@RequestBody StudyRequest s ) } ) - public ResponseEntity updateComplementaryStudy( - @PathVariable Long studyId, - @RequestPart StudyRequest dto, - @RequestPart(name = "file", required = false) MultipartFile studyFile) { - return ResponseEntity.ok(complementaryStudyService.updateStudy(studyId, dto, studyFile)); + public ResponseEntity updateComplementaryStudy(@PathVariable Long studyId, @RequestPart StudyRequest dto) { + return ResponseEntity.ok(complementaryStudyService.updateStudy(studyId, dto)); } diff --git a/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Service/ComplementaryStudyService.java b/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Service/ComplementaryStudyService.java index ebff4c0..0a77e91 100644 --- a/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Service/ComplementaryStudyService.java +++ b/Backend/src/main/java/com/gestor_clinica_veterinaria/VeterinaryHospitalManager/Service/ComplementaryStudyService.java @@ -19,7 +19,6 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -33,47 +32,12 @@ public class ComplementaryStudyService { private final HospitalizationRepository hospitalizationRepository; private final DiagnosticRepository diagnosisRepository; private final ConsultationRepository consultationRepository; - private final FileStorageService fileStorageService; - @Transactional - public StudyCreatedResponse addComplementaryStudy(StudyRequest studyRequest, MultipartFile file) { + public StudyCreatedResponse addComplementaryStudy(StudyRequest studyRequest) { try { ComplementaryStudy study = complementaryStudyMapper.toEntity(studyRequest); - if (file != null && !file.isEmpty()) { - String uploadedFileUrl = fileStorageService.saveFile(file); - study.setStudyFile(uploadedFileUrl); - }else { - study.setStudyFile(null); - } -// if (studyRequest.hospitalizationId().isPresent()) { -// Long hospitalizationId = studyRequest.hospitalizationId().get(); -// Hospitalization hospitalization = hospitalizationRepository.findById(hospitalizationId) -// .orElseThrow(() -> new EntityNotFoundException("Hospitalization not found with id: " + hospitalizationId)); -// study.setHospitalization(hospitalization); -// hospitalization.getComplementaryStudies().add(study); -// hospitalizationRepository.save(hospitalization); -// } -// -// if (studyRequest.diagnosisId().isPresent()) { -// Long diagnosisId = studyRequest.diagnosisId().get(); -// DiagnosticEntity diagnosis = diagnosisRepository.findById(diagnosisId) -// .orElseThrow(() -> new EntityNotFoundException("Diagnosis not found with id: " + diagnosisId)); -// study.setDiagnosis(diagnosis); -// diagnosis.getComplementaryStudies().add(study); -// diagnosisRepository.save(diagnosis); -// } -// -// if (studyRequest.consultationId().isPresent()) { -// Long consultationId = studyRequest.consultationId().get(); -// ConsultationEntity consultation = consultationRepository.findById(consultationId) -// .orElseThrow(() -> new EntityNotFoundException("Consultation not found with id: " + consultationId)); -// study.setConsultation(consultation); -// consultation.getComplementaryStudies().add(study); -// consultationRepository.save(consultation); -// } -// ComprobaciĆ³n para Hospitalization si se proporciona un ID if (studyRequest.hospitalizationId() != null) { Long hospitalizationId = studyRequest.hospitalizationId(); Hospitalization hospitalization = hospitalizationRepository.findById(hospitalizationId) @@ -131,7 +95,7 @@ public List getAllStudiesByOwnerId(Long ownerId){ public List getAllStudiesByState(EnumStudyState state) { return complementaryStudyMapper.toDtoList(complementaryStudyRepository.findByStudyState(state)); } - public StudyResponse updateStudy(Long studyId, StudyRequest dto, MultipartFile studyFile) { + public StudyResponse updateStudy(Long studyId, StudyRequest dto) { Optional studyOptional = complementaryStudyRepository.findById(studyId); if (studyOptional.isPresent()){ @@ -139,17 +103,11 @@ public StudyResponse updateStudy(Long studyId, StudyRequest dto, MultipartFile s existingStudy.setExaminationDate(dto.examinationDate()); existingStudy.setStudyCost(dto.studyCost()); + existingStudy.setStudyFile(dto.studyFile()); existingStudy.setStudyState(dto.studyState()); existingStudy.setStudyResult(dto.studyResult()); existingStudy.setStudyType(dto.studyType()); - if (studyFile != null) { - String filePath = fileStorageService.saveFile(studyFile); - existingStudy.setStudyFile(filePath); - }else { - existingStudy.setStudyFile(null); - } - if (dto.consultationId() != null) { ConsultationEntity consultation = consultationRepository.findById(dto.consultationId()) .orElseThrow(() -> new EntityNotFoundException("Consultation not found with id: " + dto.consultationId())); @@ -174,7 +132,7 @@ public StudyResponse updateStudy(Long studyId, StudyRequest dto, MultipartFile s existingStudy.setHospitalization(null); } - return complementaryStudyMapper.toDtoResponse(complementaryStudyRepository.save(existingStudy)); + return complementaryStudyMapper.toDtoResponse(complementaryStudyRepository.save(existingStudy)); } else { throw new TreatmentNotFoundException("No se ha podido actualizar el tratamiento porque el id ingresado es incorrecto o no existe: " + studyId); }