forked from RameshMF/Spring-Boot-Blog-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostController.java
More file actions
95 lines (78 loc) · 3.99 KB
/
PostController.java
File metadata and controls
95 lines (78 loc) · 3.99 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
package com.sopromadze.blogapi.controller;
import com.sopromadze.blogapi.model.Post;
import com.sopromadze.blogapi.payload.ApiResponse;
import com.sopromadze.blogapi.payload.PagedResponse;
import com.sopromadze.blogapi.payload.PostRequest;
import com.sopromadze.blogapi.payload.PostResponse;
import com.sopromadze.blogapi.security.CurrentUser;
import com.sopromadze.blogapi.security.UserPrincipal;
import com.sopromadze.blogapi.service.PostService;
import com.sopromadze.blogapi.utils.AppConstants;
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 javax.validation.Valid;
@RestController
@RequestMapping("/api/posts")
public class PostController {
@Autowired
private PostService postService;
@GetMapping
public ResponseEntity<PagedResponse<Post>> getAllPosts(
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
PagedResponse<Post> response = postService.getAllPosts(page, size);
return new ResponseEntity< >(response, HttpStatus.OK);
}
@GetMapping("/category/{id}")
public ResponseEntity<PagedResponse<Post>> getPostsByCategory(
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size,
@PathVariable(name = "id") Long id) {
PagedResponse<Post> response = postService.getPostsByCategory(id, page, size);
return new ResponseEntity< >(response, HttpStatus.OK);
}
@GetMapping("/tag/{id}")
public ResponseEntity<PagedResponse<Post>> getPostsByTag(
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size,
@PathVariable(name = "id") Long id) {
PagedResponse<Post> response = postService.getPostsByTag(id, page, size);
return new ResponseEntity< >(response, HttpStatus.OK);
}
@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<PostResponse> addPost(@Valid @RequestBody PostRequest postRequest,
@CurrentUser UserPrincipal currentUser) {
PostResponse postResponse = postService.addPost(postRequest, currentUser);
return new ResponseEntity< >(postResponse, HttpStatus.CREATED);
}
@GetMapping("/{id}")
public ResponseEntity<Post> getPost(@PathVariable(name = "id") Long id) {
Post post = postService.getPost(id);
return new ResponseEntity< >(post, HttpStatus.OK);
}
@PutMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<Post> updatePost(@PathVariable(name = "id") Long id,
@Valid @RequestBody PostRequest newPostRequest, @CurrentUser UserPrincipal currentUser) {
Post post = postService.updatePost(id, newPostRequest, currentUser);
return new ResponseEntity< >(post, HttpStatus.OK);
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<ApiResponse> deletePost(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
ApiResponse apiResponse = postService.deletePost(id, currentUser);
return new ResponseEntity< >(apiResponse, HttpStatus.OK);
}
}