Spaces:
Sleeping
Sleeping
Download src/controllers/admin-blog.controller.ts from underrate/webdev-service-backend: direct link, hf CLI and curl.
- Browser
- Download file 5.36 kB
-
https://huggingface.co/spaces/underrate/webdev-service-backend/resolve/main/src/controllers/admin-blog.controller.ts
- Command line
-
hf download hf://spaces/underrate/webdev-service-backend/src/controllers/admin-blog.controller.ts
-
curl -L -o admin-blog.controller.ts https://huggingface.co/spaces/underrate/webdev-service-backend/resolve/main/src/controllers/admin-blog.controller.ts
5.36 kB
| 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' }); | |
| } | |
| }; | |