diff --git a/app.js b/app.js index 701ad0d..1396269 100644 --- a/app.js +++ b/app.js @@ -161,6 +161,8 @@ import SeoRoute from "./resources/SEO&Analytics/SEORouter.js"; //Affiliate Routes import AffiliateRoute from "./resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js"; +//Blog Routes +import BlogRoute from "./resources/Blog/BlogRoute.js"; //Coupon Routes import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js"; //short urls @@ -218,6 +220,8 @@ app.use("/api/v1/affiliate", AffiliateRoute); //Coupons app.use("/api/v1/coupon", CouponRoute); +//Blog +app.use("/api/v1/blog", BlogRoute); //config specialty // app.use("/api/config/specialty", SpecialtiesRouter); //specialties diff --git a/resources/Blog/BlogController.js b/resources/Blog/BlogController.js new file mode 100644 index 0000000..79abfd3 --- /dev/null +++ b/resources/Blog/BlogController.js @@ -0,0 +1,63 @@ +import Blog from "./BlogModel.js"; + +export const createBlog = async (req, res) => { + const { title, tags, image, blog_content } = req.body; + console.log(req.body); + + // Checking Fields + if (!title || !tags || !image || !blog_content) { + return res.status(400).json({ + success: false, + message: "All fields are mandatory", + }); + } + + try { + let images = []; + let Allfiles = req.files.image; + console.log(Allfiles); + // if (!Array.isArray(Allfiles)) { + // Allfiles = [Allfiles]; // Convert to array if it's not already + // } + + // Allfiles.forEach((file) => { + // if (typeof file.tempFilePath === "string") { + // let filepath = file.tempFilePath; + // images.push(filepath); + // } + // }); + + // const newBlog = await Blog.create({ + // title, + // tags, + // image: images, // Assign the array of image file paths + // blog_content, + // }); + + res.status(201).json({ + success: true, + message: "Blog created successfully", + data: images, + }); + } catch (error) { + console.error("Error creating blog:", error); + res.status(500).json({ + success: false, + message: error.message ? error.message : "Internal server error", + }); + } +}; +export const getAllBlog = async (req, res) => { + try { + const saveData = await Blog.find(); + res.status(200).json({ + success: true, + message: saveData, + }); + } catch { + res.status(500).json({ + success: false, + message: "Internal server error", + }); + } +}; diff --git a/resources/Blog/BlogModel.js b/resources/Blog/BlogModel.js new file mode 100644 index 0000000..1b3a65b --- /dev/null +++ b/resources/Blog/BlogModel.js @@ -0,0 +1,28 @@ +import mongoose from "mongoose"; + +const { Schema, model } = mongoose; + +const blogSchema = new Schema( + { + title: { + type: String, + required: [true, "Title is required"], + }, + tags: { + type: [String], + required: [true, "Tags are required"], + }, + image: { + type: String, + }, + blog_content: { + type: Object, + required: [true, "Content is required"], + }, + }, + { timestamps: true } +); + +const Blog = model("Blog", blogSchema); + +export default Blog; diff --git a/resources/Blog/BlogRoute.js b/resources/Blog/BlogRoute.js new file mode 100644 index 0000000..772b7b6 --- /dev/null +++ b/resources/Blog/BlogRoute.js @@ -0,0 +1,11 @@ +import express from "express"; + +import { createBlog, getAllBlog } from "./BlogController.js"; +import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; + +const router = express.Router(); + +router.post("/create", createBlog); +router.get("/getallblog", getAllBlog); + +export default router; diff --git a/resources/Blog/dummy.json b/resources/Blog/dummy.json new file mode 100644 index 0000000..90dfec3 --- /dev/null +++ b/resources/Blog/dummy.json @@ -0,0 +1,5 @@ +[ + { + "title": "String0" + } +] diff --git a/resources/Content/ContentController.js b/resources/Content/ContentController.js index f7500ff..b366441 100644 --- a/resources/Content/ContentController.js +++ b/resources/Content/ContentController.js @@ -51,16 +51,24 @@ export const getTermsAndCondition = async (req, res) => { export const updateTermsAndConditions = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) - const { content } = req.body; - const termsAndCondition = await TermsAndCondition.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - termsAndContionContent: content, - } - ); + // 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 + } + + // update the terms and conndition in database + const termsAndCondition = await TermsAndCondition.findByIdAndUpdate( + { _id: id }, + { $set: updatedTermsData }, + { new: true } + ); res.status(200).json({ success: true, @@ -80,23 +88,10 @@ export const RefundPolicy = async (req, res) => { if (!req?.user) return res.status(400).json({ message: "please login !" }); // console.log(req?.user) const { content } = req.body; - const findv = await Refundpolicy.findOne(); - let refundPolicy; - if (findv) { - refundPolicy = await Refundpolicy.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - Refundpolicy: content, - } - ); - } else { - refundPolicy = await Refundpolicy.create({ + const refundPolicy = await Refundpolicy.create({ addedBy: req.user._id, Refundpolicy: content, }); - } res.status(200).json({ success: true, @@ -130,6 +125,42 @@ export const getRefundPolicy = async (req, res) => { } }; +// update refund policy +export const updateRefundPolicy = async (req, res) => { + try { + if (!req?.user) return res.status(400).json({ message: "please login !" }); + + const {content} = req.body; + // id of the refund policy document + const id = req.query.id; + + // object for updated refund policy data + const updatedRefundPolicyData = { + Refundpolicy: content, + addedBy: req.user._id + } + + // update the refund policy in database + const refundPolicy = await Refundpolicy.findByIdAndUpdate( + { _id: id }, + { $set: updatedRefundPolicyData }, + { new: true } + ); + + res.status(200).json({ + success: true, + refundPolicy, + message: "updated successfully ", + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went Wrong", + }); + } +}; + + // Privacy policy controller functions export const AddPrivacyAndPolicy = async (req, res) => { @@ -180,15 +211,24 @@ export const getPrivacyPolicy = async (req, res) => { export const updatePrivacyPolicy = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) + + // new content const { content } = req.body; - const privacyAndPolicy = await PrivacyAndPolicy.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - privacyAndPolicyContent: content, - } + + // id of the privacy policy document + const id = req.query.id; + + // object for updated privacy policy data + const updatedPrivacyPolicyData = { + privacyAndPolicyContent: content, + addedBy: req.user._id + } + + // update the privacy policy in database + const privacyAndPolicy = await PrivacyAndPolicy.findByIdAndUpdate( + { _id: id }, + { $set: updatedPrivacyPolicyData }, + { new: true } ); res.status(200).json({ @@ -254,15 +294,23 @@ export const getShipping = async (req, res) => { export const updateShipping = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) + // new content const { content } = req.body; - const shipping = await Shipping.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - shippingContent: content, - } + + // id of the shipping policy document + const id = req.query.id; + + // object for updated shipping policy data + const updatedShippingData = { + shippingContent: content, + addedBy: req.user._id + } + + // update the shipping policy in database + const shipping = await Shipping.findByIdAndUpdate( + { _id: id }, + { $set: updatedShippingData }, + { new: true } ); res.status(200).json({ diff --git a/resources/Content/ContentRoutes.js b/resources/Content/ContentRoutes.js index 907dafe..eddc29a 100644 --- a/resources/Content/ContentRoutes.js +++ b/resources/Content/ContentRoutes.js @@ -11,6 +11,7 @@ import { updatePrivacyPolicy, updateShipping, updateTermsAndConditions, + updateRefundPolicy } from "./ContentController.js"; import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; @@ -46,7 +47,10 @@ router router.route("/refund-policy").get(getRefundPolicy); router .route("/refund-policy") - .patch(isAuthenticatedUser, authorizeRoles("admin"), RefundPolicy); + .post(isAuthenticatedUser, authorizeRoles("admin"), RefundPolicy); +router + .route("/refund-policy-update") + .patch(isAuthenticatedUser, authorizeRoles("admin"), updateRefundPolicy); // export default router; diff --git a/resources/Products/ProductController.js b/resources/Products/ProductController.js index 02799f3..d1de412 100644 --- a/resources/Products/ProductController.js +++ b/resources/Products/ProductController.js @@ -1,7 +1,7 @@ import { Product } from "./ProductModel.js"; import cloudinary from "../../Utils/cloudinary.js"; import { v4 as uuidv4 } from "uuid"; - +import { CategoryModel } from "../Category/CategoryModel.js"; export const createProduct = async (req, res) => { try { if (!req.files) { @@ -334,13 +334,20 @@ export const deleteProduct = async (req, res) => { }); } }; + export const getProductsByCategory = async (req, res) => { const { categoryName } = req.params; // Assuming category name is in the route + // console.log(categoryName); try { - const products = await Product.find({ - category: categoryName, - }).sort({ createdAt: -1 }); + // Find the category object by name first + const category = await CategoryModel.findOne({ categoryName }); + + if (!category) { + throw new Error("Category not found"); + } + const products = await Product.find({ category: category._id }).populate('category'); + // console.log(products); if (products && products.length > 0) { return res.status(200).json({ diff --git a/resources/Supports/supportRoute.js b/resources/Supports/supportRoute.js index dcc3c18..3cacdb0 100644 --- a/resources/Supports/supportRoute.js +++ b/resources/Supports/supportRoute.js @@ -11,15 +11,15 @@ app.use(bodyParser.raw({ type: "application/json" })); const router = express.Router(); //checkout Routes-------------------------// router.route("/support/create/").post(isAuthenticatedUser,createSupport); -router.route("/support/getAll/").get(getAllSupportTicket); +router.route("/support/getAll/").get(isAuthenticatedUser, authorizeRoles("admin"),getAllSupportTicket); router.route("/support/userticket/").get(isAuthenticatedUser,getAllSupportTicketofuser); router .route("/support/delete/:id") .delete( deleteSupport); - router.route("/support/getOne/:id").get(getOneSupportTicket); + router.route("/support/getOne/:id").get(isAuthenticatedUser, getOneSupportTicket); router .route("/support/update/:id") - .patch(updateSupport); + .patch(isAuthenticatedUser, updateSupport); router .route("/support/deleteImage/jatinMor/CustomerSupport/:public_id") .delete( diff --git a/resources/Testimonials/TestimonialController.js b/resources/Testimonials/TestimonialController.js index 98798d8..2268ecf 100644 --- a/resources/Testimonials/TestimonialController.js +++ b/resources/Testimonials/TestimonialController.js @@ -3,7 +3,7 @@ import { Testimonial } from "./TestimonialModel.js"; export const AddNewTestimonial = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) + // console.log(req?.user); if (req.files) { let getImg = req.files.image; @@ -36,8 +36,6 @@ export const AddNewTestimonial = async (req, res) => { export const FindAllTestimonial = async (req, res) => { try { - if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) const testimonial = await Testimonial.find().sort({ createdAt: -1 }); if (testimonial) { @@ -68,6 +66,7 @@ export const FindOneTestimonial = async (req, res) => { return res.status(400).json({ message: "please give ID !" }); const testimonial = await Testimonial.findById(req.params.id); + // console.log(testimonial); if (testimonial) { return res.status(200).json({ success: true, @@ -82,3 +81,125 @@ export const FindOneTestimonial = async (req, res) => { }); } }; + +// 3.update testimonials +export const updatetesTimonial = async (req, res) => { + try { + // Check if the user is authenticated + if (!req.user) { + return res.status(400).json({ message: "Please login!" }); + } + + // Destructure request body + const { name, company, testimonial } = req.body; + + // Get the authenticated user's ID + const userId = req.user._id; + + // Prepare an object for the updated testimonial data + const updatedTestimonialData = { + name, + company, + testimonial, + user: userId, // Assign the authenticated user's ID to the testimonial's user field + }; + + // Check if files are uploaded + if (req.files && req.files.image) { + // If image file is uploaded, upload it to cloudinary + const uploadedImage = req.files.image; + const result = await cloudinary.v2.uploader.upload( + uploadedImage.tempFilePath, + { + folder: "GetSygnal/Testimonial", + } + ); + + // Prepare the image object with public_id and url + const image = { + public_id: result.public_id, + url: result.secure_url, + }; + + // Assign the uploaded image to the testimonial's image field + updatedTestimonialData.image = image; + } + // console.log(updatedTestimonialData); + // Update the testimonial in the database + const modifiedTestimonial = await Testimonial.findOneAndUpdate( + { _id: req.params.id }, + { $set: updatedTestimonialData }, + { new: true } + ); + + return res.status(200).json({ + success: true, + ModifyTestimonial: modifiedTestimonial, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: error.message ? error.message : "Something went wrong!", + }); + } +}; + +export const deleteImageFromCloudinary = async (req, res) => { + const { public_id } = req.params; + + try { + if (!public_id) { + return res.status(400).json({ + success: false, + msg: "Please Provide Product ID!", + }); + } + const response = await cloudinary.v2.uploader.destroy(public_id); + if (response) { + res.status(200).json({ + success: true, + msg: "Product Deleted Successfully!!", + }); + } + } catch (error) { + res.status(500).json({ + success: false, + msg: error.message ? error.message : "Something went wrong!", + }); + } +}; + +//delete one Product +export const deleteTestimonial = async (req, res) => { + try { + if (!req?.user) return res.status(400).json({ message: "please login !" }); + // console.log(req?.user) + if (!req.params.id) + return res.status(400).json({ message: "please give ID !" }); + // console.log(req.params.id) + const gettestimonial = await Testimonial.findById(req.params.id); + // console.log(gettestimonial) + if (!gettestimonial) { + return res + .status(404) + .json({ success: false, msg: "Testimonial not Found!" }); + } + // Deleting Images From Cloudinary + await cloudinary.v2.uploader.destroy(gettestimonial.image.public_id); + + //-------------------------// + const testimonial = await Testimonial.findByIdAndDelete(req.params.id); + if (!testimonial) { + return res.status(404).json({ message: "Testimonial Not Found" }); + } + await testimonial.remove(); + res + .status(200) + .json({ success: true, msg: "Testimonial Deleted Successfully!!" }); + } catch (error) { + res.status(500).json({ + success: false, + msg: error.message ? error.message : "Something went wrong!", + }); + } +}; diff --git a/resources/Testimonials/TestimonialRoute.js b/resources/Testimonials/TestimonialRoute.js index 90fec8c..9dda39f 100644 --- a/resources/Testimonials/TestimonialRoute.js +++ b/resources/Testimonials/TestimonialRoute.js @@ -4,6 +4,9 @@ import { AddNewTestimonial, FindAllTestimonial, FindOneTestimonial, + deleteImageFromCloudinary, + deleteTestimonial, + updatetesTimonial, } from "./TestimonialController.js"; const router = express.Router(); @@ -11,9 +14,19 @@ const router = express.Router(); router.route("/new").post(isAuthenticatedUser, AddNewTestimonial); router .route("/getAll") - .get(isAuthenticatedUser, authorizeRoles("admin"), FindAllTestimonial); + .get(FindAllTestimonial); router.route("/getOne/:id").get(isAuthenticatedUser, FindOneTestimonial); - -// router.route("/product/getAll/").get(getAllProduct) - +router + .route("/delete/:id") + .delete(isAuthenticatedUser, authorizeRoles("admin"), deleteTestimonial); + router + .route("/update/:id") + .patch(isAuthenticatedUser, authorizeRoles("admin"), updatetesTimonial); + router + .route("/deleteImage/GetSygnal/Testimonial/:public_id") + .delete( + isAuthenticatedUser, + authorizeRoles("admin"), + deleteImageFromCloudinary + ); export default router;