Skip to content

Commit

Permalink
Merge pull request #175 from dnd-side-project/feature/#174
Browse files Browse the repository at this point in the history
Add category filter to bookmark list
  • Loading branch information
miraexhoi authored Sep 30, 2024
2 parents 4d634bc + 1789bdd commit 81f97ce
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public List<BookmarkWordInfoDto> findAllBy(BookmarkConditionInfoDto dto) {
Account account = findAccount(dto.email());
BookmarkConditionDto bookmarkConditionDto = BookmarkRepositoryMapper.of(
account.getId(),
dto.category(),
dto.lastBookmarkId(),
dto.pageable()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static List<BookmarkWordInfoDto> from(List<BookmarkWordDto> dtos) {
.toList();
}

public static BookmarkConditionInfoDto of(String email, Long lastBookmarkId, Pageable pageable) {
return new BookmarkConditionInfoDto(email, lastBookmarkId, pageable);
public static BookmarkConditionInfoDto of(String email, String category, Long lastBookmarkId, Pageable pageable) {
return new BookmarkConditionInfoDto(email, category, lastBookmarkId, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.springframework.data.domain.Pageable;

public record BookmarkConditionInfoDto(String email, Long lastBookmarkId, Pageable pageable) {
public record BookmarkConditionInfoDto(String email, String category, Long lastBookmarkId, Pageable pageable) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.dnd.spaced.domain.bookmark.domain.Bookmark;
import com.dnd.spaced.domain.bookmark.domain.repository.dto.request.BookmarkConditionDto;
import com.dnd.spaced.domain.bookmark.domain.repository.dto.response.BookmarkWordDto;
import com.dnd.spaced.domain.word.domain.Category;
import com.dnd.spaced.domain.word.domain.exception.InvalidCategoryException;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -20,6 +22,8 @@
@RequiredArgsConstructor
public class QuerydslBookmarkRepository implements BookmarkRepository {

private static final String IGNORE_CATEGORY = "전체 실무";

private final JPAQueryFactory queryFactory;
private final BookmarkCrudRepository bookmarkCrudRepository;

Expand Down Expand Up @@ -66,7 +70,12 @@ public List<BookmarkWordDto> findAllBy(BookmarkConditionDto dto) {
.from(word)
.leftJoin(bookmark).on(word.id.eq(bookmark.wordId), bookmark.accountId.eq(dto.accountId()))
.where(lastBookmarkIdLt(dto.lastBookmarkId()))
.where(
categoryEq(dto.category()),
lastBookmarkIdLt(dto.lastBookmarkId())
)
.orderBy(bookmark.id.desc())
.limit(dto.pageable().getPageSize())
.fetch();
}

Expand All @@ -77,4 +86,18 @@ private BooleanExpression lastBookmarkIdLt(Long lastBookmarkId) {

return bookmark.id.lt(lastBookmarkId);
}

private BooleanExpression categoryEq(String category) {
if (category == null || IGNORE_CATEGORY.equals(category)) {
return null;
}

try {
Category categoryEnum = Category.findBy(category);

return word.category.eq(categoryEnum);
} catch (InvalidCategoryException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BookmarkRepositoryMapper {

public static BookmarkConditionDto of(Long accountId, Long lastBookmarkId, Pageable pageable) {
return new BookmarkConditionDto(accountId, lastBookmarkId, pageable);
public static BookmarkConditionDto of(Long accountId, String category, Long lastBookmarkId, Pageable pageable) {
return new BookmarkConditionDto(accountId, category, lastBookmarkId, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.springframework.data.domain.Pageable;

public record BookmarkConditionDto(Long accountId, Long lastBookmarkId, Pageable pageable) {
public record BookmarkConditionDto(Long accountId, String category, Long lastBookmarkId, Pageable pageable) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ResponseEntity<BookmarkWordResponse> findAllBy(
) {
BookmarkConditionInfoDto dto = BookmarkServiceMapper.of(
accountInfo.email(),
request.category(),
request.lastBookmarkId(),
pageable
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ResponseEntity<Void> processBookmark(

@Operation(summary = "북마크한 용어 목록 조회", description = "북마크한 용어 목록 조회 API")
@Parameters({
@Parameter(name = "category", description = "용어 카테고리", schema = @Schema(defaultValue = "전체 실무", allowableValues = {"전체 실무", "비즈니스", "개발", "디자인"}, requiredMode = RequiredMode.NOT_REQUIRED)),
@Parameter(name = "lastBookmarkId", description = "마지막으로 조회한 북마크 ID", schema = @Schema(requiredMode = RequiredMode.NOT_REQUIRED)),
@Parameter(name = "size", description = "한 페이지에 조회 가능한 용어 개수", schema = @Schema(defaultValue = "15", requiredMode = RequiredMode.NOT_REQUIRED)),
@Parameter(name = "sortBy", description = "정렬 기준(등록순 고정)", schema = @Schema(defaultValue = "id", allowableValues = "id", requiredMode = RequiredMode.NOT_REQUIRED))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package com.dnd.spaced.domain.bookmark.presentation.dto.request;

public record BookmarkConditionRequest(Long lastBookmarkId) {
import io.swagger.v3.oas.annotations.media.Schema;

public record BookmarkConditionRequest(
@Schema(
description = "용어 카테고리",
allowableValues = {"전체 실무", "디자인", "개발", "비즈니스"},
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
String category,

@Schema(description = "마지막으로 조회한 북마크 Id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
Long lastBookmarkId) {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.dnd.spaced.domain.bookmark.presentation.dto.response;

import com.dnd.spaced.domain.bookmark.application.dto.response.BookmarkWordInfoDto;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

public record BookmarkWordResponse(List<BookmarkWordInfoResponse> words, Long lastBookmarkId) {
public record BookmarkWordResponse(

@Schema(description = "용어 정보")
List<BookmarkWordInfoResponse> words,

@Schema(description = "마지막으로 조회한 용어 이름", nullable = true)
Long lastBookmarkId) {

public static BookmarkWordResponse from(List<BookmarkWordInfoDto> dtos) {
if (dtos.isEmpty()) {
Expand Down

0 comments on commit 81f97ce

Please sign in to comment.