Class CategoryController

java.lang.Object
com.glowmart.shop_management.controller.CategoryController

@RestController @RequestMapping("/api/category") public class CategoryController extends Object
REST controller for managing category operations.

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 Details

    • CategoryController

      public CategoryController()
  • Method Details

    • createCategory

      @PostMapping("/create") public org.springframework.http.ResponseEntity<?> createCategory(@RequestBody CategoryDto categoryDto)
      Create a new category.

      Accepts a CategoryDto in 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 CategoryDto in 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 update
      categoryDto - 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