// This is a REST controller in Spring Boot.
It exposes
HTTP endpoints for Category operations.
@RestController
// Lombok annotation to enable logging in the class
using 'log' variable.
@Slf4j
public class CategoryResource {
// Injecting the CategoryService to delegate
business logic to service layer.
private CategoryService catService;
// Constructor-based injection of CategoryService.
public CategoryResource(CategoryService
catService) {
[Link] = catService;
}
// ========== API 1: GET all categories
==========
// Adds Swagger summary and enables JWT bearer
token protection for Swagger UI.
@Operation(summary = "findAllCategories",
security = @SecurityRequirement(name =
"bearerAuth"))
@GetMapping("/categories") // Maps GET request
to /categories URL.
public ResponseEntity<List<CategoryBean>>
findAllCategories() {
// Delegates the call to service layer and wraps
result in HTTP 200 OK response.
return
[Link]([Link]());
}
// ========== API 2: GET child categories of a
parent ==========
@Operation(summary = "findChildCategories",
security = @SecurityRequirement(name =
"bearerAuth"))
@GetMapping("/categories/childs/{parentId}") //
Dynamic URL to fetch child categories by parent ID.
public ResponseEntity<List<CategoryBean>>
findChildCategories(@PathVariable("parentId") int
parentId) {
// Calls service method to get all child
categories of the specified parent.
return
[Link]([Link](parentId));
}
// ========== API 3: CREATE a new category
==========
@Operation(summary = "createCategory", security
= @SecurityRequirement(name = "bearerAuth"))
@PostMapping("/categories") // Maps POST requests
to /categories.
public ResponseEntity<CategoryBean>
createCategory(@RequestBody CategoryBean catBean)
{
// Accepts category details from request body
and sends it to service to save in DB.
return
[Link]([Link](catBean));
}
// ========== API 4: UPDATE an existing
category ==========
@Operation(summary = "updateCategory", security
= @SecurityRequirement(name = "bearerAuth"))
@PutMapping("/categories") // Maps PUT requests
to /categories.
public ResponseEntity<CategoryBean>
updateCategory(@RequestBody CategoryBean catBean)
{
// Accepts updated category details from
request body and delegates update operation to
service.
return
[Link]([Link](catBean))
;
}
// ========== API 5: DELETE a category by ID
==========
@Operation(summary = "deleteCategory", security
= @SecurityRequirement(name = "bearerAuth"))
@DeleteMapping("/categories/{catId}") // Dynamic
URL for DELETE operation using category ID.
public ResponseEntity<List<CategoryBean>>
deleteCategory(@PathVariable("catId") int catId) {
// Calls service to delete category and returns
updated list of categories.
return
[Link]([Link](catId));
}
// ========== API 6: GET category by ID with
CircuitBreaker ==========
@Operation(summary = "getCategoryById", security
= @SecurityRequirement(name = "bearerAuth"))
@GetMapping("/categories/{catId}") // Dynamic GET
URL to fetch a category by its ID.
@CircuitBreaker(name = "categories-circuit") //
Resilience4j Circuit Breaker used to handle failures.
public ResponseEntity<CategoryBean>
getCategoryById(@PathVariable("catId") int catId) {
// Logging the access of this endpoint for debug
purposes.
[Link]("get category by caegory id is
invoking");
// Delegating the fetch operation to service and
returning the result in HTTP 200 response.
return
[Link]([Link](catId));
}
}