-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: 관련도 정렬을 제외한 리스트 검색 기능 구현 #99
Merged
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.listywave.common.util; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class StringUtils { | ||
|
||
public static boolean match(String source, String keyword) { | ||
return source.matches(".*" + Pattern.quote(keyword) + ".*"); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/listywave/list/application/domain/SortType.java
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.listywave.list.application.domain; | ||
|
||
public enum SortType { | ||
|
||
NEW, | ||
OLD, | ||
RELATED, | ||
COLLECTED, | ||
; | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/listywave/list/application/dto/response/ListSearchResponse.java
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.listywave.list.application.dto.response; | ||
|
||
import com.listywave.list.application.domain.Item; | ||
import com.listywave.list.application.domain.Lists; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record ListSearchResponse( | ||
List<ListInfo> resultLists, | ||
Long totalCount, | ||
Long cursorId, | ||
boolean hasNext | ||
) { | ||
|
||
public static ListSearchResponse of(java.util.List<Lists> lists, Long totalCount, Long cursorId, boolean hasNext) { | ||
return ListSearchResponse.builder() | ||
.resultLists(ListInfo.toList(lists)) | ||
.totalCount(totalCount) | ||
.cursorId(cursorId) | ||
.hasNext(hasNext) | ||
.build(); | ||
} | ||
} | ||
|
||
@Builder | ||
record ListInfo( | ||
Long id, | ||
String title, | ||
java.util.List<ItemInfo> items, | ||
boolean isPublic, | ||
String backgroundColor, | ||
LocalDateTime updatedDate, | ||
Long ownerId, | ||
String ownerNickname, | ||
String ownerProfileImageUrl | ||
) { | ||
|
||
public static List<ListInfo> toList(java.util.List<Lists> lists) { | ||
return lists.stream() | ||
.map(ListInfo::of) | ||
.toList(); | ||
} | ||
|
||
private static ListInfo of(Lists lists) { | ||
return ListInfo.builder() | ||
.id(lists.getId()) | ||
.title(lists.getTitle()) | ||
.items(ItemInfo.toList(lists.getItems())) | ||
.isPublic(lists.isPublic()) | ||
.backgroundColor(lists.getBackgroundColor()) | ||
.updatedDate(lists.getUpdatedDate()) | ||
.ownerId(lists.getUser().getId()) | ||
.ownerNickname(lists.getUser().getNickname()) | ||
.ownerProfileImageUrl(lists.getUser().getProfileImageUrl()) | ||
.build(); | ||
} | ||
} | ||
|
||
@Builder | ||
record ItemInfo( | ||
Long id, | ||
String title | ||
) { | ||
|
||
public static java.util.List<ItemInfo> toList(java.util.List<Item> items) { | ||
return items.stream() | ||
.map(ItemInfo::of) | ||
.toList(); | ||
} | ||
|
||
private static ItemInfo of(Item item) { | ||
return ItemInfo.builder() | ||
.id(item.getId()) | ||
.title(item.getTitle()) | ||
.build(); | ||
} | ||
} |
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,19 +1,25 @@ | ||
package com.listywave.list.application.service; | ||
|
||
import static com.listywave.list.application.domain.SortType.COLLECTED; | ||
import static com.listywave.list.application.domain.SortType.OLD; | ||
|
||
import com.listywave.auth.application.domain.JwtManager; | ||
import com.listywave.collaborator.application.domain.Collaborator; | ||
import com.listywave.collaborator.repository.CollaboratorRepository; | ||
import com.listywave.common.exception.CustomException; | ||
import com.listywave.common.exception.ErrorCode; | ||
import com.listywave.common.util.UserUtil; | ||
import com.listywave.image.application.service.ImageService; | ||
import com.listywave.list.application.domain.CategoryType; | ||
import com.listywave.list.application.domain.Comment; | ||
import com.listywave.list.application.domain.Item; | ||
import com.listywave.list.application.domain.Lists; | ||
import com.listywave.list.application.domain.SortType; | ||
import com.listywave.list.application.dto.ListCreateCommand; | ||
import com.listywave.list.application.dto.response.ListCreateResponse; | ||
import com.listywave.list.application.dto.response.ListDetailResponse; | ||
import com.listywave.list.application.dto.response.ListRecentResponse; | ||
import com.listywave.list.application.dto.response.ListSearchResponse; | ||
import com.listywave.list.application.dto.response.ListTrandingResponse; | ||
import com.listywave.list.presentation.dto.request.ItemCreateRequest; | ||
import com.listywave.list.repository.CommentRepository; | ||
|
@@ -192,4 +198,52 @@ public ListRecentResponse getRecentLists(String accessToken) { | |
private boolean isSignedIn(String accessToken) { | ||
return !accessToken.isBlank(); | ||
} | ||
|
||
// TODO: 관련도 순 추가 (List 일급 컬렉션 만들어서 Scoring 하는 방식) | ||
// TODO: 리팩터링 | ||
public ListSearchResponse search(String keyword, SortType sortType, CategoryType category, int size, Long cursorId) { | ||
List<Lists> all = listRepository.findAll(); | ||
|
||
List<Lists> filtered = all.stream() | ||
.filter(list -> list.isIncluded(category)) | ||
.filter(list -> list.isRelatedWith(keyword)) | ||
.sorted((list, other) -> { | ||
if (sortType.equals(OLD)) { | ||
return list.getUpdatedDate().compareTo(other.getUpdatedDate()); | ||
} | ||
if (sortType.equals(COLLECTED)) { | ||
return -(list.getCollectCount() - other.getCollectCount()); | ||
} | ||
return -(list.getUpdatedDate().compareTo(other.getUpdatedDate())); | ||
}) | ||
.toList(); | ||
|
||
List<Lists> result; | ||
if (cursorId == 0L) { | ||
if (filtered.size() >= size) { | ||
return ListSearchResponse.of(filtered.subList(0, size), (long) filtered.size(), filtered.get(size - 1).getId(), true); | ||
} | ||
return ListSearchResponse.of(filtered, (long) filtered.size(), filtered.get(filtered.size() - 1).getId(), false); | ||
} else { | ||
Lists cursorList = listRepository.getById(cursorId); | ||
|
||
int cursorIndex = filtered.indexOf(cursorList); | ||
int startIndex = cursorIndex + 1; | ||
int endIndex = cursorIndex + 1 + size; | ||
|
||
if (endIndex >= filtered.size()) { | ||
endIndex = filtered.size() - 1; | ||
} | ||
|
||
result = filtered.subList(startIndex, endIndex + 1); | ||
} | ||
|
||
int totalCount = filtered.size(); | ||
if (result.size() < size) { | ||
return ListSearchResponse.of(result, (long) totalCount, result.get(result.size() - 1).getId(), false); | ||
} | ||
boolean hasNext = result.size() > size; | ||
result = result.subList(0, size); | ||
return ListSearchResponse.of(result, (long) totalCount, result.get(result.size() - 1).getId(), hasNext); | ||
Comment on lines
+221
to
+247
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데이터를 한번에 가져와서 직접 페이징을 처리하신 걸로 보이는데 추후 무수한 데이터가 있을 경우 성능면에서 문제가 있을 거 같아 보이네요! 추후 query 부분에서 가져올때 처리하는게 더 좋아 보여요! |
||
} | ||
} |
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.
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.
해당 정렬은 query dsl 을 이용하여 동적 정렬을 이용하시는게 더 좋아보이네요
reference : https://velog.io/@seungho1216/Querydsl%EB%8F%99%EC%A0%81-sorting%EC%9D%84-%EC%9C%84%ED%95%9C-OrderSpecifier-%ED%81%B4%EB%9E%98%EC%8A%A4-%EA%B5%AC%ED%98%84