Spaces:
Sleeping
Sleeping
File size: 5,358 Bytes
d53e5b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import { Request, Response } from 'express';
import prisma from '../models/prisma';
import slugify from 'slugify';
// GET /api/admin/blog
export const getAdminBlogPosts = async (req: Request, res: Response) => {
try {
const posts = await prisma.blogPost.findMany({
orderBy: { createdAt: 'desc' },
include: { author: { select: { name: true, email: true } } }
});
return res.status(200).json(posts);
} catch (error) {
console.error('Error fetching admin blog posts:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// GET /api/admin/blog/:id
export const getAdminBlogPost = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const post = await prisma.blogPost.findUnique({
where: { id },
include: { author: { select: { name: true, email: true } } }
});
if (!post) {
return res.status(404).json({ error: 'Post not found' });
}
return res.status(200).json(post);
} catch (error) {
console.error('Error fetching admin blog post:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// POST /api/admin/blog
export const createAdminBlogPost = async (req: Request, res: Response) => {
try {
const userId = (req as any).user.userId;
const { title, slug, content, excerpt, featuredImage, tags, status, publishedAt } = req.body;
if (!title || !content) {
return res.status(400).json({ error: 'Title and content are required' });
}
const generatedSlug = slug ? slugify(slug, { lower: true, strict: true }) : slugify(title, { lower: true, strict: true });
// Check for existing slug
const existing = await prisma.blogPost.findUnique({ where: { slug: generatedSlug } });
if (existing) {
return res.status(400).json({ error: 'A post with this slug already exists' });
}
const newPost = await prisma.blogPost.create({
data: {
title,
slug: generatedSlug,
content,
excerpt: excerpt || null,
featuredImage: featuredImage || null,
tags: tags || null,
status: status || 'DRAFT',
publishedAt: publishedAt ? new Date(publishedAt) : null,
authorId: userId,
}
});
return res.status(201).json(newPost);
} catch (error) {
console.error('Error creating admin blog post:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// PUT /api/admin/blog/:id
export const updateAdminBlogPost = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { title, slug, content, excerpt, featuredImage, tags, status, publishedAt } = req.body;
const existingPost = await prisma.blogPost.findUnique({ where: { id } });
if (!existingPost) {
return res.status(404).json({ error: 'Post not found' });
}
let updateSlug = existingPost.slug;
if (slug && slug !== existingPost.slug) {
updateSlug = slugify(slug, { lower: true, strict: true });
const checkSlug = await prisma.blogPost.findUnique({ where: { slug: updateSlug } });
if (checkSlug) {
return res.status(400).json({ error: 'A post with this slug already exists' });
}
}
const updatedPost = await prisma.blogPost.update({
where: { id },
data: {
title: title !== undefined ? title : existingPost.title,
slug: updateSlug,
content: content !== undefined ? content : existingPost.content,
excerpt: excerpt !== undefined ? excerpt : existingPost.excerpt,
featuredImage: featuredImage !== undefined ? featuredImage : existingPost.featuredImage,
tags: tags !== undefined ? tags : existingPost.tags,
status: status !== undefined ? status : existingPost.status,
publishedAt: publishedAt !== undefined ? (publishedAt ? new Date(publishedAt) : null) : existingPost.publishedAt,
}
});
return res.status(200).json(updatedPost);
} catch (error) {
console.error('Error updating admin blog post:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// DELETE /api/admin/blog/:id
export const deleteAdminBlogPost = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const existingPost = await prisma.blogPost.findUnique({ where: { id } });
if (!existingPost) {
return res.status(404).json({ error: 'Post not found' });
}
await prisma.blogPost.delete({ where: { id } });
return res.status(200).json({ message: 'Post deleted successfully' });
} catch (error) {
console.error('Error deleting admin blog post:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
|