gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Untitled conversation
- User
{description:Define the Product schema with required fields (id, name, price, category, etc.), Extract data from req.body including productId and all necessary fields, Validate input data to ensure all required fields are present and correctly formatted, Query the database to find the existing product by productId, Update the product information with the new data received from user, Handle errors appropriately (e.g. product not found, validation errors), Send a response back to the user containing the updated product data }
Analyse above object generate source code for description and send response as only a json_object {"name": "prompt_name", "schema": { "type": "string", "code":"genarted code" "require":"extracted external modules or libraries like const bcrypt = require('bcrypt');" } }
- Assistant
{ "name": "Product Update Handler", "schema": { "type": "string", "code": "const express = require('express');\nconst mongoose = require('mongoose');\n\nconst app = express();\napp.use(express.json());\n\nconst productSchema = new mongoose.Schema({\n id: { type: String, required: true },\n name: { type: String, required: true },\n price: { type: Number, required: true },\n category: { type: String, required: true }\n});\n\nconst Product = mongoose.model('Product', productSchema);\n\napp.put('/update-product', async (req, res) => {\n const { productId, name, price, category } = req.body;\n\n // Validate input data\n if (!productId || !name || price === undefined || !category) {\n return res.status(400).json({ error: 'All fields are required.' });\n }\n\n // Query the database to find the existing product by productId\n const product = await Product.findOne({ id: productId });\n if (!product) {\n return res.status(404).json({ error: 'Product not found.' });\n }\n\n // Update the product information with new data received from user\n product.name = name;\n product.price = price;\n product.category = category;\n await product.save();\n\n // Send a response back to the user containing the updated product data\n return res.status(200).json(product);\n});\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n console.log(`Server running on port ${PORT}`);\n});\n", "require": "const express = require('express');\nconst mongoose = require('mongoose');" } }