Skip to content
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

[BE] feat: 축제 대학교 검색 기능 보완 및 정렬(#926) #927

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.festago.festival.application;

import static com.festago.festival.repository.FestivalFilter.END;
import static com.festago.festival.repository.FestivalFilter.PLANNED;
import static com.festago.festival.repository.FestivalFilter.PROGRESS;

import com.festago.festival.dto.FestivalSearchV1Response;
import com.festago.festival.repository.ArtistFestivalSearchV1QueryDslRepository;
import com.festago.festival.repository.FestivalFilter;
import com.festago.festival.repository.SchoolFestivalSearchV1QueryDslRepository;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,16 +25,65 @@
@RequiredArgsConstructor
public class FestivalSearchV1QueryService {

private static final Pattern SCHOOL_PATTERN = Pattern.compile(".*대(학교)?$");

private final ArtistFestivalSearchV1QueryDslRepository artistFestivalSearchV1QueryDslRepository;
private final SchoolFestivalSearchV1QueryDslRepository schoolFestivalSearchV1QueryDslRepository;

public List<FestivalSearchV1Response> search(String keyword) {
Matcher schoolMatcher = SCHOOL_PATTERN.matcher(keyword);
if (schoolMatcher.matches()) {
return schoolFestivalSearchV1QueryDslRepository.executeSearch(keyword);
return sortFestival(findFestivals(keyword));
}

private List<FestivalSearchV1Response> findFestivals(String keyword) {
if (artistFestivalSearchV1QueryDslRepository.existsByName(keyword)) {
return artistFestivalSearchV1QueryDslRepository.executeSearch(keyword);
}
return schoolFestivalSearchV1QueryDslRepository.executeSearch(keyword);
}

private List<FestivalSearchV1Response> sortFestival(List<FestivalSearchV1Response> festivals) {
Map<FestivalFilter, List<FestivalSearchV1Response>> festivalByStatus = divideByStatus(festivals);
List<FestivalSearchV1Response> result = new ArrayList<>();
for (FestivalFilter festivalFilter : determineFestivalOrder()) {
List<FestivalSearchV1Response> festivalsByFilter = festivalByStatus.getOrDefault(festivalFilter,
Collections.emptyList());
sortByStatus(festivalFilter, festivalsByFilter);
result.addAll(festivalsByFilter);
}
return result;
}

private Map<FestivalFilter, List<FestivalSearchV1Response>> divideByStatus(
List<FestivalSearchV1Response> festivals
) {
return festivals.stream()
.collect(Collectors.groupingBy(
this::determineStatus,
() -> new EnumMap<>(FestivalFilter.class),
Collectors.toList()
));
}

private FestivalFilter determineStatus(FestivalSearchV1Response festival) {
LocalDate now = LocalDate.now();
if (now.isAfter(festival.endDate())) {
return END;
}
if (now.isBefore(festival.startDate())) {
return PLANNED;
}
return PROGRESS;
}

private List<FestivalFilter> determineFestivalOrder() {
return List.of(PROGRESS, PLANNED, END);
}
Comment on lines +76 to +78
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 함수 대신 FestivalFilter.values()를 사용할 수 있을 것 같은데..
사용하지 않으신 이유가 있을까요?
만약, 있으시다면 해당 리스트를 static으로 만들어 사용해도 될 것 같네요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 values를 쓸까 고민했는데 FestivalFilter에서 적힌 순서가 정렬 순서라는게 명시적이지 않은 것이라 생각하여 후에 순서를 변경할 수 있다 생각했습니다!! 그래서 정렬이 필요한 곳에서 정렬 조건을 정의하고자 순서를 명시해줬네요


private void sortByStatus(
FestivalFilter status,
List<FestivalSearchV1Response> festivals) {

switch (status) {
case END -> festivals.sort(Comparator.comparing(FestivalSearchV1Response::endDate).reversed());
case PROGRESS, PLANNED -> festivals.sort(Comparator.comparing(FestivalSearchV1Response::startDate));
}
return artistFestivalSearchV1QueryDslRepository.executeSearch(keyword);
}
Comment on lines +80 to 88
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍
추가로 .thenComparing(FestivalSearchV1Response::id) 추가해서 일정이 같은 축제의 경우 두 번째 정렬 조건을 추가해줄 수 있을 것 같네요!

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,38 @@ public ArtistFestivalSearchV1QueryDslRepository() {
}

public List<FestivalSearchV1Response> executeSearch(String keyword) {
return select(new QFestivalSearchV1Response(
festival.id,
festival.name,
festival.festivalDuration.startDate,
festival.festivalDuration.endDate,
festival.posterImageUrl,
festivalQueryInfo.artistInfo)
)
.from(artist)
.innerJoin(stageArtist).on(stageArtist.artistId.eq(artist.id))
.innerJoin(stage).on(stage.id.eq(stageArtist.stageId))
.innerJoin(festival).on(festival.id.eq(stage.festival.id))
.innerJoin(festivalQueryInfo).on(festival.id.eq(festivalQueryInfo.festivalId))
.where(getBooleanExpressionByKeyword(keyword))
.fetch();
}

private BooleanExpression getBooleanExpressionByKeyword(String keyword) {
int keywordLength = keyword.length();
if (keywordLength == 0) {
throw new BadRequestException(ErrorCode.INVALID_KEYWORD);
}
if (keywordLength == 1) {
return searchByEqual(keyword);
return artist.name.eq(keyword);
}
return searchByLike(keyword);
}

private List<FestivalSearchV1Response> searchByEqual(String keyword) {
return searchByExpression(artist.name.eq(keyword));
}

private List<FestivalSearchV1Response> searchByExpression(BooleanExpression expression) {
return select(
new QFestivalSearchV1Response(
festival.id,
festival.name,
festival.festivalDuration.startDate,
festival.festivalDuration.endDate,
festival.posterImageUrl,
festivalQueryInfo.artistInfo))
.from(artist)
.innerJoin(stageArtist).on(expression.and(stageArtist.artistId.eq(artist.id)))
.innerJoin(stage).on(stage.id.eq(stageArtist.stageId))
.innerJoin(festival).on(festival.id.eq(stage.festival.id))
.innerJoin(festivalQueryInfo).on(festival.id.eq(festivalQueryInfo.festivalId))
.where(expression)
.fetch();
return artist.name.contains(keyword);
}

private List<FestivalSearchV1Response> searchByLike(String keyword) {
return searchByExpression(artist.name.contains(keyword));
public boolean existsByName(String keyword) {
return !selectFrom(artist)
.where(getBooleanExpressionByKeyword(keyword))
.fetch()
.isEmpty();
}
}
Loading
Loading