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 Blog from "./BlogModel.js";
|
||||||
|
import cloudinary from "../../Utils/cloudinary.js";
|
||||||
|
|
||||||
export const createBlog = async (req, res) => {
|
export const createBlog = async (req, res) => {
|
||||||
const { title, tags, image, blog_content } = req.body;
|
const { title, tags, 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 {
|
try {
|
||||||
let images = [];
|
let image; // To store Cloudinary image details
|
||||||
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 (req.files && req.files.image) {
|
||||||
// if (typeof file.tempFilePath === "string") {
|
const imageFile = req.files.image;
|
||||||
// let filepath = file.tempFilePath;
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
// images.push(filepath);
|
imageFile.tempFilePath,
|
||||||
// }
|
{
|
||||||
// });
|
folder: "smellica/Blog",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// const newBlog = await Blog.create({
|
image = {
|
||||||
// title,
|
public_id: result.public_id,
|
||||||
// tags,
|
url: result.secure_url,
|
||||||
// image: images, // Assign the array of image file paths
|
};
|
||||||
// blog_content,
|
}
|
||||||
// });
|
|
||||||
|
// 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({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
blog,
|
||||||
message: "Blog created successfully",
|
message: "Blog created successfully",
|
||||||
data: images,
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating blog:", error);
|
console.error("Error creating blog:", error);
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: error.message ? error.message : "Internal server error",
|
message: error.message || "Internal server error",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const getAllBlog = async (req, res) => {
|
export const getAllBlog = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const saveData = await Blog.find();
|
const BlogData = await Blog.find().sort({ createdAt: -1 });
|
||||||
res.status(200).json({
|
if (BlogData) {
|
||||||
success: true,
|
return res.status(200).json({
|
||||||
message: saveData,
|
success: true,
|
||||||
});
|
BlogData,
|
||||||
|
message: "Fetched All Blog",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: true,
|
||||||
|
message: "No Blog till Now",
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
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,7 +13,12 @@ const blogSchema = new Schema(
|
|||||||
required: [true, "Tags are required"],
|
required: [true, "Tags are required"],
|
||||||
},
|
},
|
||||||
image: {
|
image: {
|
||||||
type: String,
|
public_id: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
url: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
blog_content: {
|
blog_content: {
|
||||||
type: Object,
|
type: Object,
|
||||||
|
@ -1,11 +1,30 @@
|
|||||||
import express from "express";
|
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";
|
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.post("/create", createBlog);
|
router
|
||||||
router.get("/getallblog", getAllBlog);
|
.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;
|
export default router;
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"title": "String0"
|
|
||||||
}
|
|
||||||
]
|
|
Loading…
Reference in New Issue
Block a user