11package com .answerdigital .answerking .controller ;
22
3+ import com .answerdigital .answerking .exception .util .ErrorResponse ;
34import com .answerdigital .answerking .model .Product ;
45import com .answerdigital .answerking .request .ProductRequest ;
56import com .answerdigital .answerking .service .ProductService ;
7+ import io .swagger .v3 .oas .annotations .Operation ;
8+ import io .swagger .v3 .oas .annotations .media .Content ;
9+ import io .swagger .v3 .oas .annotations .media .Schema ;
10+ import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
11+ import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
612import io .swagger .v3 .oas .annotations .tags .Tag ;
713import org .springframework .beans .factory .annotation .Autowired ;
814import org .springframework .http .HttpStatus ;
@@ -34,23 +40,51 @@ public ProductController(final ProductService productService) {
3440 this .productService = productService ;
3541 }
3642
43+ @ Operation (summary = "Get all products." )
44+ @ ApiResponses (value = {
45+ @ ApiResponse (responseCode = "200" , description = "When all the products have been returned." ,
46+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Product .class )) })
47+ })
3748 @ GetMapping
3849 public ResponseEntity <List <Product >> getAllProducts () {
3950 final List <Product > products = productService .findAll ();
4051 return new ResponseEntity <>(products , products .isEmpty () ? HttpStatus .NO_CONTENT : HttpStatus .OK );
4152 }
4253
54+ @ ApiResponses (value = {
55+ @ ApiResponse (responseCode = "200" , description = "When the product with the provided id has been found." ,
56+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Product .class )) }),
57+ @ ApiResponse (responseCode = "404" , description = "When the product with the given id does not exist." ,
58+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
59+ })
4360 @ GetMapping (path = "/{id}" )
44- public ResponseEntity <Product > getProductById (@ Valid @ PathVariable @ NotNull final Long id ) {
45- return new ResponseEntity <>(productService .findById (id ), HttpStatus .OK );
61+ public ResponseEntity <Product > getProductById (@ Valid @ PathVariable @ NotNull final Long id , final Errors errors ) {
62+ return new ResponseEntity <>(productService .findById (id ),
63+ errors .hasErrors () ? HttpStatus .NOT_FOUND : HttpStatus .OK );
4664 }
4765
66+ @ Operation (summary = "Create a new product." )
67+ @ ApiResponses (value = {
68+ @ ApiResponse (responseCode = "201" , description = "When the product has been created." ,
69+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Product .class )) }),
70+ @ ApiResponse (responseCode = "400" , description = "When invalid parameters are provided." ,
71+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
72+ })
4873 @ PostMapping
4974 public ResponseEntity <Product > addProduct (@ Valid @ RequestBody final ProductRequest productRequest , final Errors errors ) {
5075 return new ResponseEntity <>(productService .addNewProduct (productRequest ),
5176 errors .hasErrors () ? HttpStatus .BAD_REQUEST : HttpStatus .CREATED );
5277 }
5378
79+ @ Operation (summary = "Update an existing product." )
80+ @ ApiResponses (value = {
81+ @ ApiResponse (responseCode = "200" , description = "When the product has been updated." ,
82+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Product .class )) }),
83+ @ ApiResponse (responseCode = "400" , description = "When invalid parameters are provided." ,
84+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
85+ @ ApiResponse (responseCode = "404" , description = "When the product with the given id does not exist." ,
86+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
87+ })
5488 @ PutMapping ("/{id}" )
5589 public ResponseEntity <Product > updateProduct (@ PathVariable @ NotNull final Long id ,
5690 @ Valid @ RequestBody final ProductRequest productRequest ,
@@ -59,6 +93,15 @@ public ResponseEntity<Product> updateProduct(@PathVariable @NotNull final Long i
5993 errors .hasErrors () ? HttpStatus .BAD_REQUEST : HttpStatus .OK );
6094 }
6195
96+ @ Operation (summary = "Retire an existing product." )
97+ @ ApiResponses (value = {
98+ @ ApiResponse (responseCode = "200" , description = "When the product has been retired." ,
99+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Product .class )) }),
100+ @ ApiResponse (responseCode = "404" , description = "When the product with the given id does not exist." ,
101+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) }),
102+ @ ApiResponse (responseCode = "410" , description = "When the product with the given id is already retired." ,
103+ content = { @ Content (mediaType = "application/json" , schema = @ Schema (implementation = ErrorResponse .class )) })
104+ })
62105 @ DeleteMapping ("/{id}" )
63106 public ResponseEntity <Product > retireProduct (@ PathVariable @ NotNull final Long id ) {
64107 return new ResponseEntity <>(productService .retireProduct (id ), HttpStatus .OK );
0 commit comments