-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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); | ||
} | ||
|
||
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
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. 👍👍 |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
해당 함수 대신
FestivalFilter.values()
를 사용할 수 있을 것 같은데..사용하지 않으신 이유가 있을까요?
만약, 있으시다면 해당 리스트를 static으로 만들어 사용해도 될 것 같네요!
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.
이거 values를 쓸까 고민했는데 FestivalFilter에서 적힌 순서가 정렬 순서라는게 명시적이지 않은 것이라 생각하여 후에 순서를 변경할 수 있다 생각했습니다!! 그래서 정렬이 필요한 곳에서 정렬 조건을 정의하고자 순서를 명시해줬네요