Class CategoryController
This controller provides endpoints for creating, updating, deleting, and retrieving categories.
It delegates business logic to the CategoryService and handles exceptions by returning
appropriate HTTP status codes.
Available Endpoints:
- POST
/api/category/create– Create a new category. - PUT
/api/category/update/{id}– Update an existing category by ID. - DELETE
/api/category/delete/{id}– Delete a category by ID. - GET
/api/category/list– Retrieve all categories. - GET
/api/category/get-by-id– Retrieve a category by its ID. - GET
/api/category/get-by-name– Retrieve a category by its name.
Exception handling is performed at the controller level, returning appropriate
HTTP status codes such as 400 Bad Request, 404 Not Found,
409 Conflict, or 503 Service Unavailable depending on the error.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<?>createCategory(CategoryDto categoryDto) Create a new category.org.springframework.http.ResponseEntity<?>Delete a category by its ID.org.springframework.http.ResponseEntity<?>Retrieve all categories.org.springframework.http.ResponseEntity<?>Retrieve a category by its ID.org.springframework.http.ResponseEntity<?>getCategoryByName(String name) Retrieve a category by its name.org.springframework.http.ResponseEntity<?>updateCategoryById(String id, CategoryDto categoryDto) Update an existing category by its ID.
-
Constructor Details
-
CategoryController
public CategoryController()
-
-
Method Details
-
createCategory
@PostMapping("/create") public org.springframework.http.ResponseEntity<?> createCategory(@RequestBody CategoryDto categoryDto) Create a new category.Accepts a
CategoryDtoin the request body and attempts to persist it. Returns a success message if creation is successful, or an error message with the appropriate HTTP status if validation fails or a duplicate exists.- Parameters:
categoryDto- the category data to create- Returns:
- a response entity containing a success or error message
-
updateCategoryById
@PutMapping("/update/{id}") public org.springframework.http.ResponseEntity<?> updateCategoryById(@PathVariable("id") String id, @RequestBody CategoryDto categoryDto) Update an existing category by its ID.Accepts a category ID as a path variable and a
CategoryDtoin the request body. Returns a success message if the update is successful, or an error message with the appropriate HTTP status if validation fails, the category is not found, or a duplicate exists.- Parameters:
id- the ID of the category to updatecategoryDto- the updated category data- Returns:
- a response entity containing a success or error message
-
deleteCategoryById
@DeleteMapping("/delete/{id}") public org.springframework.http.ResponseEntity<?> deleteCategoryById(@PathVariable("id") String id) Delete a category by its ID.Accepts a category ID as a path variable and deletes the corresponding category. Returns a success message if deletion is successful, or an error message with the appropriate HTTP status if validation fails or the category is not found.
- Parameters:
id- the ID of the category to delete- Returns:
- a response entity containing a success or error message
-
getAllCategory
@GetMapping("/list") public org.springframework.http.ResponseEntity<?> getAllCategory()Retrieve all categories.Returns a list of all categories in the system.
- Returns:
- a response entity containing the list of categories or an error message
-
getCategoryById
@GetMapping("/get-by-id") public org.springframework.http.ResponseEntity<?> getCategoryById(@RequestParam("id") String id) Retrieve a category by its ID.Accepts a category ID as a request parameter and returns the corresponding category. Returns an error message with the appropriate HTTP status if validation fails or the category is not found.
- Parameters:
id- the ID of the category to retrieve- Returns:
- a response entity containing the category or an error message
-
getCategoryByName
@GetMapping("/get-by-name") public org.springframework.http.ResponseEntity<?> getCategoryByName(@RequestParam("name") String name) Retrieve a category by its name.Accepts a category name as a request parameter and returns the corresponding category. Returns an error message with the appropriate HTTP status if validation fails or the category is not found.
- Parameters:
name- the name of the category to retrieve- Returns:
- a response entity containing the category or an error message
-