Skip to content

Commit

Permalink
Get hospitalization by id pet
Browse files Browse the repository at this point in the history
  • Loading branch information
calderonic committed Sep 18, 2024
1 parent 527afe0 commit 06a9976
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Controller;

import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationCreationResponse;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationDtoResponse;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationRequest;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationResponse;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.Hospitalization;
Expand Down Expand Up @@ -172,4 +173,10 @@ public ResponseEntity<List<HospitalizationResponse>> getHospitalizationByComplem
public ResponseEntity<HospitalizationResponse> updateHospitalization(@PathVariable Long hospitalizationId, @RequestBody HospitalizationRequest dto) {
return ResponseEntity.ok(hospitalizationService.updateHospitalization(hospitalizationId, dto));
}

@GetMapping("/by-pet/{petId}")
public ResponseEntity<List<HospitalizationDtoResponse>> getHospitalizationsByPetId(@PathVariable Long petId) {
List<HospitalizationDtoResponse> hospitalizations = hospitalizationService.getHospitalizationsByPetId(petId);
return ResponseEntity.ok(hospitalizations);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

public record HospitalizationDtoResponse(

Long id,
LocalDate startDate,
LocalDate endDate,
BigDecimal hospitalizationCost,
boolean paid,
List<Long> treatmentIds, // Lista de IDs de tratamientos
List<Long> complementaryStudies // Lista de IDs de estudios complementario

) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.Hospitalization;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -13,4 +15,10 @@ public interface HospitalizationRepository extends JpaRepository<Hospitalization
List<Hospitalization> findByTreatments_Id(Long treatmentId);

List<Hospitalization> findByComplementaryStudies_Id(Long complementaryStudyId);

@Query("SELECT h FROM Hospitalization h JOIN h.complementaryStudies cs JOIN cs.consultation c WHERE c.pet.id = :petId")
List<Hospitalization> findHospitalizationsByPetId(@Param("petId") Long petId);

@Query("SELECT h FROM Hospitalization h JOIN h.treatments t JOIN t.diagnosis d JOIN d.consultation c WHERE c.pet.id = :petId")
List<Hospitalization> findHospitalizationsByPetIdTreatment(@Param("petId") Long petId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Service;

import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationCreationResponse;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationDtoResponse;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationRequest;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Dto.hospitalization.HospitalizationResponse;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.ComplementaryStudy;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.Hospitalization;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Entity.Treatment;
import com.gestor_clinica_veterinaria.VeterinaryHospitalManager.Mapper.HospitalizationMapper;
Expand All @@ -16,7 +18,10 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -111,4 +116,46 @@ private BigDecimal calculateCost(LocalDate startDate, LocalDate endDate) {
}
return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
}

// Convertir entidad Hospitalization a DTO
private HospitalizationDtoResponse convertToDto(Hospitalization hospitalization) {
return new HospitalizationDtoResponse(
hospitalization.getId(),
hospitalization.getStartDate(),
hospitalization.getEnd_date(), // Usar el nombre del atributo en la entidad
hospitalization.getHospitalizationCost(),
hospitalization.isPaid(),
hospitalization.getTreatments().stream()
.map(Treatment::getId)
.toList(), // Usar Stream.toList()
hospitalization.getComplementaryStudies().stream()
.map(ComplementaryStudy::getId) // Asegúrate de que `getId()` exista
.toList() // Usar Stream.toList()
);
}
// Obtener hospitalizaciones por ID de mascota y devolver como DTO
// public List<HospitalizationDtoResponse> getHospitalizationsByPetId(Long petId) {
// List<Hospitalization> hospitalizations = hospitalizationRepository.findHospitalizationsByPetId(petId);
// return hospitalizations.stream()
// .map(this::convertToDto)
// .collect(Collectors.toList());
// }

public List<HospitalizationDtoResponse> getHospitalizationsByPetId(Long petId) {
// Obtener hospitalizaciones desde ComplementaryStudies
List<Hospitalization> hospitalizationsByStudies = hospitalizationRepository.findHospitalizationsByPetId(petId);

// Obtener hospitalizaciones desde Treatment y Diagnosis
List<Hospitalization> hospitalizationsByTreatment = hospitalizationRepository.findHospitalizationsByPetIdTreatment(petId);

// Unir ambas listas y eliminar duplicados
Set<Hospitalization> combinedHospitalizations = new HashSet<>(hospitalizationsByStudies);
combinedHospitalizations.addAll(hospitalizationsByTreatment);

// Convertir a DTO
return combinedHospitalizations.stream()
.map(this::convertToDto)
.collect(Collectors.toList());
}

}

0 comments on commit 06a9976

Please sign in to comment.