From d2402c5095bebc689caf06b196388d15256eb5cb Mon Sep 17 00:00:00 2001 From: geonny Date: Sat, 16 Nov 2024 05:33:27 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AnxietyResultRecordService.java | 8 +- .../controller/VaccinationController.java | 36 +++--- .../VaccinationRecommendRepository.java | 114 ------------------ .../service/VaccinationReaderService.java | 19 ++- .../service/VaccinationService.java | 45 +++---- .../service/VaccinationWriterService.java | 5 +- .../VaccinationRecommend.java | 28 +++++ .../VaccinationRecommendController.java | 28 +++++ .../dto/VaccinationRecommendResponse.java | 19 +++ .../VaccinationRecommendRepository.java | 11 ++ .../service/VaccinationRecommendService.java | 27 +++++ .../vaccinationrecord/VaccinationRecord.java | 4 - 12 files changed, 166 insertions(+), 178 deletions(-) delete mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationlog/infrastructure/repository/VaccinationRecommendRepository.java create mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/VaccinationRecommend.java create mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/VaccinationRecommendController.java create mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/dto/VaccinationRecommendResponse.java create mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/infrastructure/repository/VaccinationRecommendRepository.java create mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/service/VaccinationRecommendService.java delete mode 100644 src/main/java/com/hackathon/nullnullteam/vaccinationrecord/VaccinationRecord.java diff --git a/src/main/java/com/hackathon/nullnullteam/anxietyresultrecord/service/AnxietyResultRecordService.java b/src/main/java/com/hackathon/nullnullteam/anxietyresultrecord/service/AnxietyResultRecordService.java index 978d7a7..f5bbeba 100644 --- a/src/main/java/com/hackathon/nullnullteam/anxietyresultrecord/service/AnxietyResultRecordService.java +++ b/src/main/java/com/hackathon/nullnullteam/anxietyresultrecord/service/AnxietyResultRecordService.java @@ -6,14 +6,15 @@ import com.hackathon.nullnullteam.anxietyresultrecord.service.dto.AnxietyResultRecordModel.Info; import com.hackathon.nullnullteam.member.Member; import com.hackathon.nullnullteam.member.service.MemberReaderService; -import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.VaccinationRecommendRepository; import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto; import com.hackathon.nullnullteam.vaccinationlog.service.VaccinationWriterService; +import com.hackathon.nullnullteam.vaccinationrecommend.VaccinationRecommend; +import com.hackathon.nullnullteam.vaccinationrecommend.infrastructure.repository.VaccinationRecommendRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; -import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -38,8 +39,7 @@ public void AnxietyTestResult(Long memberId, AnxietyResultRecordCommand.Result c member.setAnxietyLevel(command.score()); // 백신 추천 정보를 조회 - List vaccinationRecommendDtos = vaccinationRecommendRepository.findByAgeRange( - member.getAge()); + List vaccinationRecommendDtos = vaccinationRecommendRepository.findAllByStartAgeLessThanEqualAndEndAgeGreaterThanEqual(member.getAge()); // 추천 백신 정보를 VaccinationLog 엔티티로 변환하여 저장 vaccinationRecommendDtos.forEach(vaccinationRecommendDto -> { diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/controller/VaccinationController.java b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/controller/VaccinationController.java index 1657e08..429c942 100644 --- a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/controller/VaccinationController.java +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/controller/VaccinationController.java @@ -21,22 +21,22 @@ public class VaccinationController { private final VaccinationService vaccinationService; - @GetMapping("") - public PagingResponse getVaccinationRecommends( - @Authenticate Long memberId, - @PageableDefault(page = 0, size = 10) Pageable pageable - ) { - Page vaccinationRecommendDtos = vaccinationService.getVaccinationRecommends(memberId, pageable); - return PagingResponse.from(vaccinationRecommendDtos); - } - - @GetMapping("/{vaccination-id}") - public VaccinationRecommendDto getVaccinationRecommend( - @Authenticate Long memberId, - @PathVariable("vaccination-id") Long vaccineId - ) { - VaccinationRecommendDto vaccinationRecommend = vaccinationService.getVaccinationRecommend( - vaccineId); - return vaccinationRecommend; - } +// @GetMapping("") +// public PagingResponse getVaccinationRecommends( +// @Authenticate Long memberId, +// @PageableDefault(page = 0, size = 10) Pageable pageable +// ) { +// Page vaccinationRecommendDtos = vaccinationService.getVaccinationRecommends(memberId, pageable); +// return PagingResponse.from(vaccinationRecommendDtos); +// } +// +// @GetMapping("/{vaccination-id}") +// public VaccinationRecommendDto getVaccinationRecommend( +// @Authenticate Long memberId, +// @PathVariable("vaccination-id") Long vaccineId +// ) { +// VaccinationRecommendDto vaccinationRecommend = vaccinationService.getVaccinationRecommend( +// vaccineId); +// return vaccinationRecommend; +// } } diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/infrastructure/repository/VaccinationRecommendRepository.java b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/infrastructure/repository/VaccinationRecommendRepository.java deleted file mode 100644 index 6dabbe8..0000000 --- a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/infrastructure/repository/VaccinationRecommendRepository.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository; - -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; - -@RequiredArgsConstructor -@Repository -public class VaccinationRecommendRepository { - - private final JdbcTemplate jdbcTemplate; - - public Optional findByVaccineName(String vaccineName) { - String sql = "SELECT id, vaccine_name, start_age, end_age, disease_name " + - "FROM vaccination_recommend " + - "WHERE disease_name = ? LIMIT 1"; - - Optional result = jdbcTemplate.query(sql, new Object[]{vaccineName}, rs -> { - if (rs.next()) { - return Optional.of(new VaccinationRecommendDto( - rs.getLong("id"), - rs.getString("disease_name"), - rs.getString("vaccine_name"), - rs.getInt("start_age"), - rs.getInt("end_age") - )); - } else { - return Optional.empty(); - } - }); - - return result; - } - - public Optional findById(Long id) { - String sql = "SELECT id, vaccine_name, start_age, end_age, disease_name " + - "FROM vaccination_recommend " + - "WHERE id = ? LIMIT 1"; - - return jdbcTemplate.query(sql, new Object[]{id}, rs -> { - if (rs.next()) { - return Optional.of(new VaccinationRecommendDto( - rs.getLong("id"), - rs.getString("disease_name"), - rs.getString("vaccine_name"), - rs.getInt("start_age"), - rs.getInt("end_age") - )); - } else { - return Optional.empty(); - } - }); - } - - public List findByAgeRange(int age) { - String sql = "SELECT id, vaccine_name, start_age, end_age, disease_name " + - "FROM vaccination_recommend " + - "WHERE start_age <= ? AND end_age >= ?"; - - return jdbcTemplate.query(sql, new Object[]{age, age}, (rs, rowNum) -> - new VaccinationRecommendDto( - rs.getLong("id"), - rs.getString("disease_name"), - rs.getString("vaccine_name"), - rs.getInt("start_age"), - rs.getInt("end_age") - ) - ); - } - - public Page findByVaccineNamesWithPaging(List 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 params = new ArrayList<>(vaccinationNames); - params.add(pageable.getPageSize()); - params.add(pageable.getOffset()); - - // 데이터 조회 - List 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); - } -} diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationReaderService.java b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationReaderService.java index a160e9e..bcfb941 100644 --- a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationReaderService.java +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationReaderService.java @@ -4,28 +4,27 @@ import com.hackathon.nullnullteam.global.exception.EntityNotFoundException; import com.hackathon.nullnullteam.vaccinationlog.VaccinationLog; import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.VaccinationLogRepository; -import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.VaccinationRecommendRepository; import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto; -import java.util.List; -import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @RequiredArgsConstructor public class VaccinationReaderService { private final VaccinationLogRepository vaccinationLogRepository; - private final VaccinationRecommendRepository vaccinationRecommendRepository; +// private final VaccinationRecommendRepository vaccinationRecommendRepository; public List getVaccinationLogs(String memberName) { return vaccinationLogRepository.findByUsername(memberName); } - public VaccinationRecommendDto getVaccinationRecommendById(Long id) { - return vaccinationRecommendRepository.findById(id) - .orElseThrow( - () -> new EntityNotFoundException(MessageConstants.VACCINE_NOT_FOUND_MESSAGE) - ); - } +// public VaccinationRecommendDto getVaccinationRecommendById(Long id) { +// return vaccinationRecommendRepository.findById(id) +// .orElseThrow( +// () -> new EntityNotFoundException(MessageConstants.VACCINE_NOT_FOUND_MESSAGE) +// ); +// } } diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationService.java b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationService.java index b7d3c2c..0dcd6a8 100644 --- a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationService.java +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationService.java @@ -1,16 +1,9 @@ package com.hackathon.nullnullteam.vaccinationlog.service; -import com.hackathon.nullnullteam.member.Member; import com.hackathon.nullnullteam.member.service.MemberReaderService; -import com.hackathon.nullnullteam.vaccinationlog.VaccinationLog; -import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.VaccinationRecommendRepository; import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; +import com.hackathon.nullnullteam.vaccinationrecommend.infrastructure.repository.VaccinationRecommendRepository; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service @@ -21,22 +14,22 @@ public class VaccinationService { private final VaccinationRecommendRepository vaccinationRecommendRepository; private final MemberReaderService memberReaderService; - public Page getVaccinationRecommends(Long memberId, Pageable pageable) { - Member member = memberReaderService.getMemberById(memberId); - - // 1. 유저명으로 예방접종 로그에서 예방접종 이름 목록 조회 - List vaccinationLogs = vaccinationReaderService.getVaccinationLogs(member.getName()); - - // 2. 예방접종 로그에서 예방접종 이름 목록 추출 - List vaccinationNames = vaccinationLogs.stream() - .map(VaccinationLog::getVaccinationName) - .collect(Collectors.toList()); - - // 3. 예방접종 이름 목록으로 추천 데이터를 페이징 처리하여 조회 - return vaccinationRecommendRepository.findByVaccineNamesWithPaging(vaccinationNames, pageable); - } - - public VaccinationRecommendDto getVaccinationRecommend(Long id) { - return vaccinationReaderService.getVaccinationRecommendById(id); - } +// public Page getVaccinationRecommends(Long memberId, Pageable pageable) { +// Member member = memberReaderService.getMemberById(memberId); +// +// // 1. 유저명으로 예방접종 로그에서 예방접종 이름 목록 조회 +// List vaccinationLogs = vaccinationReaderService.getVaccinationLogs(member.getName()); +// +// // 2. 예방접종 로그에서 예방접종 이름 목록 추출 +// List vaccinationNames = vaccinationLogs.stream() +// .map(VaccinationLog::getVaccinationName) +// .collect(Collectors.toList()); +// +// // 3. 예방접종 이름 목록으로 추천 데이터를 페이징 처리하여 조회 +// return vaccinationRecommendRepository.findByVaccineNamesWithPaging(vaccinationNames, pageable); +// } +// +// public VaccinationRecommendDto getVaccinationRecommend(Long id) { +// return vaccinationReaderService.getVaccinationRecommendById(id); +// } } diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationWriterService.java b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationWriterService.java index a567a9b..5dd7245 100644 --- a/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationWriterService.java +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationlog/service/VaccinationWriterService.java @@ -3,6 +3,7 @@ import com.hackathon.nullnullteam.vaccinationlog.VaccinationLog; import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.VaccinationLogRepository; import com.hackathon.nullnullteam.vaccinationlog.infrastructure.repository.dto.VaccinationRecommendDto; +import com.hackathon.nullnullteam.vaccinationrecommend.VaccinationRecommend; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -14,10 +15,10 @@ public class VaccinationWriterService { private final VaccinationLogRepository vaccinationLogRepository; @Transactional - public void save(String memberName, VaccinationRecommendDto vaccinationRecommendDto) { + public void save(String memberName, VaccinationRecommend vaccinationRecommend) { VaccinationLog vaccinationLog = VaccinationLog.builder() .username(memberName) - .vaccinationName(vaccinationRecommendDto.vaccineName()) + .vaccinationName(vaccinationRecommend.getName()) .build(); vaccinationLogRepository.save(vaccinationLog); diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/VaccinationRecommend.java b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/VaccinationRecommend.java new file mode 100644 index 0000000..d1c9b54 --- /dev/null +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/VaccinationRecommend.java @@ -0,0 +1,28 @@ +package com.hackathon.nullnullteam.vaccinationrecommend; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Builder +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class VaccinationRecommend { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + private int startAge; + + private int endAge; +} diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/VaccinationRecommendController.java b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/VaccinationRecommendController.java new file mode 100644 index 0000000..0d6ba63 --- /dev/null +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/VaccinationRecommendController.java @@ -0,0 +1,28 @@ +package com.hackathon.nullnullteam.vaccinationrecommend.controller; + +import com.hackathon.nullnullteam.vaccinationrecommend.VaccinationRecommend; +import com.hackathon.nullnullteam.vaccinationrecommend.controller.dto.VaccinationRecommendResponse; +import com.hackathon.nullnullteam.vaccinationrecommend.service.VaccinationRecommendService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/vaccination") +public class VaccinationRecommendController { + + private final VaccinationRecommendService vaccinationRecommendService; + + @GetMapping("") + public VaccinationRecommendResponse.Infos getRecommendedVaccinations( + @PathVariable Long memberId) { + List recommendations = vaccinationRecommendService.getRecommendedVaccinations(memberId); + return VaccinationRecommendResponse.Infos.from(recommendations); + } +} diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/dto/VaccinationRecommendResponse.java b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/dto/VaccinationRecommendResponse.java new file mode 100644 index 0000000..aa6db75 --- /dev/null +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/controller/dto/VaccinationRecommendResponse.java @@ -0,0 +1,19 @@ +package com.hackathon.nullnullteam.vaccinationrecommend.controller.dto; + +import com.hackathon.nullnullteam.vaccinationrecommend.VaccinationRecommend; +import lombok.Builder; + +import java.util.List; + +public class VaccinationRecommendResponse { + @Builder + public record Infos( + List recommends + ){ + public static Infos from(List list){ + return Infos.builder() + .recommends(list) + .build(); + } + } +} diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/infrastructure/repository/VaccinationRecommendRepository.java b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/infrastructure/repository/VaccinationRecommendRepository.java new file mode 100644 index 0000000..15cacbe --- /dev/null +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/infrastructure/repository/VaccinationRecommendRepository.java @@ -0,0 +1,11 @@ +package com.hackathon.nullnullteam.vaccinationrecommend.infrastructure.repository; + +import com.hackathon.nullnullteam.vaccinationrecommend.VaccinationRecommend; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface VaccinationRecommendRepository extends JpaRepository { + + List findAllByStartAgeLessThanEqualAndEndAgeGreaterThanEqual(int age); +} diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/service/VaccinationRecommendService.java b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/service/VaccinationRecommendService.java new file mode 100644 index 0000000..2cc05c5 --- /dev/null +++ b/src/main/java/com/hackathon/nullnullteam/vaccinationrecommend/service/VaccinationRecommendService.java @@ -0,0 +1,27 @@ +package com.hackathon.nullnullteam.vaccinationrecommend.service; + +import com.hackathon.nullnullteam.member.Member; +import com.hackathon.nullnullteam.member.infrastructure.repository.MemberRepository; +import com.hackathon.nullnullteam.member.service.MemberReaderService; +import com.hackathon.nullnullteam.vaccinationrecommend.VaccinationRecommend; +import com.hackathon.nullnullteam.vaccinationrecommend.infrastructure.repository.VaccinationRecommendRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class VaccinationRecommendService { + + private final VaccinationRecommendRepository vaccinationRecommendRepository; + private final MemberReaderService memberReaderService; + + + public List getRecommendedVaccinations(Long memberId) { + Member member = memberReaderService.getMemberById(memberId); + + List recommendations = vaccinationRecommendRepository.findAllByStartAgeLessThanEqualAndEndAgeGreaterThanEqual(member.getAge()); + return recommendations; + } +} \ No newline at end of file diff --git a/src/main/java/com/hackathon/nullnullteam/vaccinationrecord/VaccinationRecord.java b/src/main/java/com/hackathon/nullnullteam/vaccinationrecord/VaccinationRecord.java deleted file mode 100644 index 3b72343..0000000 --- a/src/main/java/com/hackathon/nullnullteam/vaccinationrecord/VaccinationRecord.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hackathon.nullnullteam.vaccinationrecord; - -public class VaccinationRecord { -}