Skip to content
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

Post(플로깅 게시글) 관련 기능 리팩토링을 수행한다. #91

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,13 @@ public ResponseEntity<StatusResponse> removeReply(Long replyId, Member member) {
.build());
}

// 내가 쓴 댓글의 게시글 모아보기
// 내가 쓴 댓글의 게시글 모아보기 (수정됨)
public ResponseEntity<StatusResponse> getCommentedPosts(Member member) {
List<Comment> comments = commentRepository.findByWriter(member);

List<CommentPostDto> commentedPosts = comments.stream()
.collect(Collectors.toMap(
comment -> comment.getPost().getId(),
CommentPostDto::of,
(existing, replacement) -> existing,
LinkedHashMap::new
))
.values().stream()
.map(CommentPostDto::of)
.distinct()
.collect(Collectors.toList());

return ResponseEntity.ok(StatusResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ public ResponseEntity<StatusResponse> unheartPost(Member member, Long postId) {
.build());
}

// 찜한 글 모아보기
// 찜한 글 모아보기 (수정됨)
@Transactional(readOnly = true)
public ResponseEntity<StatusResponse> findHeartPostList(Member member) {
List<Heart> heartList = heartRepository.findAllByMemberOrderByIdDesc(member);

List<PostResponseDto> postList = new ArrayList<>();
for (Heart heart : heartList) {
List<PostResponseDto> postList = heartList.stream().map(heart -> {
Post post = postRepository.findById(heart.getPost().getId())
.orElseThrow(() -> new PostNotFoundException(heart.getPost().getId()));

Expand All @@ -100,18 +99,13 @@ public ResponseEntity<StatusResponse> findHeartPostList(Member member) {
boolean isReviewed = scoreRepository.existsByParticipantAndPost(member, post);
Long joinedMemberCount = postjoinRepository.countByPost(post);

return PostResponseDto.of(post, imgUrlList, Optional.of(isJoined), Optional.of(isHearted), isEnded, isAuthor, isReviewed, joinedMemberCount);
}).collect(Collectors.toList());

PostResponseDto postResponseDto = PostResponseDto.of(post, imgUrlList, Optional.of(isJoined), Optional.of(isHearted), isEnded, isAuthor, isReviewed, joinedMemberCount);
postList.add(postResponseDto);
}

Long count = heartRepository.countByMember(member);
HeartResponseDto response = new HeartResponseDto(postList, member.getId(), count);

return ResponseEntity.ok(StatusResponse.builder()
.status(StatusEnum.OK.getStatusCode())
.message(StatusEnum.OK.getCode())
.data(response)
.build());
return ResponseEntity.ok(StatusResponse.builder()
.status(StatusEnum.OK.getStatusCode())
.message(StatusEnum.OK.getCode())
.data(postList)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ public ResponseEntity<StatusResponse> getFilteredPosts(
filterDto.setUserGender(member.getGender());
return postService.getFilteredPosts(filterDto, member);
}

// 가장 최근에 완료한 플로깅 게시글 1개 조회하기
@GetMapping("/latest-completed")
public ResponseEntity<StatusResponse> getLatestCompletedPost(@AuthUser Member member) {
return postService.getLatestCompletedPost(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificat
List<Post> findAllByDueDateAfterOrderByCreatedAtDesc(LocalDateTime now);
List<Post> findAllByDueDateBeforeAndIsRecruitmentSuccessfulTrueOrderByCreatedAtDesc(LocalDateTime now);
List<Post> findAllByDueDateBeforeOrderByCreatedAtDesc(LocalDateTime now);
List<Post> findAllByDueDateBeforeOrderByDueDateDesc(LocalDateTime now);
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ public ResponseEntity<StatusResponse> getJoinedPosts(Member member) {
public ResponseEntity<StatusResponse> getRecruitingPosts(Member member) {
LocalDateTime now = LocalDateTime.now();
List<Post> posts = postRepository.findAllByDueDateAfterOrderByCreatedAtDesc(now);
List<PostResponseDto> responseDtos = createPostResponseDtos(posts, member, now);
List<Post> filteredPosts = posts.stream()
.filter(post -> post.getAuthor().getId().equals(member.getId()) ||
postjoinRepository.existsByMemberAndPost(member, post))
.collect(Collectors.toList());
List<PostResponseDto> responseDtos = createPostResponseDtos(filteredPosts, member, now);
return ResponseEntity.ok(createStatusResponse(responseDtos));
}

Expand All @@ -320,7 +324,11 @@ public ResponseEntity<StatusResponse> getRecruitingPosts(Member member) {
public ResponseEntity<StatusResponse> getSuccessfulRecruitmentPosts(Member member) {
LocalDateTime now = LocalDateTime.now();
List<Post> posts = postRepository.findAllByDueDateBeforeAndIsRecruitmentSuccessfulTrueOrderByCreatedAtDesc(now);
List<PostResponseDto> responseDtos = createPostResponseDtos(posts, member, now);
List<Post> filteredPosts = posts.stream()
.filter(post -> post.getAuthor().getId().equals(member.getId()) ||
postjoinRepository.existsByMemberAndPost(member, post))
.collect(Collectors.toList());
List<PostResponseDto> responseDtos = createPostResponseDtos(filteredPosts, member, now);
return ResponseEntity.ok(createStatusResponse(responseDtos));
}

Expand All @@ -329,10 +337,49 @@ public ResponseEntity<StatusResponse> getSuccessfulRecruitmentPosts(Member membe
public ResponseEntity<StatusResponse> getCompletedPosts(Member member) {
LocalDateTime now = LocalDateTime.now();
List<Post> posts = postRepository.findAllByDueDateBeforeOrderByCreatedAtDesc(now);
List<PostResponseDto> responseDtos = createPostResponseDtos(posts, member, now);
List<Post> filteredPosts = posts.stream()
.filter(post -> post.getAuthor().getId().equals(member.getId()) ||
postjoinRepository.existsByMemberAndPost(member, post))
.collect(Collectors.toList());
List<PostResponseDto> responseDtos = createPostResponseDtos(filteredPosts, member, now);
return ResponseEntity.ok(createStatusResponse(responseDtos));
}

// 가장 최근에 완료한 플로깅 게시글 1개 조회
@Transactional(readOnly = true)
public ResponseEntity<StatusResponse> getLatestCompletedPost(Member member) {
LocalDateTime now = LocalDateTime.now();
List<Post> completedPosts = postRepository.findAllByDueDateBeforeOrderByDueDateDesc(now);
Optional<Post> latestCompletedPost = completedPosts.stream()
.filter(post -> post.getAuthor().getId().equals(member.getId()) ||
postjoinRepository.existsByMemberAndPost(member, post))
.findFirst();

if (latestCompletedPost.isPresent()) {
PostResponseDto responseDto = createPostResponseDto(latestCompletedPost.get(), member, now);
return ResponseEntity.ok(createStatusResponse(responseDto));
} else {
return ResponseEntity.ok(createStatusResponse(null));
}
}

private PostResponseDto createPostResponseDto(Post post, Member member, LocalDateTime now) {
List<String> urlList = postImageRepository.findAllByPost(post)
.stream()
.map(PostImage::getFileUrl)
.collect(Collectors.toList());

boolean isAuthor = member.getId().equals(post.getAuthor().getId());
boolean hasJoined = postjoinRepository.existsByMemberAndPost(member, post);
boolean isJoined = isAuthor || hasJoined;
boolean isHearted = heartRepository.existsByMemberAndPost(member, post);
boolean isEnded = now.isAfter(post.getDueDate());
boolean isReviewed = scoreRepository.existsByParticipantAndPost(member, post);
Long joinedMemberCount = postjoinRepository.countByPost(post);

return PostResponseDto.of(post, urlList, Optional.of(isJoined), Optional.of(isHearted), isEnded, isAuthor, isReviewed, joinedMemberCount);
}

@Transactional(readOnly = true)
public ResponseEntity<StatusResponse> getFilteredPosts(PostFilterDto filterDto, Member member) {
Specification<Post> spec = Specification.where(null);
Expand Down