Skip to content

Commit

Permalink
from multiPart to json, use just the json request to create the Compl…
Browse files Browse the repository at this point in the history
…ementaryStudy and Editing
  • Loading branch information
lizzmedina committed Sep 18, 2024
1 parent 9d96d17 commit 527afe0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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)
)
),
Expand All @@ -64,25 +57,24 @@ public class ComplementaryStudyController {
)
}
)
public ResponseEntity<StudyCreatedResponse> addStudy(@RequestBody StudyRequest studyRequest, @RequestPart(value = "file", required = false) MultipartFile file) {
//return ResponseEntity.ok(complementaryStudyService.addComplementaryStudy(studyRequest, file));
public ResponseEntity<StudyCreatedResponse> 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",
tags = {"Complementary Study"},
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)
)
),
Expand All @@ -96,11 +88,8 @@ public ResponseEntity<StudyCreatedResponse> addStudy(@RequestBody StudyRequest s
)
}
)
public ResponseEntity<StudyResponse> updateComplementaryStudy(
@PathVariable Long studyId,
@RequestPart StudyRequest dto,
@RequestPart(name = "file", required = false) MultipartFile studyFile) {
return ResponseEntity.ok(complementaryStudyService.updateStudy(studyId, dto, studyFile));
public ResponseEntity<StudyResponse> updateComplementaryStudy(@PathVariable Long studyId, @RequestPart StudyRequest dto) {
return ResponseEntity.ok(complementaryStudyService.updateStudy(studyId, dto));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -131,25 +95,19 @@ public List<StudyResponse> getAllStudiesByOwnerId(Long ownerId){
public List<StudyResponse> 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<ComplementaryStudy> studyOptional = complementaryStudyRepository.findById(studyId);

if (studyOptional.isPresent()){
ComplementaryStudy existingStudy = studyOptional.get();

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()));
Expand All @@ -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);
}
Expand Down

0 comments on commit 527afe0

Please sign in to comment.