-
Notifications
You must be signed in to change notification settings - Fork 1
게시글 공개 범위 관련 기능 추가 #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
게시글 공개 범위 관련 기능 추가 #232
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d633b80
refactor: 마이페이지 관련 코드 분리
wlsh44 73663f0
fix: 비공개 게시글 마이페이지에 보이는 버그 수정
wlsh44 fa2519a
refactor: dto 올바른 패키지로 이동
wlsh44 3ed8df8
feat: 게시글 공유 관련 기능 추가
wlsh44 5bfe241
docs: 게시글 공유 관련 docs 추가
wlsh44 c702d4a
test: 테스트 독립성 깨지는 문제 수정
wlsh44 3a42519
fix: 마이페이지 조회 hasNext 올바르게 조회 안되는 문제 수정
wlsh44 f6153ac
test: 테스트 코드 추가
wlsh44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/com/chooz/post/application/MyPagePostManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package com.chooz.post.application; | ||
|
|
||
| import com.chooz.common.dto.CursorBasePaginatedResponse; | ||
| import com.chooz.post.application.dto.PollChoiceVoteInfo; | ||
| import com.chooz.post.application.dto.PostWithVoteCount; | ||
| import com.chooz.post.domain.PollChoiceRepository; | ||
| import com.chooz.post.domain.PostRepository; | ||
| import com.chooz.post.presentation.dto.MostVotedPollChoiceDto; | ||
| import com.chooz.post.presentation.dto.MyPagePostResponse; | ||
| import com.chooz.vote.application.RatioCalculator; | ||
| 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.Component; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class MyPagePostManager { | ||
|
|
||
| private final PostRepository postRepository; | ||
| private final PollChoiceRepository pollChoiceRepository; | ||
| private final RatioCalculator ratioCalculator; | ||
|
|
||
| public CursorBasePaginatedResponse<MyPagePostResponse> getUserPosts( | ||
| Long userId, | ||
| Long myPageUserId, | ||
| Long cursor, | ||
| Pageable pageable | ||
| ) { | ||
| Slice<PostWithVoteCount> postSlice = postRepository.findPostsWithVoteCountByUserId( | ||
| userId, | ||
| myPageUserId, | ||
| cursor, | ||
| pageable | ||
| ); | ||
|
|
||
| return getMyPageCursoredResponse(postSlice); | ||
| } | ||
|
|
||
| public CursorBasePaginatedResponse<MyPagePostResponse> getVotedPosts( | ||
| Long userId, | ||
| Long myPageUserId, | ||
| Long cursor, | ||
| Pageable pageable | ||
| ) { | ||
| Slice<PostWithVoteCount> postSlice = postRepository.findVotedPostsWithVoteCount( | ||
| userId, | ||
| myPageUserId, | ||
| cursor, | ||
| pageable | ||
| ); | ||
|
|
||
| return getMyPageCursoredResponse(postSlice); | ||
| } | ||
|
|
||
| private CursorBasePaginatedResponse<MyPagePostResponse> getMyPageCursoredResponse(Slice<PostWithVoteCount> postSlice) { | ||
| if (postSlice.isEmpty()) { | ||
| return CursorBasePaginatedResponse.of(new SliceImpl<>( | ||
| List.of(), | ||
| postSlice.getPageable(), | ||
| false | ||
| )); | ||
| } | ||
|
|
||
| List<Long> postIds = getPostIds(postSlice); | ||
| Map<Long, PollChoiceVoteInfo> mostVotedPollChoiceByPostId = getMostVotedPollChoiceByPostId(postIds); | ||
|
|
||
| List<MyPagePostResponse> responses = getMyPagePostResponses(postSlice, mostVotedPollChoiceByPostId); | ||
|
|
||
| return CursorBasePaginatedResponse.of(new SliceImpl<>( | ||
| responses, | ||
| postSlice.getPageable(), | ||
| postSlice.hasNext() | ||
| )); | ||
| } | ||
|
|
||
| private Map<Long, PollChoiceVoteInfo> getMostVotedPollChoiceByPostId(List<Long> postIds) { | ||
| List<PollChoiceVoteInfo> pollChoiceWithVoteInfo = pollChoiceRepository.findPollChoiceWithVoteInfo(postIds); | ||
| return pollChoiceWithVoteInfo.stream() | ||
| .collect(Collectors.groupingBy( | ||
| PollChoiceVoteInfo::postId, | ||
| Collectors.collectingAndThen( | ||
| Collectors.toList(), | ||
| choices -> choices.stream() | ||
| .max(Comparator.comparing(PollChoiceVoteInfo::voteCounts)) | ||
| .orElse(null) | ||
| ) | ||
| )); | ||
| } | ||
|
|
||
| private List<MyPagePostResponse> getMyPagePostResponses( | ||
| Slice<PostWithVoteCount> postSlice, | ||
| Map<Long, PollChoiceVoteInfo> mostVotedPollChoiceByPostId | ||
| ) { | ||
| return postSlice.getContent() | ||
| .stream() | ||
| .map(postWithVoteCount -> { | ||
| var pollChoiceVoteInfo = mostVotedPollChoiceByPostId.get(postWithVoteCount.post().getId()); | ||
| var mostVotedPollChoiceInfo = MostVotedPollChoiceDto.of( | ||
| pollChoiceVoteInfo, | ||
| ratioCalculator.calculate(postWithVoteCount.voteCount(), pollChoiceVoteInfo.voteCounts()) | ||
| ); | ||
| return MyPagePostResponse.of(postWithVoteCount, mostVotedPollChoiceInfo); | ||
| }) | ||
| .toList(); | ||
| } | ||
|
|
||
| private List<Long> getPostIds(Slice<PostWithVoteCount> postSlice) { | ||
| return postSlice.getContent() | ||
| .stream() | ||
| .map(postWithVoteCount -> postWithVoteCount.post().getId()) | ||
| .toList(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분 리스트라 다른 명명규칙 썼던 것처럼 복수로 하는게 좋을거 같습니다!
그리고, getMyPagePostResponses에서 필터에 걸러지면 postSlice hasNext랑 달라지는 현상이 생기진 않을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 그 부분은 생각 못했는데 감사합니다! 수정했습니다