forked from osopromadze/Spring-Boot-Blog-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentController.java
More file actions
84 lines (65 loc) · 3.5 KB
/
CommentController.java
File metadata and controls
84 lines (65 loc) · 3.5 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
package com.sopromadze.blogapi.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.sopromadze.blogapi.model.comment.Comment;
import com.sopromadze.blogapi.payload.ApiResponse;
import com.sopromadze.blogapi.payload.CommentRequest;
import com.sopromadze.blogapi.payload.PagedResponse;
import com.sopromadze.blogapi.security.CurrentUser;
import com.sopromadze.blogapi.security.UserPrincipal;
import com.sopromadze.blogapi.service.CommentService;
import com.sopromadze.blogapi.utils.AppConstants;
@RestController
@RequestMapping("/api/posts/{postId}/comments")
public class CommentController {
@Autowired
private CommentService commentService;
@GetMapping
public ResponseEntity<PagedResponse<Comment>> getAllComments(@PathVariable(name = "postId") Long postId,
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
PagedResponse<Comment> allComments = commentService.getAllComments(postId, page, size);
return new ResponseEntity<PagedResponse<Comment>>(allComments, HttpStatus.OK);
}
@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Comment> addComment(@Valid @RequestBody CommentRequest commentRequest,
@PathVariable(name = "postId") Long postId, @CurrentUser UserPrincipal currentUser) {
Comment newComment = commentService.addComment(commentRequest, postId, currentUser);
return new ResponseEntity<Comment>(newComment, HttpStatus.CREATED);
}
@GetMapping("/{id}")
public ResponseEntity<Comment> getComment(@PathVariable(name = "postId") Long postId,
@PathVariable(name = "id") Long id) {
Comment comment = commentService.getComment(postId, id);
return new ResponseEntity<Comment>(comment, HttpStatus.OK);
}
@PutMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<Comment> updateComment(@PathVariable(name = "postId") Long postId,
@PathVariable(name = "id") Long id, @Valid @RequestBody CommentRequest commentRequest,
@CurrentUser UserPrincipal currentUser) {
Comment updatedComment = commentService.updateComment(postId, id, commentRequest, currentUser);
return new ResponseEntity<Comment>(updatedComment, HttpStatus.OK);
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<ApiResponse> deleteComment(@PathVariable(name = "postId") Long postId,
@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
ApiResponse response = commentService.deleteComment(postId, id, currentUser);
HttpStatus status = response.getSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST;
return new ResponseEntity<ApiResponse>(response, status);
}
}