|
7 | 7 | import static org.websoso.WSSServer.domain.common.ReadStatus.WATCHED;
|
8 | 8 | import static org.websoso.WSSServer.domain.common.ReadStatus.WATCHING;
|
9 | 9 |
|
10 |
| -import com.querydsl.core.types.OrderSpecifier; |
11 | 10 | import com.querydsl.core.types.Projections;
|
12 |
| -import com.querydsl.core.types.dsl.BooleanExpression; |
| 11 | +import com.querydsl.jpa.impl.JPAQuery; |
13 | 12 | import com.querydsl.jpa.impl.JPAQueryFactory;
|
14 | 13 | import java.time.LocalDate;
|
15 | 14 | import java.util.List;
|
| 15 | +import java.util.Optional; |
| 16 | +import java.util.stream.Collectors; |
16 | 17 | import lombok.RequiredArgsConstructor;
|
17 | 18 | import org.springframework.data.domain.Pageable;
|
18 | 19 | import org.springframework.stereotype.Repository;
|
19 | 20 | import org.websoso.WSSServer.domain.Genre;
|
20 | 21 | import org.websoso.WSSServer.domain.Novel;
|
21 |
| -import org.websoso.WSSServer.domain.User; |
22 | 22 | import org.websoso.WSSServer.domain.UserNovel;
|
23 | 23 | import org.websoso.WSSServer.domain.common.ReadStatus;
|
24 | 24 | import org.websoso.WSSServer.dto.user.UserNovelCountGetResponse;
|
@@ -63,66 +63,6 @@ public UserNovelCountGetResponse findUserNovelStatistics(Long userId) {
|
63 | 63 | .fetchOne();
|
64 | 64 | }
|
65 | 65 |
|
66 |
| - @Override |
67 |
| - public List<UserNovel> findUserNovelsByNoOffsetPagination(User owner, Long lastUserNovelId, int size, |
68 |
| - String readStatus, String sortType) { |
69 |
| - return jpaQueryFactory |
70 |
| - .selectFrom(userNovel) |
71 |
| - .where( |
72 |
| - userNovel.user.eq(owner), |
73 |
| - generateReadStatusCondition(readStatus), |
74 |
| - compareFeedId(lastUserNovelId, sortType) |
75 |
| - ) |
76 |
| - .orderBy(getSortOrder(sortType)) |
77 |
| - .limit(size) |
78 |
| - .fetch(); |
79 |
| - } |
80 |
| - |
81 |
| - private BooleanExpression compareFeedId(Long lastUserNovelId, String sortType) { |
82 |
| - if (lastUserNovelId == 0) { |
83 |
| - return null; |
84 |
| - } |
85 |
| - |
86 |
| - // TODO 잘못된 sortType이 오는 경우 default null로 return이 아닌 예외 처리 |
87 |
| - if ("NEWEST".equalsIgnoreCase(sortType)) { |
88 |
| - return userNovel.userNovelId.lt(lastUserNovelId); |
89 |
| - } else if ("OLDEST".equalsIgnoreCase(sortType)) { |
90 |
| - return userNovel.userNovelId.gt(lastUserNovelId); |
91 |
| - } |
92 |
| - |
93 |
| - return null; |
94 |
| - } |
95 |
| - |
96 |
| - private BooleanExpression generateReadStatusCondition(String readStatus) { |
97 |
| - // TODO 잘못된 readStatus가 오는 경우 예외 처리 |
98 |
| - if (readStatus.equals("INTEREST")) { |
99 |
| - return userNovel.isInterest.isTrue(); |
100 |
| - } else { |
101 |
| - ReadStatus status = ReadStatus.valueOf(readStatus); |
102 |
| - return userNovel.status.eq(status); |
103 |
| - } |
104 |
| - } |
105 |
| - |
106 |
| - private OrderSpecifier<?> getSortOrder(String sortType) { |
107 |
| - // TODO 잘못된 sortType이 오는 경우 default desc가 아닌 예외 처리 |
108 |
| - if ("NEWEST".equalsIgnoreCase(sortType)) { |
109 |
| - return userNovel.userNovelId.desc(); |
110 |
| - } else if ("OLDEST".equalsIgnoreCase(sortType)) { |
111 |
| - return userNovel.userNovelId.asc(); |
112 |
| - } |
113 |
| - return userNovel.userNovelId.desc(); |
114 |
| - } |
115 |
| - |
116 |
| - @Override |
117 |
| - public List<UserNovel> findByUserAndReadStatus(User owner, String readStatus) { |
118 |
| - return jpaQueryFactory |
119 |
| - .selectFrom(userNovel) |
120 |
| - .where(userNovel.user.eq(owner), |
121 |
| - generateReadStatusCondition(readStatus) |
122 |
| - ) |
123 |
| - .fetch(); |
124 |
| - } |
125 |
| - |
126 | 66 | public List<Long> findTodayPopularNovelsId(Pageable pageable) {
|
127 | 67 | LocalDate sevenDaysAgo = LocalDate.now().minusDays(7);
|
128 | 68 |
|
@@ -156,4 +96,63 @@ public List<Novel> findTasteNovels(List<Genre> preferGenres) {
|
156 | 96 | .limit(10)
|
157 | 97 | .toList();
|
158 | 98 | }
|
| 99 | + |
| 100 | + @Override |
| 101 | + public List<UserNovel> findFilteredUserNovels(Long userId, Boolean isInterest, List<String> readStatuses, |
| 102 | + List<String> attractivePoints, Float novelRating, String query, |
| 103 | + Long lastUserNovelId, int size, boolean isAscending) { |
| 104 | + JPAQuery<UserNovel> queryBuilder = jpaQueryFactory |
| 105 | + .selectFrom(userNovel) |
| 106 | + .join(userNovel.novel, novel).fetchJoin() |
| 107 | + .where(userNovel.user.userId.eq(userId)); |
| 108 | + |
| 109 | + applyFilters(queryBuilder, isInterest, readStatuses, attractivePoints, novelRating, query); |
| 110 | + |
| 111 | + queryBuilder.where(isAscending |
| 112 | + ? userNovel.userNovelId.gt(lastUserNovelId) |
| 113 | + : userNovel.userNovelId.lt(lastUserNovelId)); |
| 114 | + queryBuilder.orderBy(isAscending |
| 115 | + ? userNovel.userNovelId.asc() |
| 116 | + : userNovel.userNovelId.desc()); |
| 117 | + return queryBuilder.limit(size).fetch(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Long countByUserIdAndFilters(Long userId, Boolean isInterest, List<String> readStatuses, |
| 122 | + List<String> attractivePoints, Float novelRating, String query) { |
| 123 | + JPAQuery<Long> queryBuilder = jpaQueryFactory |
| 124 | + .select(userNovel.count()) |
| 125 | + .from(userNovel) |
| 126 | + .join(userNovel.novel, novel) |
| 127 | + .where(userNovel.user.userId.eq(userId)); |
| 128 | + |
| 129 | + applyFilters(queryBuilder, isInterest, readStatuses, attractivePoints, novelRating, query); |
| 130 | + |
| 131 | + return queryBuilder.fetchOne(); |
| 132 | + } |
| 133 | + |
| 134 | + private <T> void applyFilters(JPAQuery<T> queryBuilder, Boolean isInterest, List<String> readStatuses, |
| 135 | + List<String> attractivePoints, Float novelRating, String query) { |
| 136 | + Optional.ofNullable(isInterest) |
| 137 | + .ifPresent(interest -> queryBuilder.where(userNovel.isInterest.eq(interest))); |
| 138 | + |
| 139 | + Optional.ofNullable(readStatuses) |
| 140 | + .filter(list -> !list.isEmpty()) |
| 141 | + .map(list -> list.stream().map(String::toUpperCase).map(ReadStatus::valueOf) |
| 142 | + .collect(Collectors.toList())) |
| 143 | + .ifPresent(statusEnums -> queryBuilder.where(userNovel.status.in(statusEnums))); |
| 144 | + |
| 145 | + Optional.ofNullable(attractivePoints) |
| 146 | + .filter(list -> !list.isEmpty()) |
| 147 | + .ifPresent(points -> queryBuilder.where( |
| 148 | + userNovel.userNovelAttractivePoints.any().attractivePoint.attractivePointName.in(points))); |
| 149 | + |
| 150 | + Optional.ofNullable(novelRating) |
| 151 | + .ifPresent(rating -> queryBuilder.where(userNovel.userNovelRating.goe(rating))); |
| 152 | + |
| 153 | + Optional.ofNullable(query) |
| 154 | + .filter(q -> !q.isBlank()) |
| 155 | + .ifPresent(q -> queryBuilder.where( |
| 156 | + novel.title.containsIgnoreCase(q).or(novel.author.containsIgnoreCase(q)))); |
| 157 | + } |
159 | 158 | }
|
0 commit comments