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

[REFACTOR #111] 자유게시판&성지순례 - 인증글 댓글 리스트, 내가 작성한 댓글 조회 #113

Merged
merged 4 commits into from
Jul 24, 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 @@ -5,9 +5,8 @@
import com.favoriteplace.app.repository.MemberRepository;
import com.favoriteplace.app.service.community.CommentCommandService;
import com.favoriteplace.app.service.community.CommentQueryService;
import com.favoriteplace.global.exception.ErrorCode;
import com.favoriteplace.global.exception.RestApiException;
import com.favoriteplace.global.util.SecurityUtil;
import com.google.api.Http;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -26,33 +25,23 @@ public class GuestBookCommentController {
private final MemberRepository memberRepository;

@GetMapping("/my-comments")
public GuestBookResponseDto.MyGuestBookCommentDto getMyComments(
public ResponseEntity<GuestBookResponseDto.MyGuestBookCommentDto> getMyComments(
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "10") int size
){
Member member = securityUtil.getUser();
List<GuestBookResponseDto.MyGuestBookComment> myComments = commentQueryService.getMyGuestBookComments(member, page, size);
return GuestBookResponseDto.MyGuestBookCommentDto.builder()
.page((long) page)
.size((long) size)
.comment(myComments)
.build();
return ResponseEntity.ok(commentQueryService.getMyGuestBookComments(member, page, size));
}

@GetMapping("/{guestbook_id}/comments")
public CommentResponseDto.PostCommentDto getGuestBookComments(
public ResponseEntity<CommentResponseDto.CommentDto> getGuestBookComments(
@PathVariable("guestbook_id") Long guestbookId,
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "10") int size,
@RequestParam(required = false, defaultValue = "5") int size,
HttpServletRequest request
){
Member member = securityUtil.getUserFromHeader(request);
List<CommentResponseDto.PostComment> comments = commentQueryService.getGuestBookComments(page, size, member, guestbookId);
return CommentResponseDto.PostCommentDto.builder()
.page((long) page)
.size((long) size)
.comment(comments)
.build();
return ResponseEntity.ok(commentQueryService.getGuestBookComments(page, size, member, guestbookId));
}

@PostMapping("/{guestbook_id}/comments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,24 @@ public class PostCommentController {
private final MemberRepository memberRepository;

@GetMapping("/my-comments")
public PostResponseDto.MyCommentDto getMyComments(
public ResponseEntity<PostResponseDto.MyCommentDto> getMyComments(
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "10") int size
){
Member member = securityUtil.getUser();
List<PostResponseDto.MyComment> comments = commentQueryService.getMyPostComments(member, page, size);
return PostResponseDto.MyCommentDto.builder()
.page((long) page)
.size((long) size)
.comment(comments)
.build();
//Member member = securityUtil.getUser();
Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
return ResponseEntity.ok(commentQueryService.getMyPostComments(member, page, size));
}

@GetMapping("/{post_id}/comments")
public CommentResponseDto.PostCommentDto getPostComments(
public ResponseEntity<CommentResponseDto.CommentDto> getPostComments(
@PathVariable("post_id") Long postId,
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "10") int size,
@RequestParam(required = false, defaultValue = "5") int size,
HttpServletRequest request
){
Member member = securityUtil.getUserFromHeader(request);
List<CommentResponseDto.PostComment> comments = commentQueryService.getPostComments(member, page, size, postId);
return CommentResponseDto.PostCommentDto.builder()
.page((long)page)
.size((long)size)
.comment(comments)
.build();
return ResponseEntity.ok(commentQueryService.getPostComments(member, page, size, postId));
}

@PostMapping("/{post_id}/comments")
Expand Down
104 changes: 93 additions & 11 deletions src/main/java/com/favoriteplace/app/converter/CommentConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.favoriteplace.app.converter;

import com.favoriteplace.app.domain.Member;
import com.favoriteplace.app.domain.community.Comment;
import com.favoriteplace.app.domain.community.GuestBook;
import com.favoriteplace.app.dto.UserInfoResponseDto;
Expand All @@ -8,8 +9,60 @@
import com.favoriteplace.app.dto.community.PostResponseDto;
import com.favoriteplace.global.util.DateTimeFormatUtils;

import java.util.Collections;
import java.util.List;

public class CommentConverter {

public static CommentResponseDto.ParentComment toComment(Comment comment, Member member, List<Comment> subComments){
if(comment.getIsDeleted()){
return CommentResponseDto.ParentComment.builder()
.userInfo(hideUserInfo(comment))
.id(null)
.content("[삭제된 댓글입니다.]")
.passedTime(null)
.isWrite(null)
.subComments(toSubComments(subComments, member))
.build();
}
else{
return CommentResponseDto.ParentComment.builder()
.userInfo(showUserInfo(comment))
.id(comment.getId())
.content(comment.getContent())
.passedTime(DateTimeFormatUtils.getPassDateTime(comment.getCreatedAt()))
.isWrite(isCommentWriter(member, comment))
.subComments(toSubComments(subComments, member))
.build();
}
}

public static List<CommentResponseDto.SubComment> toSubComments(List<Comment> subComments, Member member){
if (subComments.isEmpty()){return Collections.emptyList();}
return subComments.stream().map(comment -> {
if(comment.getIsDeleted()){
return CommentResponseDto.SubComment.builder()
.userInfo(hideUserInfo(comment))
.id(null)
.content("[삭제된 댓글입니다.]")
.passedTime(null)
.isWrite(null)
.referenceNickname(null)
.build();
}
else{
return CommentResponseDto.SubComment.builder()
.userInfo(showUserInfo(comment))
.id(comment.getId())
.content(comment.getContent())
.passedTime(DateTimeFormatUtils.getPassDateTime(comment.getCreatedAt()))
.isWrite(isCommentWriter(member, comment))
.referenceNickname(comment.getReferenceComment()==null ? null : comment.getReferenceComment().getMember().getNickname())
.build();
}
}).toList();
}

public static GuestBookResponseDto.MyGuestBookComment toMyGuestBookComment(Comment comment) {
GuestBook guestBook = comment.getGuestBook();
return GuestBookResponseDto.MyGuestBookComment.builder()
Expand All @@ -22,22 +75,12 @@ public static GuestBookResponseDto.MyGuestBookComment toMyGuestBookComment(Comme
.nickname(guestBook.getMember().getNickname())
.views(guestBook.getView())
.likes(guestBook.getLikeCount())
.comments((long) guestBook.getComments().size())
.comments(getNotDeletedComment(guestBook.getComments()))
.passedTime(DateTimeFormatUtils.getPassDateTime(guestBook.getCreatedAt()))
.build())
.build();
}

public static CommentResponseDto.PostComment toPostComment(Comment comment, Boolean isWrite){
return CommentResponseDto.PostComment.builder()
.userInfo(UserInfoResponseDto.of(comment.getMember()))
.id(comment.getId())
.content(comment.getContent())
.passedTime(DateTimeFormatUtils.getPassDateTime(comment.getCreatedAt()))
.isWrite(isWrite)
.build();
}

public static PostResponseDto.MyComment toMyPostComment(Comment comment){
return PostResponseDto.MyComment.builder()
.id(comment.getId())
Expand All @@ -46,4 +89,43 @@ public static PostResponseDto.MyComment toMyPostComment(Comment comment){
.post(PostConverter.toMyPost(comment.getPost()))
.build();
}

/**
* 사용자(앱을 사용하는 유저)가 댓글 작성자가 맞는지 확인하는 함수
*/
public static Boolean isCommentWriter(Member member, Comment comment){
if(member == null){return false;}
return member.getId().equals(comment.getMember().getId());
}


/**
* 삭제된 댓글이면 유저 정보 감춤
*/
public static UserInfoResponseDto hideUserInfo(Comment comment){
return UserInfoResponseDto.builder()
.id(null)
.nickname("[알 수 없음]")
.profileImageUrl(null)
.profileTitleUrl(null)
.profileIconUrl(null)
.build();
}

public static UserInfoResponseDto showUserInfo(Comment comment){
Member member = comment.getMember();
return UserInfoResponseDto.builder()
.id(member.getId())
.nickname(member.getNickname())
.profileImageUrl(member.getProfileImageUrl())
.profileTitleUrl(member.getProfileTitle()!= null ? member.getProfileTitle().getDefaultImage().getUrl() : null)
.profileIconUrl(member.getProfileIcon()!= null ? member.getProfileIcon().getDefaultImage().getUrl() : null)
.build();
}

private static long getNotDeletedComment(List<Comment> comments){
return comments.stream()
.filter(comment -> !comment.getIsDeleted())
.count();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.favoriteplace.app.converter;

import com.favoriteplace.app.domain.Image;
import com.favoriteplace.app.domain.community.Comment;
import com.favoriteplace.app.domain.community.GuestBook;
import com.favoriteplace.app.domain.community.HashTag;
import com.favoriteplace.app.domain.travel.Pilgrimage;
Expand All @@ -18,7 +19,7 @@ public static GuestBookResponseDto.MyGuestBookInfo toGuestBook(GuestBook guestBo
.nickname(guestBook.getMember().getNickname())
.views(guestBook.getView())
.likes(guestBook.getLikeCount())
.comments((long) guestBook.getComments().size())
.comments(getNotDeletedComment(guestBook.getComments()))
.passedTime(DateTimeFormatUtils.getPassDateTime(guestBook.getCreatedAt()))
.build();
}
Expand All @@ -30,7 +31,7 @@ public static GuestBookResponseDto.GuestBookInfo toGuestBookInfo(GuestBook guest
.content(guestBook.getContent())
.views(guestBook.getView())
.likes(guestBook.getLikeCount())
.comments((long) guestBook.getComments().size())
.comments(getNotDeletedComment(guestBook.getComments()))
.isLike(isLike)
.isWrite(isWrite)
.passedTime(DateTimeFormatUtils.getPassDateTime(guestBook.getCreatedAt()))
Expand Down Expand Up @@ -62,7 +63,7 @@ public static GuestBookResponseDto.TotalGuestBookInfo toTotalGuestBookInfo(Guest
.thumbnail(!guestBook.getImages().isEmpty() ? guestBook.getImages().get(0).getUrl() : null)
.views(guestBook.getView())
.likes(guestBook.getLikeCount())
.comments((long) guestBook.getComments().size())
.comments(getNotDeletedComment(guestBook.getComments()))
.passedTime(DateTimeFormatUtils.getPassDateTime(guestBook.getCreatedAt()))
.hashTags(guestBook.getHashTags().stream().map(HashTag::getTagName).toList())
.build();
Expand All @@ -78,4 +79,10 @@ public static GuestBookResponseDto.DetailGuestBookDto toDetailGuestBookInfo(Gues

}

private static long getNotDeletedComment(List<Comment> comments){
return comments.stream()
.filter(comment -> !comment.getIsDeleted())
.count();
}

}
12 changes: 9 additions & 3 deletions src/main/java/com/favoriteplace/app/converter/PostConverter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.favoriteplace.app.converter;

import com.favoriteplace.app.domain.Image;
import com.favoriteplace.app.domain.Member;
import com.favoriteplace.app.domain.community.Comment;
import com.favoriteplace.app.domain.community.Post;
import com.favoriteplace.app.dto.UserInfoResponseDto;
import com.favoriteplace.app.dto.community.PostResponseDto;
Expand All @@ -20,7 +20,7 @@ public static PostResponseDto.MyPost toMyPost(Post post){
.nickname(post.getMember().getNickname())
.views(post.getView())
.likes(post.getLikeCount())
.comments((long) post.getComments().size())
.comments(getNotDeletedComment(post.getComments()))
.passedTime(DateTimeFormatUtils.getPassDateTime(post.getCreatedAt())).build();
}

Expand All @@ -42,11 +42,17 @@ public static PostResponseDto.PostInfo toPostInfo(Post post, Boolean isLike, Boo
.content(post.getContent())
.view(post.getView())
.likes(post.getLikeCount())
.comments((long) post.getComments().size())
.comments(getNotDeletedComment(post.getComments()))
.isLike(isLike)
.isWrite(isWrite)
.passedTime(DateTimeFormatUtils.getPassDateTime(post.getCreatedAt()))
.image(images)
.build();
}

private static long getNotDeletedComment(List<Comment> comments){
return comments.stream()
.filter(comment -> !comment.getIsDeleted())
.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public static UserInfoResponseDto of(Member member) {
.profileIconUrl(member.getProfileIcon()!= null ? member.getProfileIcon().getDefaultImage().getUrl() : null)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,36 @@ public class CommentResponseDto {
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class PostCommentDto {
public static class CommentDto {
private Long page;
private Long size;
private List<PostComment> comment;
private List<ParentComment> parentComment;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class PostComment{
public static class ParentComment {
private UserInfoResponseDto userInfo;
private Long id;
private String content;
private String passedTime;
private Boolean isWrite;
private List<SubComment> subComments;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class SubComment {
private UserInfoResponseDto userInfo;
private Long id;
private String content;
private String passedTime;
private Boolean isWrite;
private String referenceNickname;
}

}
Loading
Loading