Skip to content

Commit

Permalink
Refactor: 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
gunGeongun committed Nov 15, 2024
1 parent 8468e7c commit d2402c5
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,8 +39,7 @@ public void AnxietyTestResult(Long memberId, AnxietyResultRecordCommand.Result c
member.setAnxietyLevel(command.score());

// 백신 추천 정보를 조회
List<VaccinationRecommendDto> vaccinationRecommendDtos = vaccinationRecommendRepository.findByAgeRange(
member.getAge());
List<VaccinationRecommend> vaccinationRecommendDtos = vaccinationRecommendRepository.findAllByStartAgeLessThanEqualAndEndAgeGreaterThanEqual(member.getAge());

// 추천 백신 정보를 VaccinationLog 엔티티로 변환하여 저장
vaccinationRecommendDtos.forEach(vaccinationRecommendDto -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VaccinationRecommendDto> 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<VaccinationRecommendDto> 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;
// }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<VaccinationLog> 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)
// );
// }
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,22 +14,22 @@ public class VaccinationService {
private final VaccinationRecommendRepository vaccinationRecommendRepository;
private final MemberReaderService memberReaderService;

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

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

// 2. 예방접종 로그에서 예방접종 이름 목록 추출
List<String> 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<VaccinationRecommendDto> getVaccinationRecommends(Long memberId, Pageable pageable) {
// Member member = memberReaderService.getMemberById(memberId);
//
// // 1. 유저명으로 예방접종 로그에서 예방접종 이름 목록 조회
// List<VaccinationLog> vaccinationLogs = vaccinationReaderService.getVaccinationLogs(member.getName());
//
// // 2. 예방접종 로그에서 예방접종 이름 목록 추출
// List<String> vaccinationNames = vaccinationLogs.stream()
// .map(VaccinationLog::getVaccinationName)
// .collect(Collectors.toList());
//
// // 3. 예방접종 이름 목록으로 추천 데이터를 페이징 처리하여 조회
// return vaccinationRecommendRepository.findByVaccineNamesWithPaging(vaccinationNames, pageable);
// }
//
// public VaccinationRecommendDto getVaccinationRecommend(Long id) {
// return vaccinationReaderService.getVaccinationRecommendById(id);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<VaccinationRecommend> recommendations = vaccinationRecommendService.getRecommendedVaccinations(memberId);
return VaccinationRecommendResponse.Infos.from(recommendations);
}
}
Original file line number Diff line number Diff line change
@@ -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<VaccinationRecommend> recommends
){
public static Infos from(List<VaccinationRecommend> list){
return Infos.builder()
.recommends(list)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<VaccinationRecommend, Long> {

List<VaccinationRecommend> findAllByStartAgeLessThanEqualAndEndAgeGreaterThanEqual(int age);
}
Loading

0 comments on commit d2402c5

Please sign in to comment.