181 lines
4.7 KiB
JavaScript
181 lines
4.7 KiB
JavaScript
import ProductManual from "./ProductManualModel.js";
|
|
import cloudinary from "../../Utils/cloudinary.js";
|
|
|
|
// Create a new product manual
|
|
export const createProductManual = async (req, res) => {
|
|
const { title } = req.body;
|
|
|
|
try {
|
|
let productManualDetails;
|
|
|
|
// Check if a file is provided
|
|
if (req.files && req.files.product_manual) {
|
|
const pdfFile = req.files.product_manual;
|
|
const result = await cloudinary.v2.uploader.upload(pdfFile.tempFilePath, {
|
|
folder: "chemiNova/ProductManuals",
|
|
resource_type: "raw", // For PDF or other non-image files
|
|
});
|
|
|
|
productManualDetails = {
|
|
public_id: result.public_id,
|
|
url: result.secure_url,
|
|
};
|
|
}
|
|
|
|
// Create the product manual
|
|
const productManual = await ProductManual.create({
|
|
title,
|
|
product_manual: productManualDetails,
|
|
});
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
productManual,
|
|
message: "Product manual created successfully",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating product manual:", error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Internal server error",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Get all product manuals
|
|
export const getAllProductManuals = async (req, res) => {
|
|
try {
|
|
const productManuals = await ProductManual.find();
|
|
res.status(200).json({
|
|
success: true,
|
|
productManuals,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching product manuals:", error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Internal server error",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Get a single product manual by ID
|
|
export const getSingleProductManual = async (req, res) => {
|
|
const { id } = req.params;
|
|
|
|
try {
|
|
const productManual = await ProductManual.findById(id);
|
|
|
|
if (!productManual) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: "Product manual not found",
|
|
});
|
|
}
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
productManual,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching product manual:", error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Internal server error",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Update a product manual
|
|
export const updateProductManual = async (req, res) => {
|
|
const { id } = req.params;
|
|
const { title } = req.body;
|
|
|
|
try {
|
|
const productManual = await ProductManual.findById(id);
|
|
|
|
if (!productManual) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: "Product manual not found",
|
|
});
|
|
}
|
|
|
|
// Check if a new file is provided
|
|
if (req.files && req.files.product_manual) {
|
|
// Delete the old file from Cloudinary
|
|
if (productManual.product_manual.public_id) {
|
|
await cloudinary.v2.uploader.destroy(productManual.product_manual.public_id, {
|
|
resource_type: "raw",
|
|
});
|
|
}
|
|
|
|
// Upload the new file to Cloudinary
|
|
const pdfFile = req.files.product_manual;
|
|
const result = await cloudinary.v2.uploader.upload(pdfFile.tempFilePath, {
|
|
folder: "chemiNova/ProductManuals",
|
|
resource_type: "raw",
|
|
});
|
|
|
|
// Update the product manual details
|
|
productManual.product_manual = {
|
|
public_id: result.public_id,
|
|
url: result.secure_url,
|
|
};
|
|
}
|
|
|
|
// Update the title
|
|
productManual.title = title || productManual.title;
|
|
|
|
await productManual.save();
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
productManual,
|
|
message: "Product manual updated successfully",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error updating product manual:", error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Internal server error",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Delete a product manual
|
|
export const deleteProductManual = async (req, res) => {
|
|
const { id } = req.params;
|
|
|
|
try {
|
|
const productManual = await ProductManual.findById(id);
|
|
|
|
if (!productManual) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: "Product manual not found",
|
|
});
|
|
}
|
|
|
|
// Delete the file from Cloudinary
|
|
if (productManual.product_manual.public_id) {
|
|
await cloudinary.v2.uploader.destroy(productManual.product_manual.public_id, {
|
|
resource_type: "raw",
|
|
});
|
|
}
|
|
|
|
// Delete the product manual from the database
|
|
await ProductManual.findByIdAndDelete(id);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
message: "Product manual deleted successfully",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error deleting product manual:", error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Internal server error",
|
|
});
|
|
}
|
|
}; |