From 56438a92aaf3493cce044d946df2117ea3ae7847 Mon Sep 17 00:00:00 2001 From: pawan-dot <71133473+pawan-dot@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:05:38 +0530 Subject: [PATCH] change all images fuctionality --- .env | 7 +- resources/Content/ContentController.js | 26 +- resources/LoginImage/LoginImageController.js | 318 ++++++++++-------- resources/LoginImage/LoginImageRoute.js | 8 + .../RegistrationImageController.js | 63 +++- .../RegistrationImageRoute.js | 8 + .../ShopPageImage/ShopPageImageController.js | 57 +++- resources/ShopPageImage/ShopPageImageRoute.js | 8 + 8 files changed, 336 insertions(+), 159 deletions(-) diff --git a/.env b/.env index 78be679..974e60c 100644 --- a/.env +++ b/.env @@ -1,8 +1,8 @@ -# DB_URL="mongodb+srv://smellica:Anjefef23dnsfjne@cluster0.c5gfqzm.mongodb.net/" + DB_URL="mongodb+srv://pulseayur:rEF9gYORzKavLeiy@cluster0.vi4tl1l.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" # pulseayur Data Connection -DB_URL="mongodb+srv://pulseayur:rEF9gYORzKavLeiy@cluster0.vi4tl1l.mongodb.net/" +# DB_URL="mongodb+srv://pulseayur:rEF9gYORzKavLeiy@cluster0.vi4tl1l.mongodb.net/" # DB_URL="mongodb://localhost:27017/smellica" PORT = 5000 @@ -37,4 +37,5 @@ SMPT_PASSWORD="ND5sgVnWtrpUFfTb" PAYPAL_CLIENT_ID="AemCjVuWswklp1sWUo4peCFg9eS4bofMsMR0RCrVRB2DifYR1IUSrWqtHpVmQlrVMKTI2cWZXLJAdYwn" PAYPAL_CLIENT_SECRET="EAo0Y9ff3jpHHg1QAbftdebfh7cb_-vnebhQrP9KALbCVer908yx2tO2eHO39r7EJSfqc4D69Qgx8R31" -STRIPE_SECRET="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql" \ No newline at end of file +STRIPE_SECRET="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql" + diff --git a/resources/Content/ContentController.js b/resources/Content/ContentController.js index 52c17fd..20fd5d8 100644 --- a/resources/Content/ContentController.js +++ b/resources/Content/ContentController.js @@ -2,7 +2,7 @@ import { PrivacyAndPolicy } from "./PrivacyPolicyModel.js"; import { Refundpolicy } from "./RefundModel.js"; import { Shipping } from "./ShippingModel.js"; import { TermsAndCondition } from "./TermsandConditonModel.js"; -import { AboutUs } from './AboutUsModel.js' +import { AboutUs } from "./AboutUsModel.js"; export const AddTermsAndConditions = async (req, res) => { try { @@ -54,15 +54,14 @@ export const updateTermsAndConditions = async (req, res) => { if (!req?.user) return res.status(400).json({ message: "please login !" }); // new content const { content } = req.body; - // id of the terms and conndition document const id = req.query.id; // object for updated terms and conndition data const updatedTermsData = { termsAndContionContent: content, - addedBy: req.user._id - } + addedBy: req.user._id, + }; // update the terms and conndition in database const termsAndCondition = await TermsAndCondition.findByIdAndUpdate( @@ -138,8 +137,8 @@ export const updateRefundPolicy = async (req, res) => { // object for updated refund policy data const updatedRefundPolicyData = { Refundpolicy: content, - addedBy: req.user._id - } + addedBy: req.user._id, + }; // update the refund policy in database const refundPolicy = await Refundpolicy.findByIdAndUpdate( @@ -161,7 +160,6 @@ export const updateRefundPolicy = async (req, res) => { } }; - // Privacy policy controller functions export const AddPrivacyAndPolicy = async (req, res) => { @@ -222,8 +220,8 @@ export const updatePrivacyPolicy = async (req, res) => { // object for updated privacy policy data const updatedPrivacyPolicyData = { privacyAndPolicyContent: content, - addedBy: req.user._id - } + addedBy: req.user._id, + }; // update the privacy policy in database const privacyAndPolicy = await PrivacyAndPolicy.findByIdAndUpdate( @@ -304,8 +302,8 @@ export const updateShipping = async (req, res) => { // object for updated shipping policy data const updatedShippingData = { shippingContent: content, - addedBy: req.user._id - } + addedBy: req.user._id, + }; // update the shipping policy in database const shipping = await Shipping.findByIdAndUpdate( @@ -387,8 +385,8 @@ export const updateAboutUs = async (req, res) => { // object for updated about us data const updatedAboutUsData = { aboutUsContent: content, - addedBy: req.user._id - } + addedBy: req.user._id, + }; // update the about us in database const aboutUs = await AboutUs.findByIdAndUpdate( @@ -409,5 +407,3 @@ export const updateAboutUs = async (req, res) => { }); } }; - - diff --git a/resources/LoginImage/LoginImageController.js b/resources/LoginImage/LoginImageController.js index 60169ef..491d4a2 100644 --- a/resources/LoginImage/LoginImageController.js +++ b/resources/LoginImage/LoginImageController.js @@ -5,60 +5,60 @@ import { LoginImageModel } from "./LoginImageModel.js"; // Add new Category export const addImage = async (req, res) => { - // const { bannerName } = req.body; - const { bannerImage } = req.files; - // console.log("image", bannerImage); - // console.log(categoryName, categoryImage); + // const { bannerName } = req.body; + const { bannerImage } = req.files; + // console.log("image", bannerImage); + // console.log(categoryName, categoryImage); - if (!req?.user) return res.status(400).json({ message: "please login !" }); - try { - if (!mongoose.Types.ObjectId.isValid(req.user._id)) { - return res.status(400).json({ message: "please login again " }); - } - const result = await cloudinary.v2.uploader.upload( - bannerImage.tempFilePath, - { - folder: "GetSygnal/loginImage", - } - ); - - if (result) { - const regsiterImage = await LoginImageModel.create({ - image: result, - addedBy: req.user._id, - }); - if (regsiterImage) { - return res - .status(201) - .json({ success: true, regsiterImage, message: "Image Added" }); - } - } - } catch (error) { - res.status(500).json({ - success: false, - message: error.message ? error.message : "Something went Wrong", - }); + if (!req?.user) return res.status(400).json({ message: "please login !" }); + try { + if (!mongoose.Types.ObjectId.isValid(req.user._id)) { + return res.status(400).json({ message: "please login again " }); } + const result = await cloudinary.v2.uploader.upload( + bannerImage.tempFilePath, + { + folder: "Ayurpulse/loginImage", + } + ); + + if (result) { + const regsiterImage = await LoginImageModel.create({ + image: result, + addedBy: req.user._id, + }); + if (regsiterImage) { + return res + .status(201) + .json({ success: true, regsiterImage, message: "Image Added" }); + } + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went Wrong", + }); + } }; export const getImage = async (req, res) => { - try { - // if (!req?.user) return res.status(400).json({ message: "please login !" }); - const image = await LoginImageModel.find().sort({ - createdAt: -1, - }); + try { + // if (!req?.user) return res.status(400).json({ message: "please login !" }); + const image = await LoginImageModel.find().sort({ + createdAt: -1, + }); - if (!image) { - return res.status(404).json({ message: "No categories found" }); - } - // console.log("image", image); - res.status(200).json({ success: true, image }); - } catch (error) { - res.status(500).json({ - success: false, - message: error.message ? error.message : "Something went wrong", - }); + if (!image) { + return res.status(404).json({ message: "No categories found" }); } + // console.log("image", image); + res.status(200).json({ success: true, image }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went wrong", + }); + } }; // export const updateImage = async (req, res) => { // try { @@ -84,7 +84,7 @@ export const getImage = async (req, res) => { // const result = await cloudinary.v2.uploader.upload( // bannerImag.tempFilePath, // { -// folder: "GetSygnal/loginImage", +// folder: "Ayurpulse/loginImage", // } // ); @@ -124,98 +124,144 @@ export const getImage = async (req, res) => { // }; export const updateImage = async (req, res) => { - try { - if (!req?.user) return res.status(400).json({ message: "please login !" }); - const { _id } = req.params; - const { bannerName } = req.body; - const olderImage = req.body?.olderImage; - const bannerImag = req.files?.bannerImage; - // console.log("bannerImag", bannerImag); + try { + if (!req?.user) return res.status(400).json({ message: "please login !" }); + const { _id } = req.params; + const olderImage = req.body?.olderImage; + const bannerImag = req.files?.bannerImage; - if (!mongoose.Types.ObjectId.isValid(_id)) { - return res.status(404).json({ error: "Can not find the document " }); - } - // console.log(JSON.parse(olderImage).length); - // find the document with the id to delete the image from cloudinary - - if (olderImage) { - - const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id }); - - // console.log("deletefromCloudinary", deletefromCloudinary) - const deleteresponse = await cloudinary.v2.uploader.destroy( - deletefromCloudinary.image.public_id - ); - - - if (deleteresponse) { - - const result = await cloudinary.v2.uploader.upload( - bannerImag.tempFilePath, - { - folder: "GetSygnal/loginImage", - } - ); - console.log("result", result); - const update = await LoginImageModel.findOneAndUpdate( - { _id: _id }, - { image: result }, // Provide the updated categoryName - { new: true } // To return the updated document - ); - if (!update) { - return res - .status(404) - .json({ message: "Can not update document, something went wrong" }); - } else { - return res.status(200).json({ success: true, update }); - } - } - } else { - const update = await LoginImageModel.findOneAndUpdate( - { _id: _id }, - { image: JSON.parse(olderImage) }, // Provide the updated categoryName - { new: true } // To return the updated document - ); - if (update) { - return res.status(200).json({ success: true, update }); - } - } - } catch (error) { - res.status(500).json({ - success: false, - message: error.message ? error.message : "Something went wrong", - }); + if (!mongoose.Types.ObjectId.isValid(_id)) { + return res.status(404).json({ error: "Can not find the document " }); } + + if (olderImage) { + const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id }); + + const deleteresponse = await cloudinary.v2.uploader.destroy( + deletefromCloudinary.image.public_id + ); + + if (deleteresponse) { + const result = await cloudinary.v2.uploader.upload( + bannerImag.tempFilePath, + { + folder: "Ayurpulse/loginImage", + } + ); + const update = await LoginImageModel.findOneAndUpdate( + { _id: _id }, + { image: result }, // Provide the updated categoryName + { new: true } // To return the updated document + ); + if (!update) { + return res + .status(404) + .json({ message: "Can not update document, something went wrong" }); + } else { + return res.status(200).json({ success: true, update }); + } + } + } else { + const update = await LoginImageModel.findOneAndUpdate( + { _id: _id }, + { image: JSON.parse(olderImage) }, // Provide the updated categoryName + { new: true } // To return the updated document + ); + if (update) { + return res.status(200).json({ success: true, update }); + } + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went wrong", + }); + } }; export const deleteImage = async (req, res) => { - try { - if (!req?.user) return res.status(400).json({ message: "please login !" }); - const { _id } = req.params; - if (!mongoose.Types.ObjectId.isValid(_id)) { - return res.status(404).json({ error: "Can not find the document " }); - } - - const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id }); - // console.log(deletefromCloudinary); - const deleteresponse = await cloudinary.v2.uploader.destroy( - deletefromCloudinary.image.public_id - ); - if (deleteresponse) { - const deleteBanner = await LoginImageModel.findOneAndDelete({ _id: _id }); - if (!deleteBanner) { - return res.status(404).json({ - error: "Can not find the document with the provided id to delete ", - }); - } - res.status(200).json({ success: true, deleteBanner }); - } else { - return res.status(404).json({ error: "can not delete the banner " }); - } - } catch (error) { - res.status(500).json({ - success: false, - message: error.message ? error.message : "Something went wrong", - }); + try { + if (!req?.user) return res.status(400).json({ message: "please login !" }); + const { _id } = req.params; + if (!mongoose.Types.ObjectId.isValid(_id)) { + return res.status(404).json({ error: "Can not find the document " }); } + + const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id }); + // console.log(deletefromCloudinary); + const deleteresponse = await cloudinary.v2.uploader.destroy( + deletefromCloudinary.image.public_id + ); + if (deleteresponse) { + const deleteBanner = await LoginImageModel.findOneAndDelete({ _id: _id }); + if (!deleteBanner) { + return res.status(404).json({ + error: "Can not find the document with the provided id to delete ", + }); + } + res.status(200).json({ success: true, deleteBanner }); + } else { + return res.status(404).json({ error: "can not delete the banner " }); + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went wrong", + }); + } +}; + +export const CreateAndUpdateImage = async (req, res) => { + try { + const LoginImag = req.files?.LoginImg; + const findImage = await LoginImageModel.find(); + if (findImage?.length > 0) { + const deleteImg = await cloudinary.v2.uploader.destroy( + findImage[0]?.image.public_id + ); + if (deleteImg) { + const result = await cloudinary.v2.uploader.upload( + LoginImag.tempFilePath, + { + folder: "Ayurpulse/loginImage", + } + ); + // console.log("result", result); + const update = await LoginImageModel.findOneAndUpdate( + { _id: findImage[0]?._id }, + { image: result }, // Provide the updated categoryName + { new: true } // To return the updated document + ); + if (update) + return res + .status(200) + .json({ message: "Login Image Update successfully" }); + } + } else { + const result = await cloudinary.v2.uploader.upload( + LoginImag.tempFilePath, + { + folder: "Ayurpulse/loginImage", + } + ); + if (result) { + const regsiterImage = await LoginImageModel.create({ + image: result, + addedBy: req.user._id, + }); + if (regsiterImage) { + return res.status(201).json({ + success: true, + regsiterImage, + message: " Login Image Added successfully", + }); + } + } + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went wrong", + }); + } }; diff --git a/resources/LoginImage/LoginImageRoute.js b/resources/LoginImage/LoginImageRoute.js index 27fe392..afd4818 100644 --- a/resources/LoginImage/LoginImageRoute.js +++ b/resources/LoginImage/LoginImageRoute.js @@ -3,6 +3,7 @@ import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; // import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js"; import { + CreateAndUpdateImage, addImage, deleteImage, getImage, @@ -17,6 +18,13 @@ router.route("/getImage").get(getImage); router .route("/update/:_id") .patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage); +router + .route("/addmodify") + .post( + isAuthenticatedUser, + authorizeRoles("admin", "Employee"), + CreateAndUpdateImage + ); router .route("/delete/:_id") .delete( diff --git a/resources/RegistrationImage/RegistrationImageController.js b/resources/RegistrationImage/RegistrationImageController.js index 487133e..ffef4a2 100644 --- a/resources/RegistrationImage/RegistrationImageController.js +++ b/resources/RegistrationImage/RegistrationImageController.js @@ -18,7 +18,7 @@ export const addImage = async (req, res) => { const result = await cloudinary.v2.uploader.upload( bannerImage.tempFilePath, { - folder: "GetSygnal/registrationImage", + folder: "Register/registrationImage", } ); @@ -84,7 +84,7 @@ export const getImage = async (req, res) => { // const result = await cloudinary.v2.uploader.upload( // bannerImag.tempFilePath, // { -// folder: "GetSygnal/registrationImage", +// folder: "Register/registrationImage", // } // ); @@ -152,10 +152,10 @@ export const updateImage = async (req, res) => { const result = await cloudinary.v2.uploader.upload( bannerImag.tempFilePath, { - folder: "GetSygnal/registrationImage", + folder: "Register/registrationImage", } ); - console.log("result", result); + // console.log("result", result); const update = await RegistrationImageModel.findOneAndUpdate( { _id: _id }, { image: result }, // Provide the updated categoryName @@ -222,3 +222,58 @@ export const deleteImage = async (req, res) => { }); } }; + +export const CreateAndUpdateImage = async (req, res) => { + try { + const RegisterImag = req.files?.RegisterImglogo; + const findImage = await RegistrationImageModel.find(); + if (findImage?.length > 0) { + const deleteImg = await cloudinary.v2.uploader.destroy( + findImage[0]?.image.public_id + ); + if (deleteImg) { + const result = await cloudinary.v2.uploader.upload( + RegisterImag.tempFilePath, + { + folder: "Ayurpulse/RegisterImage", + } + ); + // console.log("result", result); + const update = await RegistrationImageModel.findOneAndUpdate( + { _id: findImage[0]?._id }, + { image: result }, // Provide the updated categoryName + { new: true } // To return the updated document + ); + if (update) + return res + .status(200) + .json({ message: "Register Image Update successfully" }); + } + } else { + const result = await cloudinary.v2.uploader.upload( + RegisterImag.tempFilePath, + { + folder: "Ayurpulse/RegisterImage", + } + ); + if (result) { + const regsiterImage = await RegistrationImageModel.create({ + image: result, + addedBy: req.user._id, + }); + if (regsiterImage) { + return res.status(201).json({ + success: true, + regsiterImage, + message: " Register Image Added successfully", + }); + } + } + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went wrong", + }); + } +}; diff --git a/resources/RegistrationImage/RegistrationImageRoute.js b/resources/RegistrationImage/RegistrationImageRoute.js index 09542fa..5eac05e 100644 --- a/resources/RegistrationImage/RegistrationImageRoute.js +++ b/resources/RegistrationImage/RegistrationImageRoute.js @@ -6,6 +6,7 @@ import { deleteImage, getImage, updateImage, + CreateAndUpdateImage, } from "./RegistrationImageController.js"; const router = express.Router(); @@ -16,6 +17,13 @@ router.route("/getImage").get(getImage); router .route("/update/:_id") .patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage); +router + .route("/addmodify") + .post( + isAuthenticatedUser, + authorizeRoles("admin", "Employee"), + CreateAndUpdateImage + ); router .route("/delete/:_id") .delete( diff --git a/resources/ShopPageImage/ShopPageImageController.js b/resources/ShopPageImage/ShopPageImageController.js index 71e9560..7fc0a22 100644 --- a/resources/ShopPageImage/ShopPageImageController.js +++ b/resources/ShopPageImage/ShopPageImageController.js @@ -155,7 +155,7 @@ export const updateImage = async (req, res) => { folder: "GetSygnal/shopImage", } ); - console.log("result", result); + // console.log("result", result); const update = await ShopPageImageModel.findOneAndUpdate( { _id: _id }, { image: result }, // Provide the updated categoryName @@ -220,3 +220,58 @@ export const deleteImage = async (req, res) => { }); } }; + +export const CreateAndUpdateImage = async (req, res) => { + try { + const ShopImag = req.files?.ShopImg; + const findImage = await ShopPageImageModel.find(); + if (findImage?.length > 0) { + const deleteImg = await cloudinary.v2.uploader.destroy( + findImage[0]?.image.public_id + ); + if (deleteImg) { + const result = await cloudinary.v2.uploader.upload( + ShopImag.tempFilePath, + { + folder: "Ayurpulse/ShopImage", + } + ); + // console.log("result", result); + const update = await ShopPageImageModel.findOneAndUpdate( + { _id: findImage[0]?._id }, + { image: result }, // Provide the updated categoryName + { new: true } // To return the updated document + ); + if (update) + return res + .status(200) + .json({ message: "Shop Image Update successfully" }); + } + } else { + const result = await cloudinary.v2.uploader.upload( + ShopImag.tempFilePath, + { + folder: "Ayurpulse/shopImage", + } + ); + if (result) { + const shopImage = await ShopPageImageModel.create({ + image: result, + addedBy: req.user._id, + }); + if (shopImage) { + return res.status(201).json({ + success: true, + shopImage, + message: " Shop Image Added successfully", + }); + } + } + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went wrong", + }); + } +}; diff --git a/resources/ShopPageImage/ShopPageImageRoute.js b/resources/ShopPageImage/ShopPageImageRoute.js index 5d469ad..540262a 100644 --- a/resources/ShopPageImage/ShopPageImageRoute.js +++ b/resources/ShopPageImage/ShopPageImageRoute.js @@ -5,6 +5,7 @@ import { deleteImage, getImage, updateImage, + CreateAndUpdateImage, } from "./ShopPageImageController.js"; // import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js"; @@ -18,6 +19,13 @@ router.route("/getImage").get(getImage); router .route("/update/:_id") .patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage); +router + .route("/addmodify") + .post( + isAuthenticatedUser, + authorizeRoles("admin", "Employee"), + CreateAndUpdateImage + ); router .route("/delete/:_id") .delete(