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

[feat] 댓글 단건 조회 API + 댓글 삭제 여부 필드 추가 #104

Merged
merged 1 commit into from
Feb 12, 2025
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
46 changes: 44 additions & 2 deletions src/main/java/muit/backend/controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import muit.backend.apiPayLoad.ApiResponse;
import muit.backend.apiPayLoad.code.status.ErrorStatus;
import muit.backend.apiPayLoad.exception.GeneralException;
import muit.backend.converter.CommentConverter;
import muit.backend.domain.entity.member.Comment;
import muit.backend.domain.entity.member.Member;
import muit.backend.domain.entity.member.Reply;
import muit.backend.domain.enums.PostType;
import muit.backend.domain.enums.Role;
import muit.backend.dto.commentDTO.CommentReplyRequestDTO;
import muit.backend.dto.commentDTO.CommentReplyResponseDTO;
import muit.backend.dto.postDTO.LostRequestDTO;
import muit.backend.dto.postDTO.LostResponseDTO;
import muit.backend.dto.reportDTO.ReportRequestDTO;
import muit.backend.dto.reportDTO.ReportResponseDTO;
import muit.backend.repository.CommentRepository;
import muit.backend.repository.ReplyRepository;
import muit.backend.service.CommentService;
import muit.backend.service.MemberService;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,6 +34,8 @@ public class CommentController {

private final CommentService commentService;
private final MemberService memberService;
private final CommentRepository commentRepository;
private final ReplyRepository replyRepository;

@GetMapping("/{postId}")
@Operation(summary = "댓글 조회 API", description = "특정 게시물의 댓글을 모두 조회하는 API")
Expand All @@ -37,9 +47,9 @@ public ApiResponse<CommentReplyResponseDTO.CommentListResponseDTO> getCommentLis
@PathVariable("postId") Long postId,
@RequestParam(defaultValue = "0", name = "page") Integer page,
@RequestParam(defaultValue = "20", name = "size") Integer size) {
memberService.getMemberByToken(accessToken);
Member member = memberService.getMemberByToken(accessToken);

return ApiResponse.onSuccess(commentService.getCommentList(postId, page, size));
return ApiResponse.onSuccess(commentService.getCommentList(postId, member, page, size));
}

@PostMapping("/{postId}")
Expand Down Expand Up @@ -80,4 +90,36 @@ public ApiResponse<ReportResponseDTO.ReportResultDTO> reportComment(@RequestHead
Member member = memberService.getMemberByToken(accessToken);
return ApiResponse.onSuccess(commentService.reportComment(commentType,commentId,member,requestDTO));
}

@GetMapping("/admin/comment")
@Operation(summary = "댓글 단건 조회 API", description = "신고 내용 중 댓글 id로 댓글 정보 불러오는 API")
@Parameters({
@Parameter(name = "commentId", description = "댓글 아이디")
})
public ApiResponse<CommentReplyResponseDTO.CommentResponseDTO> getComment(@RequestHeader("Authorization") String accessToken,
@RequestParam("commentId") Long commentId) {
Member member = memberService.getMemberByToken(accessToken);
if(member.getRole()!= Role.ADMIN){
throw new GeneralException(ErrorStatus.MEMBER_NOT_ADMIN);
}
Comment comment = commentRepository.findById(commentId).orElseThrow(()->new GeneralException(ErrorStatus.COMMENT_NOT_FOUND));

return ApiResponse.onSuccess(CommentConverter.toCommentResponseDTO(comment,member));
}

@GetMapping("/admin/reply")
@Operation(summary = "대댓글 단건 조회 API", description = "신고 내용 중 대댓글 id로 댓글 정보 불러오는 API")
@Parameters({
@Parameter(name = "replyId", description = "대댓글 아이디")
})
public ApiResponse<CommentReplyResponseDTO.ReplyResponseDTO> getReply(@RequestHeader("Authorization") String accessToken,
@RequestParam("commentId") Long commentId) {
Member member = memberService.getMemberByToken(accessToken);
if(member.getRole()!= Role.ADMIN){
throw new GeneralException(ErrorStatus.MEMBER_NOT_ADMIN);
}
Reply reply = replyRepository.findById(commentId).orElseThrow(()->new GeneralException(ErrorStatus.COMMENT_NOT_FOUND));

return ApiResponse.onSuccess(CommentConverter.toReplyResponseDTO(reply,member));
}
}
15 changes: 10 additions & 5 deletions src/main/java/muit/backend/converter/CommentConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public static Reply toReply(CommentReplyRequestDTO.ReplyRequestDTO requestDTO, C

//Comment -> DTO
//생성 시 답, 조회 시 단건 답
public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Comment comment) {
public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Comment comment, Member member) {

List<CommentReplyResponseDTO.ReplyResponseDTO> replies = comment.getReplyList().stream()
.map(CommentConverter::toReplyResponseDTO).collect(Collectors.toList());
.map(reply->CommentConverter.toReplyResponseDTO(reply,member)).collect(Collectors.toList());

String nickname = switch (comment.getAnonymousIndex()) {
case -2 -> "삭제된 댓글";
Expand All @@ -106,20 +106,23 @@ public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Co
default -> "익명" + comment.getAnonymousIndex();
};

boolean isMyComment = comment.getMember()==member;

return CommentReplyResponseDTO.CommentResponseDTO.builder()
.commentId(comment.getId())
.content(comment.getContent())
.memberId(comment.getMember().getId())
.isMyComment(isMyComment)
.nickname(nickname)
.replies(replies)
.createdAt(comment.getCreatedAt())
.build();
}

//조회 시 리스트 형식 답
public static CommentReplyResponseDTO.CommentListResponseDTO toCommentListResponseDTO(Page<Comment> commentPage) {
public static CommentReplyResponseDTO.CommentListResponseDTO toCommentListResponseDTO(Page<Comment> commentPage, Member member) {
List<CommentReplyResponseDTO.CommentResponseDTO> commentResultListDTO = commentPage.stream()
.map(CommentConverter::toCommentResponseDTO).collect(Collectors.toList());
.map(comment->CommentConverter.toCommentResponseDTO(comment, member)).collect(Collectors.toList());

return CommentReplyResponseDTO.CommentListResponseDTO.builder()
.comments(commentResultListDTO)
Expand All @@ -132,18 +135,20 @@ public static CommentReplyResponseDTO.CommentListResponseDTO toCommentListRespon


//생성 시 단건 답, 댓글 DTO 내부 대댓글 형식
public static CommentReplyResponseDTO.ReplyResponseDTO toReplyResponseDTO(Reply reply) {
public static CommentReplyResponseDTO.ReplyResponseDTO toReplyResponseDTO(Reply reply,Member member) {

String nickname = switch (reply.getAnonymousIndex()) {
case -1 -> reply.getMember().getName();
case 0 -> "글쓴이";
default -> "익명" + reply.getAnonymousIndex();
};

boolean isMyComment = reply.getMember()==member;
return CommentReplyResponseDTO.ReplyResponseDTO.builder()
.commentId(reply.getComment().getId())
.replyId(reply.getId())
.memberId(reply.getMember().getId())
.isMyComment(isMyComment)
.nickname(nickname)
.content(reply.getContent())
.createdAt(reply.getCreatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class CommentResponseDTO{
private String nickname;
private String content;
private Long memberId;
private Boolean isMyComment;
private LocalDateTime createdAt;
private List<ReplyResponseDTO> replies;
}
Expand All @@ -33,6 +34,7 @@ public static class ReplyResponseDTO{
private Long replyId;
private String content;
private Long memberId;
private Boolean isMyComment;
private LocalDateTime createdAt;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/muit/backend/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Service
public interface CommentService {
CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId, Integer page, Integer size);
CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId,Member member, Integer page, Integer size);

CommentReplyResponseDTO.CommentResponseDTO writeComment(CommentReplyRequestDTO.CommentRequestDTO requestDTO, Long postId, Member member);

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/muit/backend/service/CommentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class CommentServiceImpl implements CommentService {
//특정 게시물의 모든 댓글 조회
@Transactional(readOnly = true)
@Override
public CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId, Integer page, Integer size) {
public CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId, Member member, Integer page, Integer size) {
Post post = postRepository.findById(postId).orElseThrow(()->new GeneralException(ErrorStatus.POST_NOT_FOUND));
Page<Comment> commentPage = commentRepository.findAllByPost(post, PageRequest.of(page, size));
return CommentConverter.toCommentListResponseDTO(commentPage);
return CommentConverter.toCommentListResponseDTO(commentPage,member);
}

//특정 게시물에 댓글 생성
Expand All @@ -51,7 +51,7 @@ public CommentReplyResponseDTO.CommentResponseDTO writeComment(CommentReplyReque
post.changeCommentCount(true);

//comment -> responseDTO
return CommentConverter.toCommentResponseDTO(comment);
return CommentConverter.toCommentResponseDTO(comment,member);
}

//특정 댓글에 대댓글 생성
Expand All @@ -65,7 +65,7 @@ public CommentReplyResponseDTO.ReplyResponseDTO writeReply(CommentReplyRequestDT
replyRepository.save(reply);
comment.getPost().changeCommentCount(true);

return CommentConverter.toReplyResponseDTO(reply);
return CommentConverter.toReplyResponseDTO(reply,member);
}

//특정 댓글 삭제
Expand Down