Task: "Create delete Api for Product" Description: "Generate a Node.js product…
- User
User: Task: "Create delete Api for Product" Description: "Generate a Node.js product delete Api with given dependenceis" Requirements: - Database: mongoose - Authentication: None specified - Output needed: Return only the function code as specified in dependencies - Dependencies: Develop Product deletion api function as exported function it's self. function mode :- exports.functionname = async () =>{...} , Accept two parmas request and response, Api should Accept product id as parmas as proId , Data should delete from Product schema by using product id. No need to import schema, While updating product should manage all errors, Api should build inder try catch method for error handeling., Response should send back in Json formate with 'status' code of 200 and deleted product - Previous Code const Product = require('../models/Product.js');
exports.getProductListing = async (req, res) => { try { // Fetch all products from the database using the Product schema const products = await Product.find();
// Check if no products were found if (!products.length) { return res.status(200).json({ status: 200, products: [] }); } // Return the products with a status of 200 return res.status(200).json({ status: 200, products }); } catch (error) { // Handle errors and send a server error status return res.status(500).json({ status: 500, error: 'Server Error' }); }};
exports.insertProduct = async (req, res) => { try { // Destructuring product details from the request body const { name, description, expiryDate, price, totalItems, insertionDate } = req.body;
// Validate product details if (!name || !description || !expiryDate || !price || !totalItems) { return res.status(400).json({ status: 400, error: 'All product details must be provided' }); } // Initialize a new product instance (assuming Product is a mongoose model exposed via schema) const product = new Product({ name, description, expiryDate, price, totalItems, insertionDate: insertionDate || new Date() // Optional, defaults to current date if not provided }); // Save the product to the database const savedProduct = await product.save(); // Send back a successful response with the saved product return res.status(200).json({ status: 200, product: savedProduct }); } catch (error) { // Handle any errors that may occur return res.status(500).json({ status: 500, error: 'Error inserting product' }); }};
exports.updateProduct = async (req, res) => { const proId = req.params.proId; const { name, description, expiryDate, price, totalItems } = req.body;
try { // Check if no update values are provided if (!name && !description && !expiryDate && !price && !totalItems) { return res.status(400).json({ status: 400, error: 'No update fields provided' }); } // Constructing the update object dynamically based on provided fields const updateData = {}; if (name) updateData.name = name; if (description) updateData.description = description; if (expiryDate) updateData.expiryDate = expiryDate; if (price) updateData.price = price; if (totalItems) updateData.totalItems = totalItems; // Update the product using the Product schema and the product ID const updatedProduct = await Product.findByIdAndUpdate(proId, updateData, { new: true }); // Check if the product was not found if (!updatedProduct) { return res.status(404).json({ status: 404, error: 'Product not found' }); } // Send back a successful response with the updated product return res.status(200).json({ status: 200, product: updatedProduct }); } catch (error) { // Handle any errors that may occur return res.status(500).json({ status: 500, error: 'Error updating product' }); } - Assistant
exports.deleteProduct = async (req, res) => { const proId = req.params.proId; try { // Attempt to delete the product with the given ID using mongoose model const deletedProduct = await Product.findByIdAndDelete(proId); // If no product is found, send a 404 not found response if (!deletedProduct) { return res.status(404).json({ status: 404, error: 'Product not found' }); } // If the product is found and deleted successfully, return a 200 response with the deleted product return res.status(200).json({ status: 200, product: deletedProduct }); } catch (error) { // Handle any errors that might occur during the process return res.status(500).json({ status: 500, error: 'Error deleting product' }); } };