Skip to content

Commit 2bb6e95

Browse files
committed
refactor: 코드리뷰 수정사항 반영
- Long타입 long으로 수정 - collect -> toList단독으로 변경 - 컨벤션 수정 - Recomment 변경사항 원복 - getGeneralRecommendsExcludingSelected함수 OutOfRange 방지 추가
1 parent f24495e commit 2bb6e95

9 files changed

Lines changed: 35 additions & 32 deletions

File tree

src/main/java/com/example/solidconnection/application/repository/ApplicationRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface ApplicationRepository extends JpaRepository<Application, Long>
3737
""")
3838
Optional<Application> findBySiteUserIdAndTerm(@Param("siteUserId") Long siteUserId, @Param("term") String term);
3939

40-
default Application getApplicationBySiteUserIdAndTerm(Long siteUserId, String term) {
40+
default Application getApplicationBySiteUserIdAndTerm(long siteUserId, String term) {
4141
return findBySiteUserIdAndTerm(siteUserId, term)
4242
.orElseThrow(() -> new CustomException(APPLICATION_NOT_FOUND));
4343
}

src/main/java/com/example/solidconnection/score/service/ScoreService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public GpaScoreStatusesResponse getGpaScoreStatus(SiteUser siteUser) {
5757
gpaScoreRepository.findBySiteUserId(siteUser.getId())
5858
.stream()
5959
.map(GpaScoreStatusResponse::from)
60-
.collect(Collectors.toList());
60+
.toList();
6161

6262
return GpaScoreStatusesResponse.from(gpaScoreStatusResponseList);
6363
}

src/main/java/com/example/solidconnection/university/domain/UnivApplyInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public class UnivApplyInfo {
7777
@Column(length = 1000)
7878
private String details;
7979

80-
@OneToMany(mappedBy = "univApplyInfo", cascade = CascadeType.ALL, orphanRemoval = true)
80+
@OneToMany(mappedBy = "univApplyInfo", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
8181
private Set<LanguageRequirement> languageRequirements = new HashSet<>();
8282

83-
@ManyToOne(fetch = FetchType.LAZY)
83+
@ManyToOne(fetch = FetchType.EAGER)
8484
private University university;
8585

8686
public void addLanguageRequirements(LanguageRequirement languageRequirements) {

src/main/java/com/example/solidconnection/university/repository/LikedUnivApplyInfoRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public interface LikedUnivApplyInfoRepository extends JpaRepository<LikedUnivApp
2525
""")
2626
List<UnivApplyInfo> findUnivApplyInfosBySiteUserId(@Param("siteUserId") long siteUserId);
2727

28-
boolean existsBySiteUserIdAndUnivApplyInfoId(long siteUserId, Long univApplyInfoId);
28+
boolean existsBySiteUserIdAndUnivApplyInfoId(long siteUserId, long univApplyInfoId);
2929
}

src/main/java/com/example/solidconnection/university/repository/UnivApplyInfoRepository.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,14 @@ OR r.code IN (
3737
""")
3838
List<UnivApplyInfo> findAllBySiteUsersInterestedCountryOrRegionAndTerm(@Param("siteUserId") Long siteUserId, @Param("term") String term);
3939

40-
@Query("""
41-
SELECT DISTINCT uai
42-
FROM UnivApplyInfo uai
43-
LEFT JOIN FETCH uai.languageRequirements lr
44-
LEFT JOIN FETCH uai.university u
45-
LEFT JOIN FETCH u.country c
46-
LEFT JOIN FETCH u.region r
47-
WHERE uai.term = :term
48-
ORDER BY FUNCTION('RAND')
49-
""")
50-
List<UnivApplyInfo> findAllRandomByTerm(@Param("term") String term);
51-
default List<UnivApplyInfo> findRandomByTerm(String term, int limitNum) {
52-
return findAllRandomByTerm(term).stream()
53-
.limit(limitNum)
54-
.collect(Collectors.toList());
55-
}
40+
@Query(value = """
41+
SELECT *
42+
FROM university_info_for_apply
43+
WHERE term = :term
44+
ORDER BY RAND()
45+
LIMIT :limitNum
46+
""", nativeQuery = true)
47+
List<UnivApplyInfo> findRandomByTerm(@Param("term") String term, @Param("limitNum") int limitNum);
5648

5749
default UnivApplyInfo getUnivApplyInfoById(Long id) {
5850
return findById(id)

src/main/java/com/example/solidconnection/university/service/GeneralUnivApplyInfoRecommendService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import com.example.solidconnection.university.domain.UnivApplyInfo;
44
import com.example.solidconnection.university.repository.UnivApplyInfoRepository;
5+
import lombok.Getter;
56
import lombok.RequiredArgsConstructor;
67
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.boot.context.event.ApplicationReadyEvent;
9+
import org.springframework.context.event.EventListener;
710
import org.springframework.stereotype.Service;
8-
import org.springframework.transaction.annotation.Transactional;
911

1012
import java.util.List;
1113

@@ -20,11 +22,14 @@ public class GeneralUnivApplyInfoRecommendService {
2022
* */
2123
private final UnivApplyInfoRepository univApplyInfoRepository;
2224

25+
@Getter
26+
private List<UnivApplyInfo> generalRecommends;
27+
2328
@Value("${university.term}")
2429
public String term;
2530

26-
@Transactional(readOnly = true)
27-
public List<UnivApplyInfo> getGeneralRecommends() {
28-
return univApplyInfoRepository.findRandomByTerm(term, RECOMMEND_UNIV_APPLY_INFO_NUM);
31+
@EventListener(ApplicationReadyEvent.class)
32+
public void init() {
33+
generalRecommends = univApplyInfoRepository.findRandomByTerm(term, RECOMMEND_UNIV_APPLY_INFO_NUM);
2934
}
3035
}

src/main/java/com/example/solidconnection/university/service/UnivApplyInfoRecommendService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ private List<UnivApplyInfo> getGeneralRecommendsExcludingSelected(List<UnivApply
5656
List<UnivApplyInfo> generalRecommend = new ArrayList<>(generalUnivApplyInfoRecommendService.getGeneralRecommends());
5757
generalRecommend.removeAll(alreadyPicked);
5858
Collections.shuffle(generalRecommend);
59-
return generalRecommend.subList(0, RECOMMEND_UNIV_APPLY_INFO_NUM - alreadyPicked.size());
59+
int needed = RECOMMEND_UNIV_APPLY_INFO_NUM - alreadyPicked.size();
60+
return generalRecommend.subList(0, Math.min(needed, generalRecommend.size()));
6061
}
61-
6262
/*
6363
* 공통 추천 대학교를 불러온다.
6464
* */

src/test/java/com/example/solidconnection/university/service/GeneralUnivApplyInfoRecommendServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void setUp() {
4040
univApplyInfoFixture.그라츠공과대학_지원_정보();
4141
univApplyInfoFixture.린츠_카톨릭대학_지원_정보();
4242
univApplyInfoFixture.메이지대학_지원_정보();
43+
generalUnivApplyInfoRecommendService.init();
4344
}
4445

4546
@Test

src/test/java/com/example/solidconnection/university/service/UnivApplyInfoRecommendServiceTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void setUp() {
7272
univApplyInfoFixture.그라츠공과대학_지원_정보();
7373
univApplyInfoFixture.린츠_카톨릭대학_지원_정보();
7474
univApplyInfoFixture.메이지대학_지원_정보();
75+
generalUnivApplyInfoRecommendService.init();
7576
}
7677

7778
@Test
@@ -140,9 +141,10 @@ void setUp() {
140141
// then
141142
assertThat(response.recommendedUniversities())
142143
.hasSize(RECOMMEND_UNIV_APPLY_INFO_NUM)
143-
.allMatch(univ -> univ.koreanName() != null)
144-
.allMatch(univ -> univ.id() > 0)
145-
.allMatch(univ -> univ.term().equals("2024-1"));
144+
.containsExactlyInAnyOrderElementsOf(
145+
generalUnivApplyInfoRecommendService.getGeneralRecommends().stream()
146+
.map(UnivApplyInfoPreviewResponse::from).toList()
147+
);
146148
}
147149
@Test
148150
void 일반_추천_대학_지원_정보를_조회한다() {
@@ -152,7 +154,10 @@ void setUp() {
152154
// then
153155
assertThat(response.recommendedUniversities())
154156
.hasSize(RECOMMEND_UNIV_APPLY_INFO_NUM)
155-
.allMatch(univ -> univ.id() > 0)
156-
.allMatch(univ -> univ.koreanName() != null);
157+
.containsExactlyInAnyOrderElementsOf(
158+
generalUnivApplyInfoRecommendService.getGeneralRecommends().stream()
159+
.map(UnivApplyInfoPreviewResponse::from)
160+
.toList()
161+
);
157162
}
158163
}

0 commit comments

Comments
 (0)