-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationQueryDslRepository.java
More file actions
162 lines (153 loc) · 7.23 KB
/
NotificationQueryDslRepository.java
File metadata and controls
162 lines (153 loc) · 7.23 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
package com.chooz.notification.persistence;
import com.chooz.notification.application.dto.QTargetPostDto;
import com.chooz.notification.application.dto.QTargetUserDto;
import com.chooz.notification.application.dto.TargetPostDto;
import com.chooz.notification.application.dto.TargetUserDto;
import com.chooz.notification.application.web.dto.NotificationDto;
import com.chooz.notification.application.web.dto.NotificationRowDto;
import com.chooz.notification.application.web.dto.QNotificationRowDto;
import com.chooz.notification.application.web.dto.QTargetDto;
import com.chooz.notification.application.web.dto.TargetDto;
import com.chooz.notification.domain.Notification;
import com.chooz.notification.domain.QTarget;
import com.querydsl.core.BooleanBuilder;
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 java.util.Map;
import java.util.Optional;
import static com.chooz.comment.domain.QComment.comment;
import static com.chooz.notification.domain.QNotification.notification;
import static com.chooz.post.domain.QPost.post;
import static com.chooz.user.domain.QUser.user;
import static com.chooz.vote.domain.QVote.vote;
@Repository
@RequiredArgsConstructor
public class NotificationQueryDslRepository {
private final JPAQueryFactory queryFactory;
public Slice<NotificationDto> findNotifications(Long userId, Long cursor, Pageable pageable) {
List<NotificationRowDto> notificationRows = queryFactory
.select(new QNotificationRowDto(
notification.id,
notification.notificationType,
notification.profileUrl,
notification.title,
notification.content,
notification.imageUrl,
notification.isValid,
notification.isRead,
notification.eventAt
)
)
.from(notification)
.where(
notification.receiverId.eq(userId),
notification.isValid.eq(true),
cursor != null ? notification.id.lt(cursor) : null
)
.orderBy(notification.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
if(notificationRows.isEmpty()) {
return new SliceImpl<>(List.of(), pageable, false);
}
List<NotificationDto> notifications = findNotificationsWithTarget(notificationRows);
boolean hasNext = notifications.size() > pageable.getPageSize();
if (hasNext) notifications.removeLast();
return new SliceImpl<>(notifications, pageable, hasNext);
}
private List<NotificationDto> findNotificationsWithTarget(List<NotificationRowDto> notificationRows) {
QTarget target = QTarget.target;
List<Long> ids = notificationRows.stream().map(NotificationRowDto::id).toList();
Map<Long, List<TargetDto>> targetsByNotificationId = queryFactory
.from(notification)
.join(notification.targets, target)
.where(notification.id.in(ids))
.transform(com.querydsl.core.group.GroupBy.groupBy(notification.id).as(
com.querydsl.core.group.GroupBy.list(new QTargetDto(target.id, target.type))
));
return notificationRows.stream().map(
row -> new NotificationDto(
row,
targetsByNotificationId.getOrDefault(row.id(), List.of())
)).toList();
}
public Optional<TargetPostDto> findPostByCommentId(Long commentId) {
return Optional.ofNullable(
queryFactory.select(new QTargetPostDto(post.id, post.title, post.imageUrl))
.from(comment)
.join(post).on(post.id.eq(comment.postId), post.deleted.eq(false))
.where(comment.id.eq(commentId))
.limit(1)
.fetchFirst());
}
public Optional<TargetUserDto> findUserByCommentId(Long commentId) {
return Optional.ofNullable(
queryFactory.select(new QTargetUserDto(user.id, user.nickname, user.profileUrl))
.from(comment)
.join(user).on(user.id.eq(comment.userId))
.where(comment.id.eq(commentId))
.limit(1)
.fetchFirst());
}
public Optional<TargetUserDto> findUserById(Long userId) {
return Optional.ofNullable(
queryFactory.select(new QTargetUserDto(user.id, user.nickname, user.profileUrl))
.from(user)
.where(user.id.eq(userId))
.limit(1)
.fetchFirst());
}
public Optional<TargetUserDto> findUserByPostId(Long postId) {
return Optional.ofNullable(
queryFactory.select(new QTargetUserDto(user.id, user.nickname, user.profileUrl))
.from(user)
.join(post).on(user.id.eq(post.userId), post.deleted.eq(false))
.where(post.id.eq(postId))
.limit(1)
.fetchFirst());
}
public Optional<TargetPostDto> findPostById(Long postId) {
return Optional.ofNullable(
queryFactory.select(new QTargetPostDto(post.id, post.title, post.imageUrl))
.from(post)
.where(
post.id.eq(postId),
post.deleted.eq(false)
)
.limit(1)
.fetchFirst());
}
public boolean existsByDedupKey(Long receiverId, String dedupkey) {
Integer one = queryFactory.selectOne()
.from(notification)
.where(
notification.receiverId.eq(receiverId),
notification.dedupKey.eq(dedupkey)
).fetchFirst();
return one != null;
}
public List<Notification> findNotificationsByDedupKey(List<Notification> notifications) {
BooleanBuilder builder = new BooleanBuilder();
for (Notification n : notifications) {
builder.or(
notification.receiverId.eq(n.getReceiverId())
.and(notification.dedupKey.eq(n.getDedupKey()))
);
}
return queryFactory.selectFrom(notification)
.where(builder)
.fetch();
}
public List<TargetUserDto> findVoteUsersByPostId(Long postId) {
return queryFactory.select(new QTargetUserDto(user.id, user.nickname, user.profileUrl))
.from(user)
.join(vote).on(user.id.eq(vote.userId), vote.deleted.eq(false))
.join(post).on(post.id.eq(vote.postId), post.deleted.eq(false))
.fetch();
}
}