-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #790 from haseebzaki-07/new_branch_6
Add rent-products APIs
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// controllers/productController.js | ||
|
||
const Product = require('../../model/rent/rentProduct'); | ||
|
||
// Create a new product | ||
exports.createProduct = async (req, res) => { | ||
try { | ||
const newProduct = new Product(req.body); | ||
const savedProduct = await newProduct.save(); | ||
res.status(201).json(savedProduct); | ||
} catch (error) { | ||
res.status(400).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Get all products | ||
exports.getAllProducts = async (req, res) => { | ||
try { | ||
const products = await Product.find(); | ||
res.json(products); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Get a single product by ID | ||
exports.getProductById = async (req, res) => { | ||
try { | ||
const product = await Product.findById(req.params.id); | ||
if (!product) return res.status(404).json({ message: 'Product not found' }); | ||
res.json(product); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Update a product by ID | ||
exports.updateProduct = async (req, res) => { | ||
try { | ||
const updatedProduct = await Product.findByIdAndUpdate(req.params.id, req.body, { | ||
new: true, | ||
runValidators: true, | ||
}); | ||
if (!updatedProduct) return res.status(404).json({ message: 'Product not found' }); | ||
res.json(updatedProduct); | ||
} catch (error) { | ||
res.status(400).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Delete a product by ID | ||
exports.deleteProduct = async (req, res) => { | ||
try { | ||
const deletedProduct = await Product.findByIdAndDelete(req.params.id); | ||
if (!deletedProduct) return res.status(404).json({ message: 'Product not found' }); | ||
res.json({ message: 'Product deleted successfully' }); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// models/Product.js | ||
|
||
const mongoose = require('mongoose'); | ||
|
||
const rentProductSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
description: { | ||
type: String, | ||
required: true, | ||
}, | ||
price: { | ||
type: Number, | ||
required: true, | ||
}, | ||
image: { | ||
type: String, | ||
required: true, | ||
}, | ||
rating: { | ||
type: Number, | ||
default: 0, | ||
min: 0, | ||
max: 5, | ||
}, | ||
category: { | ||
type: [String], | ||
required: true, | ||
} | ||
}, { timestamps: true }); | ||
|
||
// Avoid OverwriteModelError by checking if 'Product' model is already registered | ||
module.exports = mongoose.models.RentProduct || mongoose.model('RentProduct', rentProductSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// routes/productRoutes.js | ||
|
||
const express = require('express'); | ||
const router = express.Router(); | ||
const productController = require('../../controllers/rent/RentProductController'); | ||
|
||
// Create a new product | ||
router.post('/rent-products', productController.createProduct); | ||
|
||
// Get all products | ||
router.get('/rent-products', productController.getAllProducts); | ||
|
||
// Get a single product by ID | ||
router.get('/rent-products/:id', productController.getProductById); | ||
|
||
// Update a product by ID | ||
router.put('/rent-products/:id', productController.updateProduct); | ||
|
||
// Delete a product by ID | ||
router.delete('/rent-products/:id', productController.deleteProduct); | ||
|
||
module.exports = router; |