-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: 공통 추천 대학 로직 변경 #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.example.solidconnection.university.service; | ||
|
|
||
| import com.example.solidconnection.university.domain.UniversityInfoForApply; | ||
| import com.example.solidconnection.university.repository.UniversityInfoForApplyRepository; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.example.solidconnection.university.service.UniversityRecommendService.RECOMMEND_UNIVERSITY_NUM; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GeneralUniversityRecommendService { | ||
|
|
||
| /* | ||
| * 해당 시기에 열리는 대학교들 중 랜덤으로 선택해서 목록을 구성한다. | ||
| * */ | ||
| private final UniversityInfoForApplyRepository universityInfoForApplyRepository; | ||
|
|
||
| @Getter | ||
| private List<UniversityInfoForApply> recommendUniversities; | ||
|
|
||
| @Value("${university.term}") | ||
| public String term; | ||
|
|
||
| @EventListener(ApplicationReadyEvent.class) | ||
| public void init() { | ||
| recommendUniversities = universityInfoForApplyRepository.findRandomByTerm(term, RECOMMEND_UNIVERSITY_NUM); | ||
| } | ||
|
Comment on lines
+31
to
+34
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 원래 add 를 사용하면, 외부에서 init() 함수를 호출할 때마다 list 에 12개, 18개, 24개.. 이런식으로 쌓이게 됩니다. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ public class UniversityRecommendService { | |
| public static final int RECOMMEND_UNIVERSITY_NUM = 6; | ||
|
|
||
| private final UniversityInfoForApplyRepository universityInfoForApplyRepository; | ||
| private final GeneralRecommendUniversities generalRecommendUniversities; | ||
| private final GeneralUniversityRecommendService generalUniversityRecommendService; | ||
| private final SiteUserRepository siteUserRepository; | ||
|
|
||
| @Value("${university.term}") | ||
|
|
@@ -56,7 +56,7 @@ public UniversityRecommendsResponse getPersonalRecommends(String email) { | |
| } | ||
|
|
||
| private List<UniversityInfoForApply> getGeneralRecommendsExcludingSelected(List<UniversityInfoForApply> alreadyPicked) { | ||
| List<UniversityInfoForApply> generalRecommend = new ArrayList<>(generalRecommendUniversities.getRecommendUniversities()); | ||
| List<UniversityInfoForApply> generalRecommend = new ArrayList<>(generalUniversityRecommendService.getRecommendUniversities()); | ||
| generalRecommend.removeAll(alreadyPicked); | ||
| Collections.shuffle(generalRecommend); | ||
| return generalRecommend.subList(0, RECOMMEND_UNIVERSITY_NUM - alreadyPicked.size()); | ||
|
|
@@ -68,8 +68,7 @@ private List<UniversityInfoForApply> getGeneralRecommendsExcludingSelected(List< | |
| @Transactional(readOnly = true) | ||
| @ThunderingHerdCaching(key = "university:recommend:general", cacheManager = "customCacheManager", ttlSec = 86400) | ||
| public UniversityRecommendsResponse getGeneralRecommends() { | ||
| List<UniversityInfoForApply> generalRecommends = new ArrayList<>(generalRecommendUniversities.getRecommendUniversities()); | ||
| Collections.shuffle(generalRecommends); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 셔플하는 코드를 없앴습니다. |
||
| List<UniversityInfoForApply> generalRecommends = new ArrayList<>(generalUniversityRecommendService.getRecommendUniversities()); | ||
| return new UniversityRecommendsResponse(generalRecommends.stream() | ||
| .map(UniversityInfoForApplyPreviewResponse::from) | ||
| .toList()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.example.solidconnection.university.service; | ||
|
|
||
| import com.example.solidconnection.support.TestContainerSpringBootTest; | ||
| import com.example.solidconnection.support.integration.BaseIntegrationTest; | ||
| import com.example.solidconnection.university.domain.UniversityInfoForApply; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.example.solidconnection.university.service.UniversityRecommendService.RECOMMEND_UNIVERSITY_NUM; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
|
||
| @DisplayName("공통 추천 대학 서비스 테스트") | ||
| @TestContainerSpringBootTest | ||
| class GeneralUniversityRecommendServiceTest extends BaseIntegrationTest { | ||
|
|
||
| @Autowired | ||
| private GeneralUniversityRecommendService generalUniversityRecommendService; | ||
|
|
||
| @Value("${university.term}") | ||
| private String term; | ||
|
|
||
| @Test | ||
| void 모집_시기의_대학들_중에서_랜덤하게_N개를_추천_목록으로_구성한다() { | ||
| // given | ||
| generalUniversityRecommendService.init(); | ||
| List<UniversityInfoForApply> universities = generalUniversityRecommendService.getRecommendUniversities(); | ||
|
|
||
| // when & then | ||
| assertAll( | ||
| () -> assertThat(universities) | ||
| .extracting("term") | ||
| .allMatch(term::equals), | ||
| () -> assertThat(universities).hasSize(RECOMMEND_UNIVERSITY_NUM) | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기존에 GeneralRecommendUniversities 였던 클래스 이름을
GeneralUniversityRecommendService 로 바꿨습니다.
이전 이름은 도메인의 느낌이 들어서 변경의 필요성을 느꼈고,
비지니스 로직이 담긴 클래스이니 Service 를 붙여주었습니다.