1. Use Case: add to cart api Code: const Cart = require('../models/Cart'); // Add…
- User
User: 1. Use Case: add to cart api Code: const Cart = require('../models/Cart');
// Add to Cart Endpoint const addToCart = async (req, res) => { const { productId, quantity } = req.body;
if (!productId || quantity <= 0) { return res.status(400).json({ message: 'Invalid product ID or quantity.' }); } try { // Create a new cart item const cartItem = new Cart({ productId, quantity }); // Save the cart item to the database await cartItem.save(); // Respond with success message return res.status(201).json({ message: 'Item added to cart.' }); } catch (error) { return res.status(500).json({ message: 'Server error, please try again.' }); }} User: 2. Use Case: api Code: const viewCarts = async (req, res) => { try { const cartItems = await Cart.find(); // Fetch cart items from the database const response = cartItems.map(item => ({ productId: item.productId, name: item.name, price: item.price, quantity: item.quantity, totalPrice: item.price * item.quantity, })); res.json(response); } catch (error) { res.status(500).json({ message: 'Error retrieving cart items' }); } }
module.exports = { addToCart, viewCarts }; User: Description: Define cart routes for POST, GET, PUT, PATCH, and DELETE methods., Implement validation for incoming request data., Handle request and response objects appropriately., Connect to a user management service or database., Implement error handling for each operation. read below refrence create routes only for given apis
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:
- Assistant
{ "name": "cartRoutes", "schema": { "type": "string", "code": "const express = require('express');\nconst { addToCart, viewCarts } = require('../controllers/cartController');\nconst router = express.Router();\n\n// Validation middleware\nconst validateCartItem = (req, res, next) => {\n const { productId, quantity } = req.body;\n if (!productId || quantity <= 0) {\n return res.status(400).json({ message: 'Invalid product ID or quantity.' });\n }\n next();\n};\n\n// POST: Add item to cart\nrouter.post('/', validateCartItem, addToCart);\n\n// GET: View cart items\nrouter.get('/', viewCarts);\n\n// PUT: Update item in cart (additional validation needed)\nrouter.put('/:id', validateCartItem, async (req, res) => {\n try {\n const { quantity } = req.body;\n const cartItem = await Cart.findById(req.params.id);\n if (!cartItem) {\n return res.status(404).json({ message: 'Cart item not found.' });\n }\n cartItem.quantity = quantity;\n await cartItem.save();\n res.json({ message: 'Cart item updated.' });\n } catch (error) {\n res.status(500).json({ message: 'Error updating cart item.' });\n }\n});\n\n// PATCH: Modify item quantity in cart (additional validation needed)\nrouter.patch('/:id', validateCartItem, async (req, res) => {\n try {\n const cartItem = await Cart.findById(req.params.id);\n if (!cartItem) {\n return res.status(404).json({ message: 'Cart item not found.' });\n }\n cartItem.quantity += req.body.quantity;\n await cartItem.save();\n res.json({ message: 'Cart item quantity updated.' });\n } catch (error) {\n res.status(500).json({ message: 'Error updating cart item quantity.' });\n }\n});\n\n// DELETE: Remove item from cart\nrouter.delete('/:id', async (req, res) => {\n try {\n await Cart.findByIdAndDelete(req.params.id);\n res.json({ message: 'Cart item removed.' });\n } catch (error) {\n res.status(500).json({ message: 'Error removing cart item.' });\n }\n});\n\nmodule.exports = router;" } }