Skip to content
Merged

fix #112

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
57 changes: 44 additions & 13 deletions src/main/java/com/example/prdoit/controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.prdoit.controller;

import com.example.prdoit.dto.comment.*;
import com.example.prdoit.exception.CustomException;
import com.example.prdoit.service.comment.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -10,43 +11,73 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/comments")
@RequestMapping("/comments")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class CommentController {

private final CommentService commentService;

// 1. 댓글 조회
@GetMapping("/content/{contentId}")
public ResponseEntity<List<CommentResponseDto>> getComments(@PathVariable String contentId) {
return ResponseEntity.ok(commentService.getCommentList(contentId));
public ResponseEntity<Object> getComments(@PathVariable String contentId) {
try {
return ResponseEntity.ok(commentService.getCommentList(contentId));
} catch (CustomException e){
return ResponseEntity.badRequest().body("댓글 조회에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("댓글 조회에 실패했습니다.");
}
}

// 2. 댓글 작성
@PostMapping()
public ResponseEntity<String> createComment(@RequestBody CommentRequestDto commentRequestDto) {
commentService.postComment(commentRequestDto);
return ResponseEntity.ok("댓글이 성공적으로 작성되었습니다!");
public ResponseEntity<Object> createComment(@RequestBody CommentRequestDto commentRequestDto) {
try {
commentService.postComment(commentRequestDto);
return ResponseEntity.ok("댓글이 성공적으로 작성되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("댓글 작성에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("댓글 작성에 실패했습니다.");
}
}

// 3. 대댓글 작성
@PostMapping("/reply")
public ResponseEntity<String> createReply(@RequestBody CommentReplyRequestDto commentReplyRequestDto) {
commentService.postCommentReply(commentReplyRequestDto);
return ResponseEntity.ok("대댓글이 성공적으로 작성되었습니다!");
public ResponseEntity<Object> createReply(@RequestBody CommentReplyRequestDto commentReplyRequestDto) {
try {
commentService.postCommentReply(commentReplyRequestDto);
return ResponseEntity.ok("대댓글이 성공적으로 작성되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("대댓글 작성에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("대댓글 작성에 실패했습니다.");
}
}

// 4. 댓글 수정
@PatchMapping
public ResponseEntity<String> updateComment(@RequestBody CommentPatchDto commentPatchDto) {
commentService.patchComment(commentPatchDto);
return ResponseEntity.ok("댓글이 성공적으로 수정되었습니다!");
try {
commentService.patchComment(commentPatchDto);
return ResponseEntity.ok("댓글이 성공적으로 수정되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("댓글 수정에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("댓글 수정에 실패했습니다.");
}
}

// 5.대댓글 수정
@PatchMapping("/reply")
public ResponseEntity<String> updateReply(@RequestBody CommentReplyPatchDto commentReplyPatchDto) {
commentService.patchCommentReply(commentReplyPatchDto);
return ResponseEntity.ok("대댓글이 성공적으로 수정되었습니다!");
try {
commentService.patchCommentReply(commentReplyPatchDto);
return ResponseEntity.ok("대댓글이 성공적으로 수정되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("대댓글 수정에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("대댓글 수정에 실패했습니다.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,27 @@ public void postComment(CommentRequestDto commentRequestDto) {

IdTable idTable = idTableRepository.findById(commentRequestDto.getUserId()).orElseThrow(()-> new CustomException("해당 아이디가 존재하지 않습니다."));

commentTableRepository.save(CommentTable.builder()
.commentContent(commentRequestDto.getCommentContent())
.commentNickname(idTable.getNickname())
.commentDate(commentRequestDto.getCommentDate())
.contentId(contentTable)
.build());

notificationTableRepository.save(NotificationTable.builder()
.notificationContent("댓글이 작성되었습니다.")
.contentId(contentTable.getContentId())
.userId(contentTable.getUserId())
.isRead(0)
.isContent(1)
.build());

log.info("[postComment] 댓글 등록을 완료했습니다.");
try {
commentTableRepository.save(CommentTable.builder()
.commentContent(commentRequestDto.getCommentContent())
.commentNickname(idTable.getNickname())
.commentDate(commentRequestDto.getCommentDate())
.contentId(contentTable)
.build());

notificationTableRepository.save(NotificationTable.builder()
.notificationContent("댓글이 작성되었습니다.")
.contentId(contentTable.getContentId())
.userId(contentTable.getUserId())
.isRead(0)
.isContent(1)
.build());

log.info("[postComment] 댓글 등록을 완료했습니다.");
} catch (Exception e){
log.error("[postComment] 댓글 등록 중 오류 발생 - {}", e.getMessage());
throw new CustomException("댓글 등록 중 오류가 발생했습니다.");
}
}

// 3. 대댓글 작성 (POST)
Expand All @@ -87,23 +92,27 @@ public void postCommentReply(CommentReplyRequestDto commentReplyRequestDto) {
.orElseThrow(() -> new CustomException("해당 댓글이 없습니다."));

IdTable idTable = idTableRepository.findById(commentReplyRequestDto.getUserId()).orElseThrow(()->new CustomException("해당아이디가 존재하지 않습니다."));

commentCommentTableRepository.save(CommentCommentTable.builder()
.commentCommentContent(commentReplyRequestDto.getCommentReplyContent())
.userNickname(idTable.getNickname())
.commentCommentDate(commentReplyRequestDto.getCommentReplyDate())
.commentId(parentComment)
.build());

notificationTableRepository.save(NotificationTable.builder()
.notificationContent("대댓글이 작성되었습니다.")
.contentId(parentComment.getContentId().getContentId())
.userId(idTableRepository.findByNickname(parentComment.getCommentNickname()))
.isRead(0)
.isContent(1)
.build());

log.info("[postCommentReply] 대댓글 등록을 완료했습니다.");
try {
commentCommentTableRepository.save(CommentCommentTable.builder()
.commentCommentContent(commentReplyRequestDto.getCommentReplyContent())
.userNickname(idTable.getNickname())
.commentCommentDate(commentReplyRequestDto.getCommentReplyDate())
.commentId(parentComment)
.build());

notificationTableRepository.save(NotificationTable.builder()
.notificationContent("대댓글이 작성되었습니다.")
.contentId(parentComment.getContentId().getContentId())
.userId(idTableRepository.findByNickname(parentComment.getCommentNickname()))
.isRead(0)
.isContent(1)
.build());

log.info("[postCommentReply] 대댓글 등록을 완료했습니다.");
} catch (Exception e){
log.error("[postCommentReply] 대댓글 등록 중 오류 발생 - {}", e.getMessage());
throw new CustomException("대댓글 등록 중 오류가 발생했습니다.");
}
}

// 4. 댓글 수정 (PATCH)
Expand All @@ -114,11 +123,16 @@ public void patchComment(CommentPatchDto commentPatchDto) {
CommentTable comment = commentTableRepository.findById(commentPatchDto.getCommentId())
.orElseThrow(() -> new CustomException("해당 댓글이 없습니다."));

comment.setCommentContent(commentPatchDto.getCommentContent());
comment.setCommentDate(commentPatchDto.getCommentDate());
commentTableRepository.save(comment);
try{
comment.setCommentContent(commentPatchDto.getCommentContent());
comment.setCommentDate(commentPatchDto.getCommentDate());
commentTableRepository.save(comment);

log.info("[patchComment] 댓글 수정이 완료되었습니다.");
log.info("[patchComment] 댓글 수정이 완료되었습니다.");
} catch (Exception e){
log.error("[patchComment] 댓글 수정 중 오류 발생 - {}", e.getMessage());
throw new CustomException("댓글 수정 중 오류가 발생했습니다.");
}
}

// 5. 대댓글 수정 (PATCH)
Expand All @@ -129,10 +143,15 @@ public void patchCommentReply(CommentReplyPatchDto commentReplyPatchDto) {
CommentCommentTable reply = commentCommentTableRepository.findById(commentReplyPatchDto.getCommentReplyId())
.orElseThrow(() -> new CustomException("해당 대댓글이 없습니다."));

reply.setCommentCommentContent(commentReplyPatchDto.getCommentReplyContent());
reply.setCommentCommentDate(commentReplyPatchDto.getCommentReplyDate());
commentCommentTableRepository.save(reply);
try{
reply.setCommentCommentContent(commentReplyPatchDto.getCommentReplyContent());
reply.setCommentCommentDate(commentReplyPatchDto.getCommentReplyDate());
commentCommentTableRepository.save(reply);

log.info("[patchCommentReply] 대댓글 수정이 완료되었습니다.");
log.info("[patchCommentReply] 대댓글 수정이 완료되었습니다.");
} catch (Exception e){
log.error("[patchCommentReply] 대댓글 수정 중 오류 발생 - {}", e.getMessage());
throw new CustomException("대댓글 수정 중 오류가 발생했습니다.");
}
}
}