Blog Create,update,delete,read and readAll controller Done
This commit is contained in:
parent
76166c1995
commit
6fdb7451a6
@ -1,59 +1,64 @@
|
||||
import Blog from "./BlogModel.js";
|
||||
import cloudinary from "../../Utils/cloudinary.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",
|
||||
});
|
||||
}
|
||||
const { title, tags, blog_content } = req.body;
|
||||
|
||||
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
|
||||
// }
|
||||
let image; // To store Cloudinary image details
|
||||
|
||||
// Allfiles.forEach((file) => {
|
||||
// if (typeof file.tempFilePath === "string") {
|
||||
// let filepath = file.tempFilePath;
|
||||
// images.push(filepath);
|
||||
// }
|
||||
// });
|
||||
if (req.files && req.files.image) {
|
||||
const imageFile = req.files.image;
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
imageFile.tempFilePath,
|
||||
{
|
||||
folder: "smellica/Blog",
|
||||
}
|
||||
);
|
||||
|
||||
// const newBlog = await Blog.create({
|
||||
// title,
|
||||
// tags,
|
||||
// image: images, // Assign the array of image file paths
|
||||
// blog_content,
|
||||
// });
|
||||
image = {
|
||||
public_id: result.public_id,
|
||||
url: result.secure_url,
|
||||
};
|
||||
}
|
||||
|
||||
// Create the blog post
|
||||
const blog = await Blog.create({
|
||||
title,
|
||||
tags: tags.split(/\s*,\s*|\s+/)
|
||||
.filter(tag => tag.trim() !== ''), // Splitting tags string into array
|
||||
image,
|
||||
blog_content,
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
blog,
|
||||
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",
|
||||
message: error.message || "Internal server error",
|
||||
});
|
||||
}
|
||||
};
|
||||
export const getAllBlog = async (req, res) => {
|
||||
try {
|
||||
const saveData = await Blog.find();
|
||||
res.status(200).json({
|
||||
const BlogData = await Blog.find().sort({ createdAt: -1 });
|
||||
if (BlogData) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: saveData,
|
||||
BlogData,
|
||||
message: "Fetched All Blog",
|
||||
});
|
||||
} else {
|
||||
return res.status(404).json({
|
||||
success: true,
|
||||
message: "No Blog till Now",
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
@ -61,3 +66,140 @@ export const getAllBlog = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
//get single Blog
|
||||
export const getOneBlog = async (req, res) => {
|
||||
try {
|
||||
// console.log(req.params.id);
|
||||
const blog = await Blog.findById(req.params.id);
|
||||
if (blog) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
blog,
|
||||
});
|
||||
} else {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
msg: "Blog not found",
|
||||
});
|
||||
}
|
||||
} 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: "Blog Deleted Successfully!!",
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
//delete one Product
|
||||
export const deleteBlog = 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 Blog ID !" });
|
||||
// console.log(req.params.id)
|
||||
const getblog = await Blog.findById(req.params.id);
|
||||
// console.log(getblog)
|
||||
if (!getblog) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, msg: "Testimonial not Found!" });
|
||||
}
|
||||
// Deleting Images From Cloudinary
|
||||
await cloudinary.v2.uploader.destroy(getblog.image.public_id);
|
||||
|
||||
//-------------------------//
|
||||
const blog = await Blog.findByIdAndDelete(req.params.id);
|
||||
if (!blog) {
|
||||
return res.status(404).json({ message: "blog Not Found" });
|
||||
}
|
||||
await blog.remove();
|
||||
res.status(200).json({ success: true, msg: "blog Deleted Successfully!!" });
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//update blog
|
||||
export const updateBlog = 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 { title, tags, blog_content } = req.body;
|
||||
|
||||
// Prepare an object for the updated testimonial data
|
||||
const updatedBlogData = {
|
||||
title,
|
||||
tags: tags.split(/\s*,\s*|\s+/)
|
||||
.filter(tag => tag.trim() !== ''), // Splitting tags string into array
|
||||
blog_content,
|
||||
};
|
||||
|
||||
// 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: "smellica/Blog",
|
||||
}
|
||||
);
|
||||
|
||||
// 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 Blog's image field
|
||||
updatedBlogData.image = image;
|
||||
}
|
||||
const modifiedBlog = await Blog.findOneAndUpdate(
|
||||
{ _id: req.params.id },
|
||||
{ $set: updatedBlogData },
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
ModifyBlog: modifiedBlog,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -13,8 +13,13 @@ const blogSchema = new Schema(
|
||||
required: [true, "Tags are required"],
|
||||
},
|
||||
image: {
|
||||
public_id: {
|
||||
type: String,
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
blog_content: {
|
||||
type: Object,
|
||||
required: [true, "Content is required"],
|
||||
|
@ -1,11 +1,30 @@
|
||||
import express from "express";
|
||||
|
||||
import { createBlog, getAllBlog } from "./BlogController.js";
|
||||
import { createBlog, getAllBlog, getOneBlog, deleteBlog, deleteImageFromCloudinary, updateBlog } from "./BlogController.js";
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/create", createBlog);
|
||||
router.get("/getallblog", getAllBlog);
|
||||
|
||||
router
|
||||
.route("/create")
|
||||
.post(isAuthenticatedUser, authorizeRoles("admin"), createBlog);
|
||||
router
|
||||
.route("/getallblog")
|
||||
.get(getAllBlog);
|
||||
router
|
||||
.route("/getoneblog/:id")
|
||||
.get(getOneBlog);
|
||||
router
|
||||
.route("/deleteblog/:id")
|
||||
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteBlog);
|
||||
router
|
||||
.route("/deleteImage/jatinMor/Blog/:public_id")
|
||||
.delete(
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
deleteImageFromCloudinary
|
||||
);
|
||||
router
|
||||
.route("/updateblog/:id")
|
||||
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateBlog);
|
||||
export default router;
|
||||
|
@ -1,5 +0,0 @@
|
||||
[
|
||||
{
|
||||
"title": "String0"
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue
Block a user