Skip to content

Commit

Permalink
Merge pull request #790 from haseebzaki-07/new_branch_6
Browse files Browse the repository at this point in the history
Add rent-products APIs
  • Loading branch information
manikumarreddyu authored Nov 5, 2024
2 parents 1fa3f6c + b6c0019 commit 4af6c78
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
60 changes: 60 additions & 0 deletions backend/controllers/rent/RentProductController.js
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 });
}
};
2 changes: 2 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const contactRoutes = require('./routes/Contactroute');
const shopRoutes = require('./routes/shop')
const googleauth = require('./routes/googleauth')
const agriProductRoutes = require('./routes/agriProductRoutes');
const rentProductRoutes = require('./routes/rent/rentProductRoutes');
const { sendEmail } = require('./services/emailService');
const session = require('express-session');
const passport = require('passport');
Expand Down Expand Up @@ -47,6 +48,7 @@ app.use('/auth', authRoutes);
app.use('/auth', googleauth);
app.use('/api', contactRoutes);
app.use('/api', shopRoutes);
app.use('/api', rentProductRoutes);
app.use('/api', userRoutes);
app.use('/api/products', agriProductRoutes);

Expand Down
35 changes: 35 additions & 0 deletions backend/model/rent/rentProduct.js
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);
22 changes: 22 additions & 0 deletions backend/routes/rent/rentProductRoutes.js
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;

0 comments on commit 4af6c78

Please sign in to comment.