gpt-4o-mini-2024-07-18· openai
WildChat-4.8M
Description: Create a api to create app order under A user function name readOrders take…
- User
Description: Create a api to create app order under A user function name readOrders take user id from req.user user Order schma to take orders calculate total order count and total orderprice
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
{ "name": "createOrderAPI", "schema": { "type": "string", "code": "const express = require('express');\nconst mongoose = require('mongoose');\nconst router = express.Router();\n\n// Order schema\nconst orderSchema = new mongoose.Schema({\n userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },\n items: [{\n productId: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },\n quantity: Number,\n price: Number\n }],\n totalPrice: Number\n});\nconst Order = mongoose.model('Order', orderSchema);\n\n// Route to read orders\nrouter.get('/readOrders', async (req, res) => {\n try {\n const userId = req.user.id;\n const orders = await Order.find({ userId });\n const totalOrderCount = orders.length;\n const totalOrderPrice = orders.reduce((acc, order) => acc + order.totalPrice, 0);\n \n res.status(200).json({ totalOrderCount, totalOrderPrice, orders });\n } catch (error) {\n res.status(500).json({ message: 'Server error', error: error.message });\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.