-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostQueryService.java
More file actions
131 lines (114 loc) · 5.41 KB
/
PostQueryService.java
File metadata and controls
131 lines (114 loc) · 5.41 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
package com.chooz.post.application;
import com.chooz.comment.domain.CommentRepository;
import com.chooz.common.dto.CursorBasePaginatedResponse;
import com.chooz.common.exception.BadRequestException;
import com.chooz.common.exception.ErrorCode;
import com.chooz.post.application.dto.FeedDto;
import com.chooz.post.domain.PollChoice;
import com.chooz.post.domain.Post;
import com.chooz.post.domain.PostRepository;
import com.chooz.post.presentation.dto.AuthorDto;
import com.chooz.post.presentation.dto.FeedResponse;
import com.chooz.post.presentation.dto.MyPagePostResponse;
import com.chooz.post.presentation.dto.PollChoiceVoteResponse;
import com.chooz.post.presentation.dto.PostResponse;
import com.chooz.post.presentation.dto.UpdatePostResponse;
import com.chooz.user.domain.User;
import com.chooz.user.domain.UserRepository;
import com.chooz.vote.domain.Vote;
import com.chooz.vote.domain.VoteRepository;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class PostQueryService {
private final PostRepository postRepository;
private final UserRepository userRepository;
private final VoteRepository voteRepository;
private final CommentRepository commentRepository;
private final MyPagePostManager myPagePostManager;
public PostResponse findByShareUrl(Long userId, String shareUrl) {
Post post = postRepository.findByShareUrlFetchPollChoices(shareUrl)
.orElseThrow(() -> new BadRequestException(ErrorCode.POST_NOT_FOUND));
return createPostResponse(userId, post);
}
public PostResponse findById(Long userId, Long postId, @Nullable String shareKey) {
Post post = postRepository.findByIdFetchPollChoices(postId)
.orElseThrow(() -> new BadRequestException(ErrorCode.POST_NOT_FOUND));
if (!post.isRevealable(userId, shareKey)) {
throw new BadRequestException(ErrorCode.POST_NOT_REVEALABLE);
}
return createPostResponse(userId, post);
}
private PostResponse createPostResponse(Long userId, Post post) {
User author = userRepository.findById(post.getUserId())
.orElseThrow(() -> new BadRequestException(ErrorCode.USER_NOT_FOUND));
long commentCount = commentRepository.countByPostId(post.getId());
List<Vote> voteList = voteRepository.findAllByPostId(post.getId());
long voterCount = voteList.stream()
.map(Vote::getUserId)
.distinct()
.count();
boolean isAuthor = post.getUserId().equals(userId);
List<PollChoiceVoteResponse> pollChoiceVoteResponseList = createPollChoiceResponse(
userId,
post.getPollChoices(),
voteList
);
return PostResponse.of(post, author, pollChoiceVoteResponseList, isAuthor, commentCount, voterCount);
}
private List<PollChoiceVoteResponse> createPollChoiceResponse(Long userId, List<PollChoice> pollChoices, List<Vote> voteList) {
return pollChoices.stream()
.map(pollChoice -> new PollChoiceVoteResponse(
pollChoice.getId(),
pollChoice.getTitle(),
pollChoice.getImageUrl(),
getVoteId(voteList, pollChoice.getId(), userId)
))
.toList();
}
private Long getVoteId(List<Vote> voteList, Long pollChoiceId, Long userId) {
return voteList.stream()
.filter(vote -> vote.getPollChoiceId().equals(pollChoiceId) && vote.getUserId().equals(userId))
.map(Vote::getId)
.findFirst()
.orElse(null);
}
public CursorBasePaginatedResponse<MyPagePostResponse> findUserPosts(
Long userId,
Long myPageUserId,
Long cursor,
int size
) {
return myPagePostManager.getUserPosts(userId, myPageUserId, cursor, Pageable.ofSize(size));
}
public CursorBasePaginatedResponse<MyPagePostResponse> findVotedPosts(
Long userId,
Long myPageUserId,
Long cursor,
int size
) {
return myPagePostManager.getVotedPosts(userId, myPageUserId, cursor, Pageable.ofSize(size));
}
public CursorBasePaginatedResponse<FeedResponse> findFeed(Long userId, Long cursor, int size) {
Slice<FeedDto> postSlice = postRepository.findFeed(cursor, PageRequest.ofSize(size));
return CursorBasePaginatedResponse.of(postSlice.map(post -> createFeedResponse(userId, post)));
}
private FeedResponse createFeedResponse(Long userId, FeedDto feedDto) {
AuthorDto author = new AuthorDto(feedDto.postUserId(), feedDto.nickname(), feedDto.profileUrl());
boolean isAuthor = feedDto.postUserId().equals(userId);
return FeedResponse.of(feedDto, author, isAuthor);
}
public UpdatePostResponse findUpdatePost(Long userId, Long postId) {
Post post = postRepository.findByIdAndUserId(postId, userId)
.orElseThrow(() -> new BadRequestException(ErrorCode.POST_NOT_FOUND));
return UpdatePostResponse.of(post);
}
}