Skip to content

Commit

Permalink
#114 인덱스 설정 및 검색 테스트 코드 작성 Merge
Browse files Browse the repository at this point in the history
#114 인덱스 설정 및 검색 테스트 코드 작성
  • Loading branch information
Train0303 authored Oct 21, 2023
2 parents 346f144 + 380421c commit cc7a8be
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kakao.linknamu.bookmark.dto;

import lombok.Builder;

import java.util.List;

public record BookmarkSearchCondition(
Expand All @@ -9,4 +11,7 @@ public record BookmarkSearchCondition(
List<String> tags,
String workspaceName
) {

@Builder
public BookmarkSearchCondition{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"bookmark_link"
}
)
}
},
indexes = @Index(name = "idx__bookmark_name", columnList = "bookmark_name")
)
public class Bookmark extends AuditingEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;

import static com.kakao.linknamu.bookmark.entity.QBookmark.bookmark;
import static com.kakao.linknamu.category.entity.QCategory.category;
import static com.kakao.linknamu.workspace.entity.QWorkspace.workspace;
import static java.util.Objects.isNull;
import static org.springframework.util.StringUtils.hasText;

Expand All @@ -28,9 +30,10 @@ public Page<Bookmark> search(BookmarkSearchCondition condition, Long userId, Pag
JPAQuery<Bookmark> searchQuery = queryFactory
.select(bookmark)
.from(bookmark)
.join(bookmark.category, category)
.join(category.workspace, workspace).on(workspace.user.userId.eq(userId))
.where(
bookmark.category.workspace.user.userId.eq(userId),
bookmarkNameContains(condition.bookmarkName()),
bookmarkNameLike(condition.bookmarkName()),
bookmarkLinkContains(condition.bookmarkLink()),
bookmarkDescriptionContains(condition.bookmarkDescription()),
workspaceNameEq(condition.workspaceName())
Expand All @@ -45,8 +48,8 @@ public Page<Bookmark> search(BookmarkSearchCondition condition, Long userId, Pag
return PageableExecutionUtils.getPage(bookmarks, pageable, searchQuery::fetchCount);
}

private BooleanExpression bookmarkNameContains(String bookmarkName) {
return hasText(bookmarkName) ? bookmark.bookmarkName.contains(bookmarkName) : null;
private BooleanExpression bookmarkNameLike(String bookmarkName) {
return hasText(bookmarkName) ? bookmark.bookmarkName.like(bookmarkName + "%") : null;
}

private BooleanExpression bookmarkLinkContains(String bookmarkLink) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public class BookmarkSearchService {
private final BookmarkTagSearchService bookmarkTagSearchService;

public BookmarkSearchResponseDto bookmarkSearch(BookmarkSearchCondition condition, User user, Pageable pageable) {
Page<Bookmark> searchedBookmarks;
if (isNull(condition.tags()))
searchedBookmarks = bookmarkJPARepository.search(condition, user.getUserId(), pageable);
else searchedBookmarks = bookmarkTagSearchService.search(condition, user.getUserId(), pageable);


List<BookmarkSearchResponseDto.BookmarkContentDto> bookmarkContentDtos = new ArrayList<>();
for (Bookmark resultBookmark : searchedBookmarks) {
List<Tag> bookmarkTags = bookmarkTagSearchService.findTagsByBookmarkId(resultBookmark.getBookmarkId());
bookmarkContentDtos.add(BookmarkSearchResponseDto.BookmarkContentDto.of(resultBookmark, bookmarkTags));
Page<Bookmark> bookmarks;
if (isNull(condition.tags())) bookmarks = bookmarkJPARepository.search(condition, user.getUserId(), pageable);
else bookmarks = bookmarkTagSearchService.search(condition, user.getUserId(), pageable);
return BookmarkSearchResponseDto.of(PageInfoDto.of(bookmarks), getBookmarkContentDtos(bookmarks));
}

private List<BookmarkSearchResponseDto.BookmarkContentDto> getBookmarkContentDtos(Page<Bookmark> bookmarks) {
List<BookmarkSearchResponseDto.BookmarkContentDto> responseDto = new ArrayList<>();
for (Bookmark bookmark : bookmarks) {
List<Tag> tags = bookmarkTagSearchService.findTagsByBookmarkId(bookmark.getBookmarkId());
responseDto.add(BookmarkSearchResponseDto.BookmarkContentDto.of(bookmark, tags));
}
return BookmarkSearchResponseDto.of(PageInfoDto.of(searchedBookmarks), bookmarkContentDtos);
return responseDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static com.kakao.linknamu.bookmarkTag.entity.QBookmarkTag.bookmarkTag;
import static com.kakao.linknamu.bookmark.entity.QBookmark.bookmark;
import static com.kakao.linknamu.tag.entity.QTag.tag;
import static java.util.Objects.isNull;
import static org.springframework.util.StringUtils.hasText;

Expand All @@ -34,9 +35,9 @@ public Page<Bookmark> search(BookmarkSearchCondition condition, Long userId, Pag
.select(bookmarkTag.bookmark)
.from(bookmarkTag)
.join(bookmarkTag.bookmark, bookmark)
.join(bookmarkTag.tag, tag).on(tag.user.userId.eq(userId))
.where(
bookmark.category.workspace.user.userId.eq(userId),
bookmarkNameContains(condition.bookmarkName()),
bookmarkNameLike(condition.bookmarkName()),
bookmarkLinkContains(condition.bookmarkLink()),
bookmarkDescriptionContains(condition.bookmarkDescription()),
workspaceEq(condition.workspaceName()),
Expand All @@ -55,8 +56,8 @@ public Page<Bookmark> search(BookmarkSearchCondition condition, Long userId, Pag
return PageableExecutionUtils.getPage(bookmarks, pageable, searchQuery::fetchCount);
}

private BooleanExpression bookmarkNameContains(String bookmarkName) {
return hasText(bookmarkName) ? bookmark.bookmarkName.contains(bookmarkName) : null;
private BooleanExpression bookmarkNameLike(String bookmarkName) {
return hasText(bookmarkName) ? bookmark.bookmarkName.like(bookmarkName + "%") : null;
}

private BooleanExpression bookmarkLinkContains(String bookmarkLink) {
Expand Down
Loading

0 comments on commit cc7a8be

Please sign in to comment.