Spaces:
Sleeping
Sleeping
| import Product from "../models/productModel.js"; | |
| // Create a new product | |
| const createProduct = async (req, res) => { | |
| try { | |
| // Add the user ID from the protect middleware to the product | |
| const product = await Product.create({ | |
| ...req.body, | |
| user: req.user._id, | |
| }); | |
| res.status(201).json(product); | |
| } catch (error) { | |
| res.status(500).json({ msg: error.message }); | |
| } | |
| }; | |
| // Get all products | |
| const getProducts = async (req, res) => { | |
| try { | |
| const products = await Product.find({}); | |
| res.status(200).json(products); | |
| } catch (error) { | |
| res.status(500).json({ msg: error.message }); | |
| } | |
| }; | |
| // Get a single product by ID | |
| const getProductById = async (req, res) => { | |
| try { | |
| const { id } = req.params; | |
| const product = await Product.findById(id); | |
| if (!product) { | |
| return res.status(404).json({ msg: "Product not found" }); | |
| } | |
| res.status(200).json(product); | |
| } catch (error) { | |
| res.status(500).json({ msg: error.message }); | |
| } | |
| }; | |
| // Update a product | |
| const updateProduct = async (req, res) => { | |
| try { | |
| const { id } = req.params; | |
| const product = await Product.findByIdAndUpdate(id, req.body, { | |
| new: true, // Return the modified document | |
| runValidators: true, | |
| }); | |
| if (!product) { | |
| return res.status(404).json({ msg: "Product not found" }); | |
| } | |
| res.status(200).json(product); | |
| } catch (error) { | |
| res.status(500).json({ msg: error.message }); | |
| } | |
| }; | |
| // Delete a product | |
| const deleteProduct = async (req, res) => { | |
| try { | |
| const { id } = req.params; | |
| const product = await Product.findByIdAndDelete(id); | |
| if (!product) { | |
| return res.status(404).json({ msg: "Product not found" }); | |
| } | |
| res.status(200).json({ msg: "Product deleted successfully" }); | |
| } catch (error) { | |
| res.status(500).json({ msg: error.message }); | |
| } | |
| }; | |
| export { | |
| createProduct, | |
| getProducts, | |
| getProductById, | |
| updateProduct, | |
| deleteProduct, | |
| }; | |