product contorller apis updated
This commit is contained in:
parent
d1b7e2f5d6
commit
5d6093ca6e
@ -98,8 +98,109 @@ export const getOneProduct = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 3.update Product
|
// 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) => {
|
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 {
|
try {
|
||||||
// Prepare an array for the images
|
// Prepare an array for the images
|
||||||
@ -109,16 +210,18 @@ export const updateProduct = async (req, res) => {
|
|||||||
url,
|
url,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
let updatedImages = AllImages;
|
||||||
|
|
||||||
if (req.files && req.files.newImages) {
|
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
|
||||||
: [req.files.newImages];
|
: [req.files.newImages];
|
||||||
|
|
||||||
const imagesLinks = [];
|
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(
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
newuploadImages[i].tempFilePath,
|
newUploadImages[i].tempFilePath,
|
||||||
{
|
{
|
||||||
folder: "smellica/product",
|
folder: "smellica/product",
|
||||||
}
|
}
|
||||||
@ -131,10 +234,11 @@ export const updateProduct = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Combine the existing images and the newly uploaded images
|
// Combine the existing images and the newly uploaded images
|
||||||
const updatedImages = [...AllImages, ...imagesLinks];
|
updatedImages = [...AllImages, ...imagesLinks];
|
||||||
|
}
|
||||||
|
|
||||||
// Perform the product update
|
// Perform the product update
|
||||||
const ModifyProduct = await Product.findOneAndUpdate(
|
const updatedProduct = await Product.findOneAndUpdate(
|
||||||
{ _id: req.params.id },
|
{ _id: req.params.id },
|
||||||
{
|
{
|
||||||
$set: {
|
$set: {
|
||||||
@ -143,34 +247,24 @@ export const updateProduct = async (req, res) => {
|
|||||||
price,
|
price,
|
||||||
category,
|
category,
|
||||||
image: updatedImages,
|
image: updatedImages,
|
||||||
|
gst,
|
||||||
|
gst_amount,
|
||||||
|
total_amount,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
);
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
if (!updatedProduct) {
|
||||||
ModifyProduct,
|
return res.status(404).json({ success: false, msg: "Product not found" });
|
||||||
});
|
|
||||||
} 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,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
updatedProduct,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error("Error updating product:", error);
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
msg: error.message ? error.message : "Something went wrong!",
|
||||||
|
@ -5,7 +5,7 @@ const productSchema = new Schema(
|
|||||||
{
|
{
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
maxLength: [25, "name cannot exceed 25 characters"],
|
maxLength: [35, "name cannot exceed 25 characters"],
|
||||||
required: [true, "Please Enter product Name"],
|
required: [true, "Please Enter product Name"],
|
||||||
trim: true,
|
trim: true,
|
||||||
},
|
},
|
||||||
@ -15,7 +15,7 @@ const productSchema = new Schema(
|
|||||||
},
|
},
|
||||||
description: {
|
description: {
|
||||||
type: String,
|
type: String,
|
||||||
maxLength: [100, "description cannot exceed 100 characters"],
|
maxLength: [400, "description cannot exceed 100 characters"],
|
||||||
required: [true, "Please Enter product Description"],
|
required: [true, "Please Enter product Description"],
|
||||||
},
|
},
|
||||||
price: {
|
price: {
|
||||||
|
Loading…
Reference in New Issue
Block a user