|
1 | 1 | package com.answerdigital.answerking.controller; |
2 | 2 |
|
| 3 | +import com.answerdigital.answerking.exception.util.ErrorResponse; |
3 | 4 | import com.answerdigital.answerking.model.Category; |
4 | 5 | import com.answerdigital.answerking.request.RequestModelsCategory; |
| 6 | +import com.answerdigital.answerking.response.CategoryResponse; |
5 | 7 | import com.answerdigital.answerking.service.CategoryService; |
6 | 8 | import io.swagger.v3.oas.annotations.Operation; |
7 | 9 | import io.swagger.v3.oas.annotations.media.Content; |
@@ -39,45 +41,89 @@ public CategoryController(final CategoryService categoryService) { |
39 | 41 | this.categoryService = categoryService; |
40 | 42 | } |
41 | 43 |
|
| 44 | + @Operation(summary = "Create a new category.") |
| 45 | + @ApiResponses(value = { |
| 46 | + @ApiResponse(responseCode = "201", description = "When the category has been created.", |
| 47 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }), |
| 48 | + @ApiResponse(responseCode = "400", description = "When invalid parameters are provided.", |
| 49 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }) |
| 50 | + }) |
42 | 51 | @PostMapping |
43 | | - public ResponseEntity<Category> addCategory(@Valid @RequestBody final RequestModelsCategory categoryRequest) { |
| 52 | + public ResponseEntity<CategoryResponse> addCategory(@Valid @RequestBody final RequestModelsCategory categoryRequest) { |
44 | 53 | return new ResponseEntity<>(categoryService.addCategory(categoryRequest), HttpStatus.CREATED); |
45 | 54 | } |
46 | 55 |
|
47 | 56 | @Operation(summary = "Get all categories.") |
48 | 57 | @ApiResponses(value = { |
49 | | - @ApiResponse(responseCode = "200", description = "Found the the list of categories", |
| 58 | + @ApiResponse(responseCode = "200", description = "Found the list of categories.", |
50 | 59 | content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }) |
51 | 60 | }) |
52 | 61 | @GetMapping |
53 | | - public ResponseEntity<Collection<Category>> getAllCategories() { |
54 | | - final Set<Category> categories = categoryService.findAll(); |
| 62 | + public ResponseEntity<Collection<CategoryResponse>> getAllCategories() { |
| 63 | + final Set<CategoryResponse> categories = categoryService.findAll(); |
55 | 64 | return new ResponseEntity<>(categories, categories.isEmpty() ? HttpStatus.NO_CONTENT : HttpStatus.OK); |
56 | 65 | } |
57 | 66 |
|
| 67 | + @Operation(summary = "Get a single category.") |
| 68 | + @ApiResponses(value = { |
| 69 | + @ApiResponse(responseCode = "200", description = "When the category with the provided id has been found.", |
| 70 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }), |
| 71 | + @ApiResponse(responseCode = "404", description = "When the category with the given id does not exist.", |
| 72 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }) |
| 73 | + }) |
58 | 74 | @GetMapping("/{categoryId}") |
59 | 75 | public ResponseEntity<Category> getCategoryById(@PathVariable @NotNull final Long categoryId) { |
60 | 76 | return new ResponseEntity<>(categoryService.findById(categoryId), HttpStatus.OK); |
61 | 77 | } |
62 | 78 |
|
| 79 | + @Operation(summary = "Add product to a category.") |
| 80 | + @ApiResponses(value = { |
| 81 | + @ApiResponse(responseCode = "200", description = "Add product to a category.", |
| 82 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }) |
| 83 | + }) |
63 | 84 | @PutMapping("/{categoryId}/addproduct/{productId}") |
64 | 85 | public ResponseEntity<Category> addProductToCategory(@PathVariable @NotNull final Long categoryId, |
65 | 86 | @PathVariable @NotNull final Long productId) { |
66 | 87 | return new ResponseEntity<>(categoryService.addProductToCategory(categoryId, productId), HttpStatus.OK); |
67 | 88 | } |
68 | 89 |
|
| 90 | + @Operation(summary = "Remove product from a category.") |
| 91 | + @ApiResponses(value = { |
| 92 | + @ApiResponse(responseCode = "200", description = "Remove product from a category.", |
| 93 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }) |
| 94 | + }) |
69 | 95 | @PutMapping("/{categoryId}/removeproduct/{productId}") |
70 | 96 | public ResponseEntity<Category> removeProductFromCategory(@PathVariable @NotNull final Long categoryId, |
71 | 97 | @PathVariable @NotNull final Long productId) { |
72 | 98 | return new ResponseEntity<>(categoryService.removeProductFromCategory(categoryId, productId), HttpStatus.OK); |
73 | 99 | } |
74 | 100 |
|
| 101 | + @Operation(summary = "Update an existing category.") |
| 102 | + @ApiResponses(value = { |
| 103 | + @ApiResponse(responseCode = "200", description = "When the category has been updated.", |
| 104 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }), |
| 105 | + @ApiResponse(responseCode = "400", description = "When invalid parameters are provided.", |
| 106 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }), |
| 107 | + @ApiResponse(responseCode = "404", description = "When the category with the given id does not exist.", |
| 108 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }) |
| 109 | + }) |
75 | 110 | @PutMapping("/{categoryId}") |
76 | 111 | public ResponseEntity<Category> updateCategory(@Valid @RequestBody final RequestModelsCategory updateCategoryRequest, |
77 | 112 | @PathVariable @NotNull final Long categoryId) { |
78 | 113 | return new ResponseEntity<>(categoryService.updateCategory(updateCategoryRequest, categoryId), HttpStatus.OK); |
79 | 114 | } |
80 | 115 |
|
| 116 | + @Operation(summary = "Retire an existing category.") |
| 117 | + @ApiResponses(value = { |
| 118 | + @ApiResponse(responseCode = "200", description = "When the category has been retired.", |
| 119 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) }), |
| 120 | + @ApiResponse(responseCode = "400", description = "When invalid parameters are provided.", |
| 121 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }), |
| 122 | + @ApiResponse(responseCode = "404", description = "When the category with the given id does not exist.", |
| 123 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }), |
| 124 | + @ApiResponse(responseCode = "410", description = "When the category with the given id is already retired.", |
| 125 | + content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) }) |
| 126 | + }) |
81 | 127 | @DeleteMapping("/{categoryId}") |
82 | 128 | public ResponseEntity<Category> retireCategory(@PathVariable @NotNull final Long categoryId) { |
83 | 129 | return new ResponseEntity<>(categoryService.retireCategory(categoryId), HttpStatus.OK); |
|
0 commit comments