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: develope a product insert api, accetp alll theses values from user, { "name": "string", "description": "string", "price": "number", "sizes": ["S", "M", "L"], "colors": ["red", "blue"], "images": ["url1", "url2"], "stock": "number", "rating": "number" } api functions name createProduct response with created product details and total product count
Generate source code based on the description above and respond in the following JSON format ONLY: { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code" } } IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only.Assistant:
- Assistant
{ "name": "createProduct", "schema": { "type": "string", "code": "const express = require('express');\nconst Product = require('./path/to/productModel'); // Update with the correct path\nconst router = express.Router();\n\nrouter.post('/products', async (req, res) => {\n try {\n const { name, description, price, sizes, colors, images, stock, rating } = req.body;\n\n const newProduct = new Product({\n name,\n description,\n price,\n sizes,\n colors,\n images,\n stock,\n rating\n });\n\n const savedProduct = await newProduct.save();\n const totalProducts = await Product.countDocuments();\n\n res.status(201).json({ product: savedProduct, totalProducts });\n } catch (error) {\n res.status(400).json({ error: error.message });\n }\n});\n\nmodule.exports = router;" } }