1. Use Case: Product schema Code: const mongoose = require('mongoose'); const…
- User
User: 1. Use Case: Product schema Code: const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({ name: { type: String, required: true, }, description: { type: String, required: true, }, price: { type: Number, required: true, min: 0, }, sizes: { type: [String], enum: ['S', 'M', 'L'], default: ['M'], }, colors: { type: [String], enum: ['red', 'blue'], }, images: { type: [String], default: [], }, stock: { type: Number, required: true, min: 0, }, rating: { type: Number, min: 0, max: 5, default: 0, } });
const Product = mongoose.model('Product', productSchema);
module.exports = Product; User: Description: Create a function api for delete product., accept product id as param, return deleted count, recive a flag from user for bulk delete. scenario is user can delete product bulky or single., if it bulk delete flag is true system should check you have given array of id. in single case id string is needed, bulk id can recive from body,
user Product schema to fetch products
Generate source code based on the description above and respond in the following JSON format ONLY: ```json { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code here" } } ``` IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only.Assistant:
- Assistant
{ "name": "deleteProductApi", "schema": { "type": "string", "code": "const express = require('express');\nconst Product = require('./path_to_product_model');\n\nconst router = express.Router();\n\nrouter.delete('/products', async (req, res) => {\n const { ids, bulk } = req.body;\n try {\n let deletedCount;\n if (bulk) {\n if (!Array.isArray(ids) || ids.length === 0) {\n return res.status(400).json({ message: 'Please provide an array of IDs for bulk delete.' });\n }\n const result = await Product.deleteMany({ _id: { $in: ids } });\n deletedCount = result.deletedCount;\n } else {\n if (!ids || typeof ids !== 'string') {\n return res.status(400).json({ message: 'Please provide a valid product ID for single delete.' });\n }\n const result = await Product.deleteOne({ _id: ids });\n deletedCount = result.deletedCount;\n }\n return res.status(200).json({ deletedCount });\n } catch (error) {\n return res.status(500).json({ message: 'Error deleting product.', error: error.message });\n }\n});\n\nmodule.exports = router;" } }