-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCommentController.java
More file actions
97 lines (78 loc) · 3.07 KB
/
CommentController.java
File metadata and controls
97 lines (78 loc) · 3.07 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
package com.example.devSns.Comment;
import com.example.devSns.Comment.Dto.CreateCommentDto;
import com.example.devSns.Comment.Dto.UpdateCommentDto;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
public class CommentController {
private final CommentService commentService;
public CommentController(CommentService commentService) {
this.commentService = commentService;
}
@PostMapping("/posts/{post_id}/comments")
public ResponseEntity<?> postComment(
@Valid
@RequestBody CreateCommentDto dto,
@PathVariable Long post_id) {
try{
commentService.createComment(post_id, dto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}catch (EntityNotFoundException e){
return ResponseEntity.notFound().build();
}
}
@PostMapping("/posts/{post_id}/comments/{comment_id}")
public ResponseEntity<?> postReplyComment(
@Valid
@RequestBody CreateCommentDto dto,
@PathVariable Long post_id,
@PathVariable Long comment_id
) {
try{
commentService.createReplyComment(post_id, comment_id, dto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}catch (EntityNotFoundException e){
return ResponseEntity.notFound().build();
}
}
@GetMapping("/posts/{post_id}/comments")
public ResponseEntity<List<Comment>> getComments(@PathVariable("post_id") Long post_id) {
return ResponseEntity.ok().body(commentService.getAllComments(post_id));
}
@GetMapping("/posts/{post_id}/comments/{comment_id}")
public ResponseEntity<Comment> getCommentsByPostId(
@PathVariable("post_id") Long post_id,
@PathVariable("comment_id") Long comment_id) {
try{
return ResponseEntity.ok(commentService.getCommentById(post_id, comment_id));
}catch (EntityNotFoundException e){
return ResponseEntity.notFound().build();
}
}
@PatchMapping("/comments/{comment_id}")
public ResponseEntity<Comment> updateComment(
@PathVariable("comment_id") Long comment_id,
@Valid
@RequestBody UpdateCommentDto dto) {
try{
return ResponseEntity.ok().body(commentService.updateComment(comment_id, dto));
}catch (EntityNotFoundException e){
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/comments/{comment_id}")
public ResponseEntity<?> deleteComment(@PathVariable("comment_id") Long comment_id) {
try{
commentService.deleteCommentById(comment_id);
return ResponseEntity.noContent().build();
}
catch (EntityNotFoundException e){
return ResponseEntity.notFound().build();
}
}
}