Skip to content

Commit d36ec44

Browse files
author
hospel
authored
adding more swagger docs (#80)
1 parent 219b534 commit d36ec44

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/main/java/com/answerdigital/answerking/config/SwaggerConfig.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ public GroupedOpenApi actuatorApi() {
3030
@Bean
3131
public OpenAPI springShopOpenAPI() {
3232
return new OpenAPI()
33-
.info(new Info().title("AnswerKing API")
34-
.description("Answer King application")
35-
.version("v0.0.1")
36-
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
33+
.info(new Info()
34+
.title("AnswerKing API")
35+
.description("Answer King application")
36+
.version("v1")
37+
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
3738
}
3839

3940
}

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

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

3+
import com.answerdigital.answerking.exception.util.ErrorResponse;
34
import com.answerdigital.answerking.model.Category;
45
import com.answerdigital.answerking.request.AddCategoryRequest;
56
import com.answerdigital.answerking.request.UpdateCategoryRequest;
@@ -41,16 +42,23 @@ public CategoryController(final CategoryService categoryService) {
4142
this.categoryService = categoryService;
4243
}
4344

45+
@Operation(summary = "Create a new category")
46+
@ApiResponses(value = {
47+
@ApiResponse(responseCode = "201", description = "When the category has been created.",
48+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }),
49+
@ApiResponse(responseCode = "400", description = "When invalid parameters are provided.",
50+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
51+
})
4452
@PostMapping
4553
public ResponseEntity<Category> addCategory(@Valid @RequestBody final AddCategoryRequest categoryRequest,
4654
final Errors errors) {
4755
return new ResponseEntity<>(categoryService.addCategory(categoryRequest),
4856
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.CREATED);
4957
}
5058

51-
@Operation(summary = "Get all categories.")
59+
@Operation(summary = "Get all categories")
5260
@ApiResponses(value = {
53-
@ApiResponse(responseCode = "200", description = "Found the the list of categories",
61+
@ApiResponse(responseCode = "200", description = "Found the list of categories",
5462
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) })
5563
})
5664
@GetMapping
@@ -59,23 +67,49 @@ public ResponseEntity<Collection<Category>> getAllCategories() {
5967
return new ResponseEntity<>(categories, categories.isEmpty() ? HttpStatus.NO_CONTENT : HttpStatus.OK);
6068
}
6169

70+
@Operation(summary = "Get a single category")
71+
@ApiResponses(value = {
72+
@ApiResponse(responseCode = "200", description = "When the category with the provided id has been found.",
73+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }),
74+
@ApiResponse(responseCode = "404", description = "When the category with the given id does not exist",
75+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
76+
})
6277
@GetMapping("/{categoryId}")
6378
public ResponseEntity<Category> getCategoryById(@PathVariable @NotNull final Long categoryId) {
6479
return new ResponseEntity<>(categoryService.findById(categoryId), HttpStatus.OK);
6580
}
6681

82+
@Operation(summary = "Add product to a category")
83+
@ApiResponses(value = {
84+
@ApiResponse(responseCode = "200", description = "Add product to a category.",
85+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) })
86+
})
6787
@PutMapping("/{categoryId}/addproduct/{productId}")
6888
public ResponseEntity<Category> addProductToCategory(@PathVariable @NotNull final Long categoryId,
6989
@PathVariable @NotNull final Long productId) {
7090
return new ResponseEntity<>(categoryService.addProductToCategory(categoryId, productId), HttpStatus.OK);
7191
}
7292

93+
@Operation(summary = "Remove product from a category")
94+
@ApiResponses(value = {
95+
@ApiResponse(responseCode = "200", description = "Remove product from a category.",
96+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) })
97+
})
7398
@PutMapping("/{categoryId}/removeproduct/{productId}")
7499
public ResponseEntity<Category> removeProductFromCategory(@PathVariable @NotNull final Long categoryId,
75100
@PathVariable @NotNull final Long productId) {
76101
return new ResponseEntity<>(categoryService.removeProductFromCategory(categoryId, productId), HttpStatus.OK);
77102
}
78103

104+
@Operation(summary = "Update an existing category.")
105+
@ApiResponses(value = {
106+
@ApiResponse(responseCode = "200", description = "When the category has been updated.",
107+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }),
108+
@ApiResponse(responseCode = "400", description = "When invalid parameters are provided.",
109+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }),
110+
@ApiResponse(responseCode = "404", description = "When the category with the given id does not exist.",
111+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
112+
})
79113
@PutMapping("/{categoryId}")
80114
public ResponseEntity<Category> updateCategory(@Valid @RequestBody final UpdateCategoryRequest updateCategoryRequest,
81115
@PathVariable @NotNull final Long categoryId,
@@ -84,6 +118,17 @@ public ResponseEntity<Category> updateCategory(@Valid @RequestBody final UpdateC
84118
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
85119
}
86120

121+
@Operation(summary = "Retire an existing category.")
122+
@ApiResponses(value = {
123+
@ApiResponse(responseCode = "200", description = "When the category has been retired.",
124+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }),
125+
@ApiResponse(responseCode = "400", description = "When invalid parameters are provided.",
126+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }),
127+
@ApiResponse(responseCode = "404", description = "When the category with the given id does not exist.",
128+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }),
129+
@ApiResponse(responseCode = "410", description = "When the category with the given id is already retired.",
130+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
131+
})
87132
@DeleteMapping("/{categoryId}")
88133
public ResponseEntity<Category> retireCategory(@PathVariable @NotNull final Long categoryId) {
89134
return new ResponseEntity<>(categoryService.retireCategory(categoryId), HttpStatus.OK);

0 commit comments

Comments
 (0)