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.

142 lines
3.7 KiB
JavaScript

11 months ago
const Product = require("../model/Product")
exports.createProduct = async (req, res) => {
11 months ago
try {
const { name, description, price } = req.body
const newProduct = new Product({ name, description, price })
await newProduct.save()
res.status(201).json(newProduct)
} catch (error) {
res.status(500).json({ error: error.message })
}
}
// Controller function for retrieving all products (accessible to both admin and normal user)
exports.getAllProducts = async (req, res) => {
try {
const products = await Product.find()
res.status(200).json(products)
} catch (error) {
console.error(error)
res.status(500).json({ message: "Internal server error" })
}
}
// Controller function for retrieving all active products (accessible to both admin and normal user)
exports.getActiveProducts = async (req, res) => {
try {
const activeProducts = await Product.find({ isActive: true })
res.status(200).json(activeProducts)
} catch (error) {
console.error(error)
res.status(500).json({ message: "Internal server error" })
}
}
// Controller to retrieve a single product by id
exports.getProductById = async (req, res) => {
const productId = req.params.id;
try {
11 months ago
const product = await Product.findById(productId);
if (!product) {
return res.status(404).json({ message: 'Product not found' });
}
11 months ago
res.status(200).json(product);
} catch (error) {
res.status(500).json({ message: 'Internal server error' });
}
};
// Controller function to update product information
exports.updateProduct = async (req, res) => {
const productId = req.params.id;
const { name, description, price, isActive } = req.body;
try {
// Check if the product exists
const existingProduct = await Product.findById(productId);
if (!existingProduct) {
return res.status(404).json({ message: 'Product not found' });
}
11 months ago
// Update product information
existingProduct.name = name;
existingProduct.description = description;
existingProduct.price = price;
existingProduct.isActive = isActive;
11 months ago
// Save the updated product
const updatedProduct = await existingProduct.save();
11 months ago
res.status(200).json(updatedProduct);
} catch (error) {
console.error(error);
11 months ago
res.status(500).json({ message: 'Internal Server Error' });
}
};
11 months ago
exports.activateProduct = async (req, res) => {
const { productId } = req.params;
try {
11 months ago
// Find the product by ID
const product = await Product.findById(productId);
if (!product) {
return res.status(404).json({ message: 'Product not found' });
}
11 months ago
// Check if the product is already active
if (product.isActive) {
return res.status(400).json({ message: 'Product is already active' });
}
// Activate the product
product.isActive = true;
// Save the updated product
await product.save();
res.json({ message: 'Product activated successfully', product });
} catch (error) {
console.error(error);
11 months ago
res.status(500).json({ message: 'Internal Server Error' });
}
};
11 months ago
exports.archiveProduct = async (req, res) => {
const { productId } = req.params;
try {
11 months ago
// Find the product by ID
const product = await Product.findById(productId);
if (!product) {
return res.status(404).json({ message: 'Product not found' });
}
11 months ago
// Check if the product is already active
if (!product.isActive) {
return res.status(400).json({ message: 'Product is already in Archive' });
}
// Activate the product
product.isActive = false;
// Save the updated product
await product.save();
res.json({ message: 'Product successfuly archived', product });
} catch (error) {
console.error(error);
11 months ago
res.status(500).json({ message: 'Internal Server Error' });
}
};