gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Description: Create api to delete products from wjishlist, accept userId and producnt id…
- User
Description: Create api to delete products from wjishlist, accept userId and producnt id from user body below given wishlist shcema to save data, export functions name, handel all errors and use exeptions scenarios
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": "deleteProductFromWishlist", "schema": { "type": "string", "code": "const express = require('express');\nconst mongoose = require('mongoose');\n\nconst app = express();\napp.use(express.json());\n\nconst Wishlist = mongoose.model('Wishlist', new mongoose.Schema({\n userId: { type: String, required: true },\n products: [{ productId: String }]\n}));\n\n/**\n * Deletes a product from the user's wishlist.\n * @param {string} userId - The ID of the user.\n * @param {string} productId - The ID of the product to delete.\n */\nasync function deleteProductFromWishlist(userId, productId) {\n try {\n const wishlist = await Wishlist.findOne({ userId });\n if (!wishlist) {\n throw new Error('Wishlist not found for the given user.');\n }\n wishlist.products = wishlist.products.filter(product => product.productId !== productId);\n await wishlist.save();\n return { message: 'Product removed successfully from wishlist.' };\n } catch (error) {\n throw new Error(error.message);\n }\n}\n\napp.delete('/wishlist', async (req, res) => {\n const { userId, productId } = req.body;\n if (!userId || !productId) {\n return res.status(400).json({ error: 'userId and productId are required.' });\n }\n try {\n const response = await deleteProductFromWishlist(userId, productId);\n return res.status(200).json(response);\n } catch (error) {\n return res.status(500).json({ error: error.message });\n }\n});\n\napp.listen(3000, () => {\n console.log('Server is running on port 3000');\n});" } }