Class ProductController
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<?>createProduct(String categoryName, boolean active, String productJson, org.springframework.web.multipart.MultipartFile file) Create a new product.org.springframework.http.ResponseEntity<?>Delete a product by its ID.org.springframework.http.ResponseEntity<?>getProductById(String id) Retrieve a product by its ID.org.springframework.http.ResponseEntity<?>updateProductById(String id, String categoryName, boolean active, org.springframework.web.multipart.MultipartFile file, String productJson) Update an existing product by its ID.
-
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 belongsactive- whether the product is activeproductJson- the product details in JSON formatfile- 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 updatecategoryName- the name of the category to which the product belongsactive- whether the product is activefile- the new image file for the productproductJson- 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
-