2026-03-12 14:15:44 +05:30
|
|
|
import prisma from "../prisma/client.js";
|
2026-04-14 15:25:20 +05:30
|
|
|
import slugify from "slugify";
|
2026-03-12 14:15:44 +05:30
|
|
|
|
|
|
|
|
/* CREATE BLOG */
|
|
|
|
|
|
|
|
|
|
export async function createBlog(req, res) {
|
2026-04-14 15:25:20 +05:30
|
|
|
const { title, writer, image, content, isActive } = req.body;
|
2026-03-12 14:15:44 +05:30
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const blog = await prisma.blog.create({
|
|
|
|
|
data: {
|
|
|
|
|
title,
|
|
|
|
|
writer,
|
|
|
|
|
image,
|
|
|
|
|
content,
|
|
|
|
|
isActive,
|
2026-04-14 15:25:20 +05:30
|
|
|
slug: slugify(title),
|
2026-03-12 14:15:44 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json(blog);
|
|
|
|
|
} catch (error) {
|
2026-04-14 15:25:20 +05:30
|
|
|
res.status(500).json({ error: "Blog creation failed" });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GET ALL BLOGS (Public) */
|
|
|
|
|
|
|
|
|
|
export async function getBlogs(req, res) {
|
|
|
|
|
try {
|
|
|
|
|
const blogs = await prisma.blog.findMany({
|
2026-04-14 15:25:20 +05:30
|
|
|
where: { isActive: true },
|
|
|
|
|
orderBy: { createdAt: "desc" },
|
2026-03-12 14:15:44 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json(blogs);
|
|
|
|
|
} catch (error) {
|
2026-04-14 15:25:20 +05:30
|
|
|
res.status(500).json({ error: error.message });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GET ALL BLOGS (Admin) */
|
|
|
|
|
|
|
|
|
|
export async function getAllBlogs(req, res) {
|
|
|
|
|
try {
|
|
|
|
|
const blogs = await prisma.blog.findMany({
|
2026-04-14 15:25:20 +05:30
|
|
|
orderBy: { createdAt: "desc" },
|
2026-03-12 14:15:44 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json(blogs);
|
|
|
|
|
} catch (error) {
|
2026-04-14 15:25:20 +05:30
|
|
|
res.status(500).json({ error: error.message });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GET SINGLE BLOG */
|
|
|
|
|
|
|
|
|
|
export async function getBlog(req, res) {
|
2026-04-14 15:25:20 +05:30
|
|
|
try {
|
|
|
|
|
const slug = req.params.slug;
|
|
|
|
|
console.log({ slug });
|
|
|
|
|
|
|
|
|
|
const blog = await prisma.blog.findUnique({
|
|
|
|
|
where: { slug },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!blog) {
|
|
|
|
|
return res.status(404).json({ error: "Blog not found" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(blog);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GET SINGLE BLOG (ADMIN)*/
|
|
|
|
|
|
|
|
|
|
export async function getBlogForAdmin(req, res) {
|
2026-03-12 14:15:44 +05:30
|
|
|
try {
|
|
|
|
|
const id = Number(req.params.id);
|
|
|
|
|
|
|
|
|
|
const blog = await prisma.blog.findUnique({
|
2026-04-14 15:25:20 +05:30
|
|
|
where: { id },
|
2026-03-12 14:15:44 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!blog) {
|
2026-04-14 15:25:20 +05:30
|
|
|
return res.status(404).json({ error: "Blog not found" });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(blog);
|
|
|
|
|
} catch (error) {
|
2026-04-14 15:25:20 +05:30
|
|
|
res.status(500).json({ error: error.message });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* UPDATE BLOG */
|
|
|
|
|
|
|
|
|
|
export async function updateBlog(req, res) {
|
|
|
|
|
try {
|
2026-04-14 15:25:20 +05:30
|
|
|
const { title, writer, image, content } = req.body;
|
2026-03-12 14:15:44 +05:30
|
|
|
|
|
|
|
|
const blog = await prisma.blog.update({
|
2026-04-14 15:25:20 +05:30
|
|
|
where: { id: Number(req.params.id) },
|
2026-03-12 14:15:44 +05:30
|
|
|
data: {
|
|
|
|
|
title,
|
|
|
|
|
writer,
|
|
|
|
|
image,
|
|
|
|
|
content,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json(blog);
|
|
|
|
|
} catch (error) {
|
2026-04-14 15:25:20 +05:30
|
|
|
res.status(500).json({ error: error.message });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DELETE BLOG */
|
|
|
|
|
|
|
|
|
|
export async function deleteBlog(req, res) {
|
|
|
|
|
try {
|
|
|
|
|
const id = Number(req.params.id);
|
|
|
|
|
|
|
|
|
|
await prisma.blog.delete({
|
2026-04-14 15:25:20 +05:30
|
|
|
where: { id },
|
2026-03-12 14:15:44 +05:30
|
|
|
});
|
|
|
|
|
|
2026-04-14 15:25:20 +05:30
|
|
|
res.json({ message: "Blog deleted successfully" });
|
2026-03-12 14:15:44 +05:30
|
|
|
} catch (error) {
|
2026-04-14 15:25:20 +05:30
|
|
|
res.status(500).json({ error: error.message });
|
2026-03-12 14:15:44 +05:30
|
|
|
}
|
|
|
|
|
}
|