Skip to content

Commit

Permalink
Merge pull request #20 from GDGoC-KNU-10/issue#19
Browse files Browse the repository at this point in the history
issue#19 코드 수정
  • Loading branch information
yso8296 authored Nov 14, 2024
2 parents 8cae4d0 + e59a8c1 commit 6a27127
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.hackathon.nullnullteam.vaccinationlog.controller;

import com.hackathon.nullnullteam.global.annotation.Authenticate;
import com.hackathon.nullnullteam.global.dto.PagingResponse;
import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto;
import com.hackathon.nullnullteam.vaccinationlog.service.VaccinationService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -18,9 +22,12 @@ public class VaccinationController {
private final VaccinationService vaccinationService;

@GetMapping("")
public List<VaccinationRecommendDto> getVaccinationRecommends(@Authenticate Long memberId) {
List<VaccinationRecommendDto> vaccinationRecommendDtos = vaccinationService.getVaccinationRecommends(memberId);
return vaccinationRecommendDtos;
public PagingResponse getVaccinationRecommends(
@Authenticate Long memberId,
@PageableDefault(page = 0, size = 10) Pageable pageable
) {
Page<VaccinationRecommendDto> vaccinationRecommendDtos = vaccinationService.getVaccinationRecommends(memberId, pageable);
return PagingResponse.from(vaccinationRecommendDtos);
}

@GetMapping("/{vaccination-id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -73,4 +78,37 @@ public List<VaccinationRecommendDto> findByAgeRange(int age) {
)
);
}

public Page<VaccinationRecommendDto> findByVaccineNamesWithPaging(List<String> vaccinationNames, Pageable pageable) {
// SQL 쿼리 생성
String sql = "SELECT id, vaccine_name, start_age, end_age, disease_name " +
"FROM vaccination_recommend " +
"WHERE vaccine_name IN (" + String.join(",", Collections.nCopies(vaccinationNames.size(), "?")) + ") " +
"LIMIT ? OFFSET ?";

// 값 배열 생성 (예방접종 이름 목록과 페이징 정보 포함)
List<Object> params = new ArrayList<>(vaccinationNames);
params.add(pageable.getPageSize());
params.add(pageable.getOffset());

// 데이터 조회
List<VaccinationRecommendDto> recommendations = jdbcTemplate.query(
sql,
params.toArray(),
(rs, rowNum) -> new VaccinationRecommendDto(
rs.getLong("id"),
rs.getString("disease_name"),
rs.getString("vaccine_name"),
rs.getInt("start_age"),
rs.getInt("end_age")
)
);

// 전체 데이터 개수 조회 쿼리
String countSql = "SELECT COUNT(*) FROM vaccination_recommend WHERE vaccine_name IN (" +
String.join(",", Collections.nCopies(vaccinationNames.size(), "?")) + ")";
Integer total = jdbcTemplate.queryForObject(countSql, vaccinationNames.toArray(), Integer.class);

return new PageImpl<>(recommendations, pageable, total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -18,19 +21,19 @@ public class VaccinationService {
private final VaccinationRecommendRepository vaccinationRecommendRepository;
private final MemberReaderService memberReaderService;

public List<VaccinationRecommendDto> getVaccinationRecommends(Long memberId) {
public Page<VaccinationRecommendDto> getVaccinationRecommends(Long memberId, Pageable pageable) {
Member member = memberReaderService.getMemberById(memberId);

// 1. 유저명으로 예방접종 로그에서 예방접종 이름 목록 조회
List<VaccinationLog> vaccinationLogs = vaccinationReaderService.getVaccinationLogs(member.getName());

// 2. 예방접종 이름으로 추천 데이터를 조회하여 리스트에 추가
List<VaccinationRecommendDto> recommendations = new ArrayList<>();
for (VaccinationLog log : vaccinationLogs) {
vaccinationRecommendRepository.findByVaccineName(log.getVaccinationName()).ifPresent(recommendations::add);
}
// 2. 예방접종 로그에서 예방접종 이름 목록 추출
List<String> vaccinationNames = vaccinationLogs.stream()
.map(VaccinationLog::getVaccinationName)
.collect(Collectors.toList());

return recommendations;
// 3. 예방접종 이름 목록으로 추천 데이터를 페이징 처리하여 조회
return vaccinationRecommendRepository.findByVaccineNamesWithPaging(vaccinationNames, pageable);
}

public VaccinationRecommendDto getVaccinationRecommend(Long id) {
Expand Down

0 comments on commit 6a27127

Please sign in to comment.