-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentController.java
More file actions
166 lines (130 loc) · 7.14 KB
/
CommentController.java
File metadata and controls
166 lines (130 loc) · 7.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.codeboy.mvc.controller;
import java.util.ArrayList;
import java.util.List;
import com.codeboy.mvc.model.dto.request.CommentUpdateRequest;
import com.codeboy.mvc.model.dto.response.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.codeboy.mvc.model.dto.Comment;
import com.codeboy.mvc.model.service.CommentService;
@RestController
@RequestMapping("/api/comments")
@Tag(name="Comment RESTful API", description = "Comment CRUD를 할 수 있는 REST API")
public class CommentController {
private final CommentService commentService;
@Autowired
public CommentController(CommentService commentService) {
this.commentService = commentService;
}
@GetMapping("{userProblemSetId}")
//숫자가 아닌 값이 userProblemSetId에 오면 스프링이 컨트롤러에 도달하기전에 400BAD_REQUEST를 보내줌
public ResponseEntity<ApiResponse<List<Comment>>> getAllCommentsById(@PathVariable("userProblemSetId") long userProblemSetId){
List<Comment> comments = commentService.getAllCommentsById(userProblemSetId);
if (comments.isEmpty()) {
//댓글이 달리지 않은 경우
return ResponseEntity.status(HttpStatus.OK)
.body(ApiResponse.success(HttpStatus.OK, "댓글이 없습니다.", new ArrayList<Comment>()));
}
//댓글 조회에 성공한 경우
return ResponseEntity.status(HttpStatus.OK).body(ApiResponse.success(HttpStatus.OK, "댓글 조회 성공", comments));
}
@PostMapping("{userProblemSetId}")
public ResponseEntity<ApiResponse<Void>> addComment(@PathVariable long userProblemSetId,
@RequestBody Comment comment, HttpSession session) {
if (comment.getContent() == null || comment.getContent().isBlank()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.failure(HttpStatus.BAD_REQUEST, "댓글 내용은 비어 있을 수 없습니다."));
}
Long memberId = (Long) session.getAttribute("memberId");
if (memberId == null) {
// 인증 안 됨
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(ApiResponse.failure(HttpStatus.UNAUTHORIZED, "로그인이 필요합니다."));
}
comment.setMemberId(memberId);
int result = commentService.addComment(userProblemSetId, comment);
if (result == 0) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.failure(HttpStatus.BAD_REQUEST, "잘못된 요청. 댓글 추가 실패"));
}
return ResponseEntity.status(HttpStatus.CREATED)
.body(ApiResponse.success(HttpStatus.CREATED, "댓글 추가 성공", null));
}
//리소스의 일부(content)만 수정하므로 패치매핑
@PatchMapping("{userProblemSetId}/{commentId}")
public ResponseEntity<ApiResponse<Void>> updateComment(
@PathVariable long userProblemSetId,
@PathVariable long commentId,
@RequestBody CommentUpdateRequest commentUpdateRequest,
HttpSession session) {
Long memberId = (Long) session.getAttribute("memberId");
if (memberId == null) {
// 인증 안 됨
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(ApiResponse.failure(HttpStatus.UNAUTHORIZED, "로그인이 필요합니다."));
}
if (!memberId.equals(commentUpdateRequest.getMemberId())) {
// 인가 실패
// 로그인 중인 회원이 자신이 작성한 댓글이 아닌 것을 수정하려할 때
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(ApiResponse.failure(HttpStatus.FORBIDDEN, "본인의 댓글만 수정할 수 있습니다."));
}
//댓글이 비어있을 때
if (commentUpdateRequest.getContent() == null || commentUpdateRequest.getContent().isBlank()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.failure(HttpStatus.BAD_REQUEST, "수정할 댓글 내용을 입력해주세요."));
}
Comment comment = new Comment();
comment.setContent(commentUpdateRequest.getContent());
int result = commentService.updateComment(commentId, comment);
if (result == 0) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ApiResponse.failure(HttpStatus.NOT_FOUND, "존재하지 않는 댓글입니다."));
}
return ResponseEntity.status(HttpStatus.OK)
.body(ApiResponse.success(HttpStatus.OK, "댓글 수정 성공", null));
}
@DeleteMapping("{userProblemSetId}/{commentId}")
public ResponseEntity<ApiResponse<Void>> deleteComment(@PathVariable long userProblemSetId,
@PathVariable long commentId,
HttpSession session) {
Long memberId = (Long) session.getAttribute("memberId");
// 로그인 안한 상태
if (memberId == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(ApiResponse.failure(HttpStatus.UNAUTHORIZED, "로그인이 필요합니다."));
}
// DB에서 댓글 작성자 ID 조회
Long ownerId = commentService.getCommentOwnerId(commentId);
// 댓글ID가 존재하지 않는 경우
if (ownerId == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ApiResponse.failure(HttpStatus.NOT_FOUND, "존재하지 않는 댓글입니다."));
}
// 권한 없음 (본인 댓글 아님)
if (!memberId.equals(ownerId)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(ApiResponse.failure(HttpStatus.FORBIDDEN, "본인이 작성한 댓글만 삭제할 수 있습니다."));
}
// 삭제 실행
int result = commentService.deleteComment(commentId);
if (result == 0) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ApiResponse.failure(HttpStatus.INTERNAL_SERVER_ERROR, "댓글 삭제에 실패했습니다."));
}
// 삭제 실패 (DB 오류 등의 상황)
return ResponseEntity.status(HttpStatus.OK)
.body(ApiResponse.success(HttpStatus.OK, "댓글 삭제 성공", null));
}
}