-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostQueryDslRepository.java
More file actions
191 lines (172 loc) · 6.65 KB
/
PostQueryDslRepository.java
File metadata and controls
191 lines (172 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.chooz.post.persistence;
import com.chooz.post.application.dto.PostWithVoteCount;
import com.chooz.post.application.dto.QFeedDto;
import com.chooz.post.application.dto.QPostWithVoteCount;
import com.chooz.post.domain.Post;
import com.chooz.post.application.dto.FeedDto;
import com.chooz.post.domain.Scope;
import com.chooz.user.domain.QUser;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Repository;
import java.util.List;
import static com.chooz.comment.domain.QComment.comment;
import static com.chooz.post.domain.QPost.*;
import static com.chooz.user.domain.QUser.*;
import static com.chooz.vote.domain.QVote.vote;
@Repository
@RequiredArgsConstructor
public class PostQueryDslRepository {
private final JPAQueryFactory queryFactory;
Slice<Post> findByUserId(Long userId, Long postId, Pageable pageable) {
List<Post> results = queryFactory.selectFrom(post)
.where(
post.userId.eq(userId),
cursor(postId, post.id),
post.deleted.isFalse()
)
.orderBy(post.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
boolean hasNext = isHasNext(pageable, results);
if (hasNext) {
results.removeLast();
}
return new SliceImpl<>(results, pageable, hasNext);
}
private Predicate cursor(Long cursor, NumberPath<Long> id) {
return cursor != null ? id.lt(cursor) : null;
}
private boolean isHasNext(Pageable pageable, List<?> results) {
return results.size() > pageable.getPageSize();
}
/**
* 피드 관련 데이터 조회
* @param postId
* @param pageable
* @return
*/
public Slice<FeedDto> findFeed(Long postId, Pageable pageable) {
List<FeedDto> results = queryFactory
.select(new QFeedDto(
post.id,
post.status,
post.title,
post.imageUrl,
post.userId,
user.nickname,
user.profileUrl,
JPAExpressions
.select(vote.userId.countDistinct())
.from(vote)
.where(
vote.postId.eq(post.id),
vote.deleted.isFalse()
),
JPAExpressions
.select(comment.count())
.from(comment)
.where(
comment.postId.eq(post.id),
comment.deleted.isFalse()
),
post.createdAt
))
.from(post)
.innerJoin(user).on(post.userId.eq(user.id))
.where(
post.deleted.isFalse(),
post.pollOption.scope.eq(Scope.PUBLIC),
cursor(postId, post.id),
post.deleted.isFalse()
)
.orderBy(post.createdAt.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
boolean hasNext = isHasNext(pageable, results);
if (hasNext) {
results.removeLast();
}
return new SliceImpl<>(results, pageable, hasNext);
}
/**
* 유저가 작성한 게시글 리스트 조회
* @param userId
* @param postId
* @param pageable
* @return
*/
public Slice<PostWithVoteCount> findPostsWithVoteCountByUserId(Long userId, Long postId, Pageable pageable) {
List<PostWithVoteCount> results = queryFactory
.select(new QPostWithVoteCount(
post,
JPAExpressions
.select(vote.userId.countDistinct())
.from(vote)
.where(
vote.postId.eq(post.id),
vote.deleted.isFalse()
)
))
.from(post)
.where(
post.userId.eq(userId),
cursor(postId, post.id),
post.deleted.isFalse()
)
.orderBy(post.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
boolean hasNext = isHasNext(pageable, results);
if (hasNext) {
results.removeLast();
}
return new SliceImpl<>(results, pageable, hasNext);
}
/**
* 유저가 투표한 게시글 리스트 조회
* @param userId
* @param postId
* @param pageable
* @return
*/
public Slice<PostWithVoteCount> findVotedPostsWithVoteCount(Long userId, Long postId, Pageable pageable) {
List<PostWithVoteCount> results = queryFactory
.select(new QPostWithVoteCount(
post,
JPAExpressions
.select(vote.userId.count())
.from(vote)
.where(
vote.postId.eq(post.id),
vote.deleted.isFalse()
)
))
.from(post)
.where(
post.id.in(
JPAExpressions
.select(vote.postId)
.from(vote)
.where(vote.userId.eq(userId))
),
cursor(postId, post.id),
post.deleted.isFalse()
)
.orderBy(post.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
boolean hasNext = isHasNext(pageable, results);
if (hasNext) {
results.removeLast();
}
return new SliceImpl<>(results, pageable, hasNext);
}
}