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 insert product in to Product model
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_product_model'); // adjust the path accordingly\n\nconst router = express.Router();\n\nrouter.post('/createProduct', async (req, res) => {\n const { name, description, price, sizes, colors, images, stock, rating } = req.body;\n \n try {\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 \n const totalProducts = await Product.countDocuments();\n \n return res.status(201).json({ product: savedProduct, totalProducts });\n } catch (error) {\n return res.status(500).json({ error: 'Error creating product', details: error });\n }\n});\n\nmodule.exports = router;" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.