Testimonial API for create,edit,delete is complete and fix small error in support section

This commit is contained in:
Sibunnayak 2024-03-27 10:49:58 +05:30
parent 79d133e238
commit c6ebde79f6
3 changed files with 143 additions and 7 deletions

View File

@ -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(

View File

@ -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;
@ -68,6 +68,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 +83,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!",
});
}
};

View File

@ -4,6 +4,9 @@ import {
AddNewTestimonial,
FindAllTestimonial,
FindOneTestimonial,
deleteImageFromCloudinary,
deleteTestimonial,
updatetesTimonial,
} from "./TestimonialController.js";
const router = express.Router();
@ -13,7 +16,17 @@ router
.route("/getAll")
.get(isAuthenticatedUser, authorizeRoles("admin"), 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;