You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.4 KiB
JavaScript

// Dependencies and Modules
const Product = require ("../models/Product")
const User = require("../models/User");
const bcrypt = require("bcrypt");
// Auth
const auth = require("../auth");
module.exports.createProduct = (req,res) => {
let newProduct = new Product({
name:req.body.name,
description:req.body.description,
price:req.body.price,
createdOn:req.body.createdOn
});
return newProduct.save()
.then(course => {
return res.send(true);
})
.catch(error => {
console.error(error); // Log the error for debugging purposes
return res.send(false);
});
}
module.exports.getAllProducts = (req,res) =>{
// to retrieve all the documents in the "product" collection, we will use find({}) method.
return Product.find({}).then(result =>{
return res.send(result);
});
}
module.exports.getactiveproduct = (req,res) =>{
return Product.find({isActive:true}).then(result =>{
return res.send(result);
});
}
module.exports.getProduct = (req,res) =>{
return Product.findById(req.params.productId).then(result =>{
return res.send(result);
});
}
module.exports.updateProduct = (req, res) => {
// Specify the fields/properties of the document to be updated
let updateProduct = {
name: req.body.name,
description: req.body.description,
price: req.body.price
}
// Syntax
// findByIdAndUpdate(documentId, updatesToBeApplied)
return Product.findByIdAndUpdate(req.params.productId, updateProduct).then((product, error) => {
// If product was not updated
if(error){
return res.send(false);
// If product was updated successfully
} else {
return res.send(true);
}
});
}
module.exports.archiveProduct = async (req, res) => {
const productId = req.params.productId; // Assuming you're passing the productId in the URL
try {
const product = await Product.findById(productId);
if (!product) {
return res.status(404).send('Product not found');
}
product.isArchived = true;
await product.save();
return res.send('Product archived successfully');
} catch (error) {
return res.status(500).send(error.message);
}
};
module.exports.activateProduct = async (req, res) => {
const productId = req.params.productId; // Assuming you're passing the productId in the URL
try {
const product = await Product.findById(productId);
if (!product) {
return res.status(404).send('Product not found');
}
product.isActive = true;
await product.save();
return res.send('Product activated successfully');
} catch (error) {
return res.status(500).send(error.message);
}
};
module.exports.searchProductsByName = async (req, res) => {
try {
const { productName } = req.body;
// Use a regular expression to perform a case-insensitive search
const courses = await Course.find({
name: { $regex: productName, $options: 'i' }
});
res.json(products);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
};
exports.searchProductsByPriceRange = async (req, res) => {
try {
const { minPrice, maxPrice } = req.body;
// Find courses within the price range
const products = await Product.find({
price: { $gte: minPrice, $lte: maxPrice }
});
res.status(200).json({ products });
} catch (error) {
res.status(500).json({ error: 'An error occurred while searching for courses' });
}
};
// exports.updateProduct = async (req, res) => {
// try {
// // Check if the user is an admin
// if (!req.user.isAdmin) {
// return res.status(403).json({ message: 'You are not authorized to perform this action' });
// }
// const updateProduct = await Product.findByIdAndUpdate(req.params.id, req.body, { new: true });
// res.json(updatedProduct);
// } catch (error) {
// res.status(500).json({ message: error.message });
// }
// };
// exports.deleteProduct = async (req, res) => {
// try {
// // Check if the user is an admin
// if (!req.user.isAdmin) {
// return res.status(403).json({ message: 'You are not authorized to perform this action' });
// }
// await Product.findByIdAndDelete(req.params.id);
// res.json({ message: 'Product deleted successfully' });
// } catch (error) {
// res.status(500).json({ message: error.message });
// }
// };