-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommentCommandController.java
More file actions
86 lines (77 loc) · 4.71 KB
/
CommentCommandController.java
File metadata and controls
86 lines (77 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package bst.bobsoolting.comment.command.application.controller;
import bst.bobsoolting.comment.command.application.controller.docs.CommentCommandControllerDocs;
import bst.bobsoolting.comment.command.application.dto.CommentDTO;
import bst.bobsoolting.comment.command.application.mapper.CommentConverter;
import bst.bobsoolting.comment.command.application.service.CommentCommandService;
import bst.bobsoolting.comment.command.domain.vo.request.RequestCreateCommentVO;
import bst.bobsoolting.comment.command.domain.vo.request.RequestUpdateCommentVO;
import bst.bobsoolting.comment.command.domain.vo.response.ResponseCreateCommentVO;
import bst.bobsoolting.comment.command.domain.vo.response.ResponseUpdateCommentVO;
import bst.bobsoolting.common.exception.CommonException;
import bst.bobsoolting.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api/comment")
@Slf4j
@RequiredArgsConstructor
public class CommentCommandController implements CommentCommandControllerDocs {
private final CommentCommandService commentService;
private final CommentConverter commentConverter;
private final SecurityUtil securityUtil;
@PostMapping
public ResponseEntity<?> createComment(@RequestBody RequestCreateCommentVO request, @RequestHeader(HttpHeaders.AUTHORIZATION) String token) {
log.info("등록 요청된 댓글 데이터 : {}", request);
String kakaoId = securityUtil.getKakaoIdFromToken(token.replace("Bearer ", ""));
try {
CommentDTO commentDTO = commentConverter.fromCreateVOToDTO(request);
CommentDTO saveCommentDTO = commentService.createComment(commentDTO, kakaoId);
ResponseCreateCommentVO response = commentConverter.fromDTOToCreateVO(saveCommentDTO);
return ResponseEntity.status(HttpStatus.OK).body(response);
} catch (CommonException e) {
log.error("댓글 등록 오류: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (Exception e) {
log.error("예상치 못한 오류", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("예상치 못한 오류가 발생했습니다");
}
}
@PatchMapping("/{commentId}")
public ResponseEntity<?> updateComment(@PathVariable Long commentId, @RequestBody RequestUpdateCommentVO request, @RequestHeader(HttpHeaders.AUTHORIZATION) String token) {
log.info("수정 요청된 commentId: {}, 데이터: {}", commentId, request);
String kakaoId = securityUtil.getKakaoIdFromToken(token.replace("Bearer ", ""));
try {
CommentDTO commentDTO = commentConverter.fromUpdateVOToDTO(request);
commentDTO.setCommentId(commentId);
CommentDTO updatedComment = commentService.updateComment(commentDTO, kakaoId);
ResponseUpdateCommentVO response = commentConverter.fromDTOToUpdateVO(updatedComment);
return ResponseEntity.status(HttpStatus.OK).body(response);
} catch (CommonException e) {
log.error("댓글 수정 오류: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (Exception e) {
log.error("예상치 못한 오류", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("예상치 못한 오류가 발생했습니다");
}
}
@PatchMapping("/deactivate/{commentId}")
public ResponseEntity<?> deleteComment(@PathVariable Long commentId, @RequestHeader(HttpHeaders.AUTHORIZATION) String token) {
log.info("삭제 요청된 댓글 ID : {}", commentId);
String kakaoId = securityUtil.getKakaoIdFromToken(token.replace("Bearer ", ""));
try {
CommentDTO deletedComment = commentService.deleteComment(commentId, kakaoId);
log.info("삭제된 commentId : {}, 해당 댓글의 상태 : {}", deletedComment.getCommentId(), deletedComment.getCommentStatus());
return ResponseEntity.status(HttpStatus.OK).body("댓글이 성공적으로 삭제되었습니다.");
} catch (CommonException e) {
log.error("댓글 삭제 오류: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (Exception e) {
log.error("예상치 못한 오류", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("예상치 못한 오류가 발생했습니다");
}
}
}