Spaces:
Sleeping
Sleeping
Download src/controllers/blog.controller.ts from underrate/webdev-service-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/underrate/webdev-service-backend/resolve/main/src/controllers/blog.controller.ts
- Command line
-
hf download hf://spaces/underrate/webdev-service-backend/src/controllers/blog.controller.ts
-
curl -L -o blog.controller.ts https://huggingface.co/spaces/underrate/webdev-service-backend/resolve/main/src/controllers/blog.controller.ts
1.87 kB
| import { Request, Response } from 'express'; | |
| import prisma from '../models/prisma'; | |
| // GET /api/blog (Public) | |
| export const getPublishedPosts = async (req: Request, res: Response) => { | |
| try { | |
| const posts = await prisma.blogPost.findMany({ | |
| where: { | |
| status: 'PUBLISHED', | |
| publishedAt: { | |
| lte: new Date(), | |
| }, | |
| }, | |
| orderBy: { | |
| publishedAt: 'desc', | |
| }, | |
| include: { | |
| author: { | |
| select: { | |
| name: true, | |
| }, | |
| }, | |
| }, | |
| }); | |
| return res.status(200).json(posts); | |
| } catch (error) { | |
| console.error('Error fetching blog posts:', error); | |
| return res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }; | |
| // GET /api/blog/:slug (Public) | |
| export const getPostBySlug = async (req: Request, res: Response) => { | |
| try { | |
| const { slug } = req.params; | |
| const post = await prisma.blogPost.findFirst({ | |
| where: { | |
| slug, | |
| status: 'PUBLISHED', | |
| publishedAt: { | |
| lte: new Date(), // ensure it's not a scheduled post in the future | |
| }, | |
| }, | |
| include: { | |
| author: { | |
| select: { | |
| name: true, | |
| }, | |
| }, | |
| }, | |
| }); | |
| if (!post) { | |
| return res.status(404).json({ error: 'Post not found' }); | |
| } | |
| return res.status(200).json(post); | |
| } catch (error) { | |
| console.error('Error fetching blog post:', error); | |
| return res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }; | |