11package com .answerdigital .answerking .controller ;
22
33import com .answerdigital .answerking .exception .util .ErrorResponse ;
4- import com .answerdigital .answerking .model .Category ;
54import com .answerdigital .answerking .request .CategoryRequest ;
65import com .answerdigital .answerking .response .CategoryResponse ;
76import com .answerdigital .answerking .response .ProductResponse ;
1615import org .springframework .http .HttpStatus ;
1716import org .springframework .http .ResponseEntity ;
1817import org .springframework .validation .annotation .Validated ;
18+ import org .springframework .web .bind .annotation .DeleteMapping ;
19+ import org .springframework .web .bind .annotation .GetMapping ;
1920import org .springframework .web .bind .annotation .PathVariable ;
21+ import org .springframework .web .bind .annotation .PostMapping ;
22+ import org .springframework .web .bind .annotation .PutMapping ;
2023import org .springframework .web .bind .annotation .RequestBody ;
2124import org .springframework .web .bind .annotation .RequestMapping ;
2225import org .springframework .web .bind .annotation .RestController ;
23- import org .springframework .web .bind .annotation .GetMapping ;
24- import org .springframework .web .bind .annotation .PostMapping ;
25- import org .springframework .web .bind .annotation .PutMapping ;
26- import org .springframework .web .bind .annotation .DeleteMapping ;
2726
2827import javax .validation .Valid ;
2928import javax .validation .constraints .NotNull ;
@@ -45,9 +44,9 @@ public CategoryController(final CategoryService categoryService) {
4544 @ Operation (summary = "Create a new category." )
4645 @ ApiResponses (value = {
4746 @ ApiResponse (responseCode = "201" , description = "When the category has been created." ,
48- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) }),
47+ content = {@ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class ))}),
4948 @ ApiResponse (responseCode = "400" , description = "When invalid parameters are provided." ,
50- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
49+ content = {@ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class ))})
5150 })
5251 @ PostMapping
5352 public ResponseEntity <CategoryResponse > addCategory (@ Valid @ RequestBody final CategoryRequest categoryRequest ) {
@@ -57,7 +56,7 @@ public ResponseEntity<CategoryResponse> addCategory(@Valid @RequestBody final Ca
5756 @ Operation (summary = "Get all categories." )
5857 @ ApiResponses (value = {
5958 @ ApiResponse (responseCode = "200" , description = "Found the list of categories." ,
60- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) })
59+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) })
6160 })
6261 @ GetMapping
6362 public ResponseEntity <Collection <CategoryResponse >> getAllCategories () {
@@ -68,21 +67,21 @@ public ResponseEntity<Collection<CategoryResponse>> getAllCategories() {
6867 @ Operation (summary = "Get a single category." )
6968 @ ApiResponses (value = {
7069 @ ApiResponse (responseCode = "200" , description = "When the category with the provided id has been found." ,
71- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) }),
70+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) }),
7271 @ ApiResponse (responseCode = "404" , description = "When the category with the given id does not exist." ,
73- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
72+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
7473 })
7574 @ GetMapping ("/{categoryId}" )
76- public ResponseEntity <Category > getCategoryById (@ PathVariable @ NotNull final Long categoryId ) {
77- return new ResponseEntity <>(categoryService .findById (categoryId ), HttpStatus .OK );
75+ public ResponseEntity <CategoryResponse > getCategoryById (@ PathVariable @ NotNull final Long categoryId ) {
76+ return new ResponseEntity <>(categoryService .findByIdResponse (categoryId ), HttpStatus .OK );
7877 }
7978
8079 @ Operation (summary = "Get all products in a category." )
8180 @ ApiResponses (value = {
8281 @ ApiResponse (responseCode = "200" , description = "When all the products have been returned." ,
83- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) }),
82+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) }),
8483 @ ApiResponse (responseCode = "404" , description = "When the category with the given id does not exist." ,
85- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
84+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
8685 })
8786 @ GetMapping ("/{categoryId}/products" )
8887 public ResponseEntity <Collection <ProductResponse >> fetchProductsByCategory (@ PathVariable @ NotNull final Long categoryId ) {
@@ -92,53 +91,54 @@ public ResponseEntity<Collection<ProductResponse>> fetchProductsByCategory(@Path
9291 @ Operation (summary = "Add product to a category." )
9392 @ ApiResponses (value = {
9493 @ ApiResponse (responseCode = "200" , description = "Add product to a category." ,
95- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) })
94+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) })
9695 })
9796 @ PutMapping ("/{categoryId}/addproduct/{productId}" )
98- public ResponseEntity <Category > addProductToCategory (@ PathVariable @ NotNull final Long categoryId ,
97+ public ResponseEntity <CategoryResponse > addProductToCategory (@ PathVariable @ NotNull final Long categoryId ,
9998 @ PathVariable @ NotNull final Long productId ) {
10099 return new ResponseEntity <>(categoryService .addProductToCategory (categoryId , productId ), HttpStatus .OK );
101100 }
102101
103102 @ Operation (summary = "Remove product from a category." )
104103 @ ApiResponses (value = {
105104 @ ApiResponse (responseCode = "200" , description = "Remove product from a category." ,
106- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) })
105+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) })
107106 })
108107 @ PutMapping ("/{categoryId}/removeproduct/{productId}" )
109- public ResponseEntity <Category > removeProductFromCategory (@ PathVariable @ NotNull final Long categoryId ,
108+ public ResponseEntity <CategoryResponse > removeProductFromCategory (@ PathVariable @ NotNull final Long categoryId ,
110109 @ PathVariable @ NotNull final Long productId ) {
111110 return new ResponseEntity <>(categoryService .removeProductFromCategory (categoryId , productId ), HttpStatus .OK );
112111 }
113112
114113 @ Operation (summary = "Update an existing category." )
115114 @ ApiResponses (value = {
116115 @ ApiResponse (responseCode = "200" , description = "When the category has been updated." ,
117- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) }),
116+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) }),
118117 @ ApiResponse (responseCode = "400" , description = "When invalid parameters are provided." ,
119- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
118+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
120119 @ ApiResponse (responseCode = "404" , description = "When the category with the given id does not exist." ,
121- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
120+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
122121 })
123122 @ PutMapping ("/{categoryId}" )
124- public ResponseEntity <Category > updateCategory (@ Valid @ RequestBody final CategoryRequest categoryRequest ,
123+ public ResponseEntity <CategoryResponse > updateCategory (@ Valid @ RequestBody final CategoryRequest categoryRequest ,
125124 @ PathVariable @ NotNull final Long categoryId ) {
126125 return new ResponseEntity <>(categoryService .updateCategory (categoryRequest , categoryId ), HttpStatus .OK );
127126 }
128127
129128 @ Operation (summary = "Retire an existing category." )
130129 @ ApiResponses (value = {
131130 @ ApiResponse (responseCode = "200" , description = "When the category has been retired." ,
132- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Category .class )) }),
131+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = CategoryResponse .class )) }),
133132 @ ApiResponse (responseCode = "400" , description = "When invalid parameters are provided." ,
134- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
133+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
135134 @ ApiResponse (responseCode = "404" , description = "When the category with the given id does not exist." ,
136- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
135+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
137136 @ ApiResponse (responseCode = "410" , description = "When the category with the given id is already retired." ,
138- content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
137+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
139138 })
140139 @ DeleteMapping ("/{categoryId}" )
141- public ResponseEntity <Category > retireCategory (@ PathVariable @ NotNull final Long categoryId ) {
142- return new ResponseEntity <>(categoryService .retireCategory (categoryId ), HttpStatus .OK );
140+ public ResponseEntity <Void > retireCategory (@ PathVariable @ NotNull final Long categoryId ) {
141+ categoryService .retireCategory (categoryId );
142+ return new ResponseEntity <>(HttpStatus .NO_CONTENT );
143143 }
144144}
0 commit comments