Class ProductController

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

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

This controller provides endpoints for creating, updating, deleting, and retrieving products. It delegates business logic to the ProductService and handles exceptions by returning appropriate HTTP status codes.

Available Endpoints:

  • POST /api/product/create – Create a new product with details and image.
  • PUT /api/product/update/{id} – Update an existing product by ID.
  • DELETE /api/product/delete/{id} – Delete a product by ID.
  • GET /api/product/get-by-id – Retrieve a product by its ID.

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

    • ProductController

      public ProductController()
  • Method Details

    • createProduct

      @PostMapping(value="/create", consumes="multipart/form-data") public org.springframework.http.ResponseEntity<?> createProduct(@RequestParam("categoryName") String categoryName, @RequestParam("active") boolean active, @RequestParam("product") String productJson, @RequestParam("image") org.springframework.web.multipart.MultipartFile file) throws IOException
      Create a new product.

      Accepts product details and an image file via multipart form data. The product is associated with a category and marked as active or inactive. Returns a success message if creation is successful, or an error message with the appropriate HTTP status if validation fails or a duplicate product exists.

      Parameters:
      categoryName - the name of the category to which the product belongs
      active - whether the product is active
      productJson - the product details in JSON format
      file - the image file associated with the product
      Returns:
      a response entity containing a success or error message
      Throws:
      IOException - if an error occurs while processing the image file
    • updateProductById

      @PutMapping("/update/{id}") public org.springframework.http.ResponseEntity<?> updateProductById(@PathVariable("id") String id, @RequestParam("categoryName") String categoryName, @RequestParam("active") boolean active, @RequestParam("image") org.springframework.web.multipart.MultipartFile file, @RequestParam("product") String productJson) throws IOException
      Update an existing product by its ID.

      Accepts a product ID as a path variable, product details in JSON format, and an image file. Returns a success message if the update is successful, or an error message with the appropriate HTTP status if validation fails, the product is not found, or a duplicate exists.

      Parameters:
      id - the ID of the product to update
      categoryName - the name of the category to which the product belongs
      active - whether the product is active
      file - the new image file for the product
      productJson - the updated product details in JSON format
      Returns:
      a response entity containing a success or error message
      Throws:
      IOException - if an error occurs while processing the image file
    • deleteProductById

      @DeleteMapping("/delete/{id}") public org.springframework.http.ResponseEntity<?> deleteProductById(@PathVariable("id") Long id)
      Delete a product by its ID.

      Accepts a product ID as a path variable and deletes the corresponding product. Returns a success message if deletion is successful, or an error message with the appropriate HTTP status if the product is not found.

      Parameters:
      id - the ID of the product to delete
      Returns:
      a response entity containing a success or error message
    • getProductById

      @GetMapping("/get-by-id") public org.springframework.http.ResponseEntity<?> getProductById(@RequestParam("id") String id)
      Retrieve a product by its ID.

      Accepts a product ID as a request parameter and returns the corresponding product details. Returns an error message with the appropriate HTTP status if validation fails or the product is not found.

      Parameters:
      id - the ID of the product to retrieve
      Returns:
      a response entity containing the product details or an error message