Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rent-products APIs #790

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading