4
4
import com .springboot .blog .payload .PostResponse ;
5
5
import com .springboot .blog .service .PostService ;
6
6
import com .springboot .blog .utils .AppConstants ;
7
+ import io .swagger .v3 .oas .annotations .Operation ;
8
+ import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
9
+ import io .swagger .v3 .oas .annotations .security .SecurityRequirement ;
10
+ import io .swagger .v3 .oas .annotations .tags .Tag ;
7
11
import org .springframework .http .HttpStatus ;
8
12
import org .springframework .http .ResponseEntity ;
9
13
import org .springframework .security .access .prepost .PreAuthorize ;
13
17
import java .util .List ;
14
18
15
19
@ RestController
16
- @ RequestMapping ()
20
+ @ RequestMapping ("/api/posts" )
21
+ @ Tag (
22
+ name = "CRUD REST APIs for Post Resource"
23
+ )
17
24
public class PostController {
18
25
19
26
private PostService postService ;
@@ -22,15 +29,34 @@ public PostController(PostService postService) {
22
29
this .postService = postService ;
23
30
}
24
31
32
+ @ Operation (
33
+ summary = "Create Post REST API" ,
34
+ description = "Create Post REST API is used to save post into database"
35
+ )
36
+ @ ApiResponse (
37
+ responseCode = "201" ,
38
+ description = "Http Status 201 CREATED"
39
+ )
40
+ @ SecurityRequirement (
41
+ name = "Bear Authentication"
42
+ )
25
43
// create blog post rest api
26
44
@ PreAuthorize ("hasRole('ADMIN')" )
27
- @ PostMapping ( "/api/v1/posts" )
45
+ @ PostMapping
28
46
public ResponseEntity <PostDto > createPost (@ Valid @ RequestBody PostDto postDto ){
29
47
return new ResponseEntity <>(postService .createPost (postDto ), HttpStatus .CREATED );
30
48
}
31
49
50
+ @ Operation (
51
+ summary = "Get All Posts REST API" ,
52
+ description = "Get All Posts REST API is used to fetch all the posts from the database"
53
+ )
54
+ @ ApiResponse (
55
+ responseCode = "200" ,
56
+ description = "Http Status 200 SUCCESS"
57
+ )
32
58
// get all posts rest api
33
- @ GetMapping ( "/api/v1/posts" )
59
+ @ GetMapping
34
60
public PostResponse getAllPosts (
35
61
@ RequestParam (value = "pageNo" , defaultValue = AppConstants .DEFAULT_PAGE_NUMBER , required = false ) int pageNo ,
36
62
@ RequestParam (value = "pageSize" , defaultValue = AppConstants .DEFAULT_PAGE_SIZE , required = false ) int pageSize ,
@@ -40,25 +66,55 @@ public PostResponse getAllPosts(
40
66
return postService .getAllPosts (pageNo , pageSize , sortBy , sortDir );
41
67
}
42
68
69
+ @ Operation (
70
+ summary = "Get Post By Id REST API" ,
71
+ description = "Get Post By Id REST API is used to get single post from the database"
72
+ )
73
+ @ ApiResponse (
74
+ responseCode = "200" ,
75
+ description = "Http Status 200 SUCCESS"
76
+ )
43
77
// get post by id
44
- @ GetMapping (value = "/api/v1/posts /{id}" )
78
+ @ GetMapping (" /{id}" )
45
79
public ResponseEntity <PostDto > getPostById (@ PathVariable (name = "id" ) long id ){
46
80
return ResponseEntity .ok (postService .getPostById (id ));
47
81
}
48
82
83
+ @ Operation (
84
+ summary = "update Post REST API" ,
85
+ description = "Update Post REST API is used to update a particular post in the database"
86
+ )
87
+ @ ApiResponse (
88
+ responseCode = "200" ,
89
+ description = "Http Status 200 SUCCESS"
90
+ )
91
+ @ SecurityRequirement (
92
+ name = "Bear Authentication"
93
+ )
49
94
// update post by id rest api
50
95
@ PreAuthorize ("hasRole('ADMIN')" )
51
- @ PutMapping ("/api/v1/posts/ {id}" )
96
+ @ PutMapping ("/{id}" )
52
97
public ResponseEntity <PostDto > updatePost (@ Valid @ RequestBody PostDto postDto , @ PathVariable (name = "id" ) long id ){
53
98
54
99
PostDto postResponse = postService .updatePost (postDto , id );
55
100
56
101
return new ResponseEntity <>(postResponse , HttpStatus .OK );
57
102
}
58
103
104
+ @ Operation (
105
+ summary = "Delete Post REST API" ,
106
+ description = "Delete Post REST API is used to delete a particular post from the database"
107
+ )
108
+ @ ApiResponse (
109
+ responseCode = "200" ,
110
+ description = "Http Status 200 SUCCESS"
111
+ )
112
+ @ SecurityRequirement (
113
+ name = "Bear Authentication"
114
+ )
59
115
// delete post rest api
60
116
@ PreAuthorize ("hasRole('ADMIN')" )
61
- @ DeleteMapping ("/api/v1/posts/ {id}" )
117
+ @ DeleteMapping ("/{id}" )
62
118
public ResponseEntity <String > deletePost (@ PathVariable (name = "id" ) long id ){
63
119
64
120
postService .deletePostById (id );
@@ -68,7 +124,7 @@ public ResponseEntity<String> deletePost(@PathVariable(name = "id") long id){
68
124
69
125
// Build Get Posts by Category REST API
70
126
// http://localhost:8080/api/posts/category/3
71
- @ GetMapping ("/api/v1/posts/ category/{id}" )
127
+ @ GetMapping ("/category/{id}" )
72
128
public ResponseEntity <List <PostDto >> getPostsByCategory (@ PathVariable ("id" ) Long categoryId ){
73
129
List <PostDto > postDtos = postService .getPostsByCategory (categoryId );
74
130
return ResponseEntity .ok (postDtos );
0 commit comments