@@ -4,6 +4,15 @@ import { prisma } from '../lib/prisma'
44
55export const postRouter = Router ( )
66
7+ function parsePostId ( id : string | string [ ] , res : Response ) : number | null {
8+ const postId = Number ( id )
9+ if ( ! Number . isInteger ( postId ) || postId <= 0 ) {
10+ res . status ( 400 ) . json ( { error : `Invalid post ID: ${ id } ` } )
11+ return null
12+ }
13+ return postId
14+ }
15+
716// GET /feed — all published posts
817postRouter . get ( '/feed' , async ( _req : Request , res : Response ) => {
918 const posts = await prisma . post . findMany ( {
@@ -15,25 +24,25 @@ postRouter.get('/feed', async (_req: Request, res: Response) => {
1524
1625// GET /post/:id — single post by id
1726postRouter . get ( '/post/:id' , async ( req : Request , res : Response ) => {
18- const { id } = req . params
19- const postId = Number ( id )
20- if ( ! Number . isInteger ( postId ) ) {
21- res . status ( 400 ) . json ( { error : `Invalid post ID: ${ id } ` } )
22- return
23- }
27+ const postId = parsePostId ( req . params . id , res )
28+ if ( postId === null ) return
2429 const post = await prisma . post . findUnique ( {
2530 where : { id : postId } ,
2631 } )
2732 if ( ! post ) {
28- res . status ( 404 ) . json ( { error : `Post with ID ${ id } not found` } )
33+ res . status ( 404 ) . json ( { error : `Post with ID ${ req . params . id } not found` } )
2934 return
3035 }
3136 res . json ( post )
3237} )
3338
3439// POST /post — create post
3540postRouter . post ( '/post' , async ( req : Request , res : Response ) => {
36- const { title, content, authorEmail } = req . body
41+ const { title, authorEmail, content } = req . body
42+ if ( ! title || ! authorEmail ) {
43+ res . status ( 400 ) . json ( { error : 'title and authorEmail are required' } )
44+ return
45+ }
3746 const post = await prisma . post . create ( {
3847 data : {
3948 title,
@@ -46,12 +55,8 @@ postRouter.post('/post', async (req: Request, res: Response) => {
4655
4756// PUT /publish/:id — publish a post
4857postRouter . put ( '/publish/:id' , async ( req : Request , res : Response ) => {
49- const { id } = req . params
50- const postId = Number ( id )
51- if ( ! Number . isInteger ( postId ) ) {
52- res . status ( 400 ) . json ( { error : `Invalid post ID: ${ id } ` } )
53- return
54- }
58+ const postId = parsePostId ( req . params . id , res )
59+ if ( postId === null ) return
5560 try {
5661 const post = await prisma . post . update ( {
5762 where : { id : postId } ,
@@ -63,7 +68,7 @@ postRouter.put('/publish/:id', async (req: Request, res: Response) => {
6368 error instanceof Prisma . PrismaClientKnownRequestError &&
6469 error . code === 'P2025'
6570 ) {
66- res . status ( 404 ) . json ( { error : `Post with ID ${ id } not found` } )
71+ res . status ( 404 ) . json ( { error : `Post with ID ${ req . params . id } not found` } )
6772 return
6873 }
6974 res . status ( 500 ) . json ( { error : 'Internal server error' } )
@@ -72,12 +77,8 @@ postRouter.put('/publish/:id', async (req: Request, res: Response) => {
7277
7378// DELETE /post/:id — delete post
7479postRouter . delete ( '/post/:id' , async ( req : Request , res : Response ) => {
75- const { id } = req . params
76- const postId = Number ( id )
77- if ( ! Number . isInteger ( postId ) ) {
78- res . status ( 400 ) . json ( { error : `Invalid post ID: ${ id } ` } )
79- return
80- }
80+ const postId = parsePostId ( req . params . id , res )
81+ if ( postId === null ) return
8182 try {
8283 const post = await prisma . post . delete ( {
8384 where : { id : postId } ,
@@ -88,7 +89,7 @@ postRouter.delete('/post/:id', async (req: Request, res: Response) => {
8889 error instanceof Prisma . PrismaClientKnownRequestError &&
8990 error . code === 'P2025'
9091 ) {
91- res . status ( 404 ) . json ( { error : `Post with ID ${ id } not found` } )
92+ res . status ( 404 ) . json ( { error : `Post with ID ${ req . params . id } not found` } )
9293 return
9394 }
9495 res . status ( 500 ) . json ( { error : 'Internal server error' } )
0 commit comments