Skip to content

Commit

Permalink
Merge pull request #178 from teamterning/refactor/#177
Browse files Browse the repository at this point in the history
[♻️ refactor] N+1 문제 해결 및 HomeServiceImpl 성능 개선
  • Loading branch information
junggyo1020 authored Dec 11, 2024
2 parents 6437e37 + 08bbfe5 commit e413bf1
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,6 @@ public Page<InternshipAnnouncement> searchInternshipAnnouncement(String keyword,
return PageableExecutionUtils.getPage(internshipAnnouncements, pageable, count::fetchOne);
}

@Override
public List<Tuple> findFilteredInternshipsWithScrapInfo(User user, String sortBy, int startYear, int startMonth) {
return jpaQueryFactory
.select(
internshipAnnouncement,
scrap.id,
scrap.color
)
.from(internshipAnnouncement)
.leftJoin(internshipAnnouncement.scraps, scrap).on(scrap.user.eq(user)) // Fetch Join
.where(
getGraduatingFilter(user),
getWorkingPeriodFilter(user),
getStartDateFilter(startYear, startMonth)
)
.orderBy(
sortAnnouncementsByDeadline().asc(),
getSortOrder(sortBy)
)
.fetch();
}

private boolean isPureEnglish(String summonerName) {
//공백은 무시
Expand Down Expand Up @@ -129,7 +108,24 @@ private OrderSpecifier createOrderSpecifier(String sortBy) {
};
}


@Override
public List<Tuple> findFilteredInternshipsWithScrapInfo(User user, String sortBy, int startYear, int startMonth){
return jpaQueryFactory
.select(internshipAnnouncement, scrap.id, scrap.color) // tuple -> Scrap 정보 한번에 불러오기
.from(internshipAnnouncement)
.leftJoin(internshipAnnouncement.scraps, scrap).on(scrap.user.eq(user))
.where(
getGraduatingFilter(user),
getWorkingPeriodFilter(user),
getStartDateFilter(startYear, startMonth)
)
.orderBy(
sortAnnouncementsByDeadline().asc(),
getSortOrder(sortBy)
)
.fetch();
}

private BooleanExpression getGraduatingFilter(User user){
if(user.getFilter().getGrade() != Grade.SENIOR){
return internshipAnnouncement.isGraduating.isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.terning.terningserver.domain.InternshipAnnouncement;
import org.terning.terningserver.domain.User;
import org.terning.terningserver.domain.enums.Color;
import org.terning.terningserver.dto.user.response.HomeAnnouncementsResponseDto;
import org.terning.terningserver.dto.user.response.HomeResponseDto;
import org.terning.terningserver.exception.CustomException;
Expand All @@ -32,20 +33,29 @@ public HomeAnnouncementsResponseDto getAnnouncements(Long userId, String sortBy,
() -> new CustomException(ErrorMessage.NOT_FOUND_USER_EXCEPTION)
);

// 유저의 필터 정보가 없는 경우
if(user.getFilter() == null){
return HomeAnnouncementsResponseDto.of(0,List.of());
}

List<Tuple> results = internshipRepository.findFilteredInternshipsWithScrapInfo(user, sortBy, startYear, startMonth);
List<Tuple> announcements = internshipRepository.findFilteredInternshipsWithScrapInfo(user, sortBy, startYear, startMonth);

List<HomeResponseDto> responseDtos = results.stream()
// 해당하는 공고가 없는 경우
if(announcements.isEmpty()){
return HomeAnnouncementsResponseDto.of(0, List.of());
}

List<HomeResponseDto> responseDtos = announcements.stream()
.map(tuple -> {
InternshipAnnouncement announcement = tuple.get(internshipAnnouncement);
Long scrapId = tuple.get(scrap.id);
String color = tuple.get(scrap.color.stringValue());
Color color = tuple.get(scrap.color);
boolean isScrapped = (scrapId != null); // 스크랩 여부

// scrap 하지 않은 경우 color는 지정되지 않아야 한다.
String colorValue = (isScrapped && color != null) ? color.getColorValue() : null;

boolean isScrapped = isScrapped(scrapId);
return HomeResponseDto.of(announcement, isScrapped, color);
return HomeResponseDto.of(announcement, isScrapped, colorValue);
})
.toList();

Expand Down
Loading

0 comments on commit e413bf1

Please sign in to comment.