gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
1 prodect create api Code : // Import necessary modules const mongoose =…
- User
User:
1 prodect create apiCode : // Import necessary modulesconst mongoose = require('mongoose'); const Product = require('../models/Product');
// Function to create a new product const createProduct = async (req, res) => { try {
// Validate provided fields const { name, description, stocks, countTotal, company_name } = req.body; if (!name || typeof name !== 'string') { return res.status(400).json({ error: 'Invalid name' }); } if (!description || typeof description !== 'string') { return res.status(400).json({ error: 'Invalid description' }); } if (typeof stocks !== 'number' || stocks < 0) { return res.status(400).json({ error: 'Invalid stocks' }); } if (typeof countTotal !== 'number' || countTotal < 0) { return res.status(400).json({ error: 'Invalid total count' }); } if (!company_name || !mongoose.Types.ObjectId.isValid(company_name)) { return res.status(400).json({ error: 'Invalid company ID' }); } // Create a new product object const product = new Product({ name, description, stocks, countTotal, company_name }); // Insert product data into the database const savedProduct = await product.save(); // Send a JSON response with inserted product data res.status(201).json(savedProduct); } catch (error) { res.status(500).json({ error: 'Server error' }); }}; User:
2 product update apiCode : const updateProduct = async (req, res) => { const { productId, name, description, stocks, availability, countTotal, company_name } = req.body; // Validate input data if (!productId || !name || !description || stocks === undefined || countTotal === undefined || !company_name) { return res.status(400).json({ message: 'All fields are required.' }); } try { // Query the database to find the existing product const product = await Product.findById(productId); if (!product) { return res.status(404).json({ message: 'Product not found.' }); } // Update the product information product.name = name; product.description = description; product.stocks = stocks; product.availability = availability; product.countTotal = countTotal; product.company_name = company_name; // Save the updated product const updatedProduct = await product.save(); // Send a response back to the user res.status(200).json(updatedProduct); } catch (error) { // Handle errors appropriately console.error(error); res.status(500).json({ message: 'Server error.' }); }};
module.exports = { createProduct, updateProduct }; User:
- Assistant
{ "name": "ProductRoutes", "schema": { "type": "string", "code": "// Import necessary modules\nconst express = require('express');\nconst { createProduct, updateProduct } = require('../controllers/productController');\n\n// Create a router instance\nconst router = express.Router();\n\n// POST route to create a new product\nrouter.post('/', createProduct);\n\n// GET route to retrieve all products\nrouter.get('/', async (req, res) => {\n try {\n const products = await Product.find();\n res.status(200).json(products);\n } catch (error) {\n res.status(500).json({ message: 'Server error.' });\n }\n});\n\n// PUT route to update a specific product\nrouter.put('/:id', updateProduct);\n\n// PATCH route for partial updates to a specific product\nrouter.patch('/:id', async (req, res) => {\n const { productId } = req.params;\n const updates = req.body;\n\n try {\n const product = await Product.findById(productId);\n if (!product) {\n return res.status(404).json({ message: 'Product not found.' });\n }\n\n Object.keys(updates).forEach(key => {\n product[key] = updates[key];\n });\n\n const updatedProduct = await product.save();\n res.status(200).json(updatedProduct);\n } catch (error) {\n res.status(500).json({ message: 'Server error.' });\n }\n});\n\n// DELETE route to remove a specific product\nrouter.delete('/:id', async (req, res) => {\n const { id } = req.params;\n\n try {\n await Product.findByIdAndDelete(id);\n res.status(204).send();\n } catch (error) {\n res.status(500).json({ message: 'Server error.' });\n }\n});\n\n// Export the router\nmodule.exports = router;", "require": "const express = require('express');" } }