diff --git a/resources/Products/ProductController.js b/resources/Products/ProductController.js index 887ed0c..02799f3 100644 --- a/resources/Products/ProductController.js +++ b/resources/Products/ProductController.js @@ -98,8 +98,109 @@ export const getOneProduct = async (req, res) => { }; // 3.update Product +// export const updateProduct = async (req, res) => { +// const { +// name, +// description, +// price, +// category, +// image, +// gst_amount, +// gst, +// total_amount, +// } = req.body; +// console.log(gst_amount, gst, total_amount); + +// try { +// // Prepare an array for the images +// const jsonArray = JSON.parse(image); +// const AllImages = jsonArray.map(({ public_id, url }) => ({ +// public_id, +// url, +// })); + +// if (req.files && req.files.newImages) { +// const newuploadImages = Array.isArray(req.files.newImages) +// ? req.files.newImages +// : [req.files.newImages]; + +// const imagesLinks = []; + +// for (let i = 0; i < newuploadImages.length; i++) { +// const result = await cloudinary.v2.uploader.upload( +// newuploadImages[i].tempFilePath, +// { +// folder: "smellica/product", +// } +// ); + +// imagesLinks.push({ +// public_id: result.public_id, +// url: result.secure_url, +// }); +// } + +// // Combine the existing images and the newly uploaded images +// const updatedImages = [...AllImages, ...imagesLinks]; + +// // Perform the product update +// const ModifyProduct = await Product.findOneAndUpdate( +// { _id: req.params.id }, +// { +// $set: { +// name, +// description, +// price, +// category, +// image: updatedImages, +// gst, +// gst_amount, +// total_amount, +// }, +// }, +// { new: true } +// ); +// return res.status(200).json({ +// success: true, +// ModifyProduct, +// }); +// } else { +// const ModifyProduct = await Product.findOneAndUpdate( +// { _id: req.params.id }, +// { +// $set: { +// name, +// description, +// price, +// category, +// image: AllImages, +// }, +// }, +// { new: true } +// ); +// return res.status(200).json({ +// success: true, +// ModifyProduct, +// }); +// } +// } catch (error) { +// res.status(500).json({ +// success: false, +// msg: error.message ? error.message : "Something went wrong!", +// }); +// } +// }; export const updateProduct = async (req, res) => { - const { name, description, price, category, image } = req.body; + const { + name, + description, + price, + category, + image, + gst_amount, + gst, + total_amount, + } = req.body; try { // Prepare an array for the images @@ -109,16 +210,18 @@ export const updateProduct = async (req, res) => { url, })); + let updatedImages = AllImages; + if (req.files && req.files.newImages) { - const newuploadImages = Array.isArray(req.files.newImages) + const newUploadImages = Array.isArray(req.files.newImages) ? req.files.newImages : [req.files.newImages]; const imagesLinks = []; - for (let i = 0; i < newuploadImages.length; i++) { + for (let i = 0; i < newUploadImages.length; i++) { const result = await cloudinary.v2.uploader.upload( - newuploadImages[i].tempFilePath, + newUploadImages[i].tempFilePath, { folder: "smellica/product", } @@ -131,46 +234,37 @@ export const updateProduct = async (req, res) => { } // Combine the existing images and the newly uploaded images - const updatedImages = [...AllImages, ...imagesLinks]; - - // Perform the product update - const ModifyProduct = await Product.findOneAndUpdate( - { _id: req.params.id }, - { - $set: { - name, - description, - price, - category, - image: updatedImages, - }, - }, - { new: true } - ); - return res.status(200).json({ - success: true, - ModifyProduct, - }); - } else { - const ModifyProduct = await Product.findOneAndUpdate( - { _id: req.params.id }, - { - $set: { - name, - description, - price, - category, - image: AllImages, - }, - }, - { new: true } - ); - return res.status(200).json({ - success: true, - ModifyProduct, - }); + updatedImages = [...AllImages, ...imagesLinks]; } + + // Perform the product update + const updatedProduct = await Product.findOneAndUpdate( + { _id: req.params.id }, + { + $set: { + name, + description, + price, + category, + image: updatedImages, + gst, + gst_amount, + total_amount, + }, + }, + { new: true } + ); + + if (!updatedProduct) { + return res.status(404).json({ success: false, msg: "Product not found" }); + } + + return res.status(200).json({ + success: true, + updatedProduct, + }); } catch (error) { + console.error("Error updating product:", error); res.status(500).json({ success: false, msg: error.message ? error.message : "Something went wrong!", diff --git a/resources/Products/ProductModel.js b/resources/Products/ProductModel.js index 24469a4..4780c94 100644 --- a/resources/Products/ProductModel.js +++ b/resources/Products/ProductModel.js @@ -5,7 +5,7 @@ const productSchema = new Schema( { name: { type: String, - maxLength: [25, "name cannot exceed 25 characters"], + maxLength: [35, "name cannot exceed 25 characters"], required: [true, "Please Enter product Name"], trim: true, }, @@ -15,7 +15,7 @@ const productSchema = new Schema( }, description: { type: String, - maxLength: [100, "description cannot exceed 100 characters"], + maxLength: [400, "description cannot exceed 100 characters"], required: [true, "Please Enter product Description"], }, price: {