Skip to content

Commit 97165a5

Browse files
Defined API responses for product HTTP requests
1 parent 698fdb8 commit 97165a5

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/main/java/com/answerdigital/answerking/controller/ProductController.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.answerdigital.answerking.controller;
22

3+
import com.answerdigital.answerking.exception.util.ErrorResponse;
34
import com.answerdigital.answerking.model.Product;
45
import com.answerdigital.answerking.request.ProductRequest;
56
import 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;
612
import io.swagger.v3.oas.annotations.tags.Tag;
713
import org.springframework.beans.factory.annotation.Autowired;
814
import 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);

src/main/java/com/answerdigital/answerking/model/Product.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnore;
44
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
56
import lombok.Builder;
67
import lombok.Getter;
78
import lombok.NoArgsConstructor;
@@ -37,6 +38,7 @@
3738
public class Product {
3839
@Id
3940
@GeneratedValue(strategy = GenerationType.IDENTITY)
41+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
4042
private Long id;
4143

4244
private String name;
@@ -46,6 +48,7 @@ public class Product {
4648
@Column(precision = 12, scale = 2)
4749
private BigDecimal price;
4850

51+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
4952
private boolean retired;
5053

5154
@JsonIgnore

0 commit comments

Comments
 (0)