@@ -13,36 +13,67 @@ import { authenticateUser } from "./auth";
1313export async function postRoutes ( fastify : FastifyInstance ) {
1414
1515 fastify . get (
16- "/threads/:id/posts" ,
17- { preHandler : authenticateUser } ,
18- async ( request , reply ) => {
19- const userId = request . userId ;
20- if ( ! userId ) {
21- return reply . status ( 401 ) . send ( { error : "Unauthorized" , success : false } ) ;
22- }
23- const user = await DrizzleClient . query . users . findFirst ( {
24- where : ( u , { eq } ) => eq ( u . id , userId ) ,
25- } ) ;
26- if ( ! user ) {
27- return reply . status ( 404 ) . send ( { error : "User not found" , success : false } ) ;
28- }
29- const params = threadIdParamsSchema . safeParse ( request . params ) ;
30- if ( ! params . success ) {
31- return reply . status ( 400 ) . send ( { success : false , error : "Invalid thread ID" } ) ;
32- }
33- const threadId = params . data . id ;
34- try {
35- const threadPosts = await DrizzleClient . query . posts . findMany ( {
36- where : ( p , { eq } ) => eq ( p . threadId , threadId ) ,
37- orderBy : ( p , { asc } ) => [ asc ( p . createdAt ) ] ,
38- } ) ;
39- return reply . status ( 200 ) . send ( { success : true , posts : threadPosts } ) ;
40- } catch ( error ) {
41- fastify . log . error ( "Error fetching posts for thread:" , error ) ;
42- return reply . status ( 500 ) . send ( { success : false , error : "Failed to fetch posts" } ) ;
43- }
44- }
45- ) ;
16+ "/threads/:id/posts" ,
17+ {
18+ preHandler : authenticateUser ,
19+ schema : {
20+ querystring : {
21+ type : "object" ,
22+ properties : {
23+ page : { type : "integer" , minimum : 1 , default : 1 } ,
24+ limit : { type : "integer" , minimum : 1 , maximum : 100 , default : 20 } ,
25+ } ,
26+ } ,
27+ } ,
28+ } ,
29+ async ( request , reply ) => {
30+ const userId = request . userId ;
31+ if ( ! userId ) {
32+ return reply
33+ . status ( 401 )
34+ . send ( { error : "Unauthorized" , success : false } ) ;
35+ }
36+ const user = await DrizzleClient . query . users . findFirst ( {
37+ where : ( u , { eq } ) => eq ( u . id , userId ) ,
38+ } ) ;
39+ if ( ! user ) {
40+ return reply
41+ . status ( 404 )
42+ . send ( { error : "User not found" , success : false } ) ;
43+ }
44+ const params = threadIdParamsSchema . safeParse ( request . params ) ;
45+ if ( ! params . success ) {
46+ return reply
47+ . status ( 400 )
48+ . send ( { success : false , error : "Invalid thread ID" } ) ;
49+ }
50+ const threadId = params . data . id ;
51+ const { page = 1 , limit = 20 } = request . query as { page ?: number ; limit ?: number } ;
52+ const offset = ( page - 1 ) * limit ;
53+ try {
54+ const threadPosts = await DrizzleClient . query . posts . findMany ( {
55+ where : ( p , { eq } ) => eq ( p . threadId , threadId ) ,
56+ orderBy : ( p , { asc } ) => [ asc ( p . createdAt ) ] ,
57+ limit : limit ,
58+ offset : offset ,
59+ } ) ;
60+ return reply . status ( 200 ) . send ( {
61+ success : true ,
62+ posts : threadPosts ,
63+ pagination : {
64+ page,
65+ limit,
66+ count : threadPosts . length ,
67+ } ,
68+ } ) ;
69+ } catch ( error ) {
70+ fastify . log . error ( "Error fetching posts for thread:" , error ) ;
71+ return reply
72+ . status ( 500 )
73+ . send ( { success : false , error : "Failed to fetch posts" } ) ;
74+ }
75+ }
76+ ) ;
4677
4778 fastify . get (
4879 "/posts/:id" ,
0 commit comments