@@ -12,50 +12,59 @@ import { authenticateUser } from "./auth";
1212export async function topicRoutes ( fastify : FastifyInstance ) {
1313
1414 fastify . get (
15- "/topics" ,
16- {
17- preHandler : authenticateUser ,
18- schema : {
19- querystring : {
20- type : "object" ,
21- properties : {
22- page : { type : "integer" , minimum : 1 , default : 1 } ,
23- limit : { type : "integer" , minimum : 1 , maximum : 100 , default : 10 } ,
24- } ,
25- } ,
26- } ,
27- } ,
28- async ( request : FastifyRequest < { Querystring : { page ?: number ; limit ?: number } } > , reply ) => {
29- const userId = request . userId ;
30- if ( ! userId )
31- return reply
32- . status ( 401 )
33- . send ( { success : false , error : "Unauthorized" } ) ;
34- const { page = 1 , limit = 10 } = request . query ;
35- const offset = ( page - 1 ) * limit ;
36- try {
37- const topics = await DrizzleClient . query . topics . findMany ( {
38- limit : limit ,
39- offset : offset ,
40- orderBy : ( table , { desc } ) => [ desc ( table . createdAt ) ] ,
41- } ) ;
42-
43- return reply . status ( 200 ) . send ( {
44- success : true ,
45- data : topics ,
46- pagination : {
47- page,
48- limit,
49- count : topics . length ,
50- } ,
51- } ) ;
52- } catch ( error ) {
53- fastify . log . error ( { err : error } , "Failed to fetch topics" ) ;
54- return reply
55- . status ( 500 )
56- . send ( { success : false , error : "Failed to fetch topics" } ) ;
57- }
58- }
15+ "/topics" ,
16+ {
17+ preHandler : authenticateUser ,
18+ schema : {
19+ querystring : {
20+ type : "object" ,
21+ properties : {
22+ page : { type : "integer" , minimum : 1 , default : 1 } ,
23+ limit : { type : "integer" , minimum : 1 , maximum : 100 , default : 10 } ,
24+ } ,
25+ } ,
26+ } ,
27+ } ,
28+ async (
29+ request : FastifyRequest < { Querystring : { page : number ; limit : number } } > ,
30+ reply
31+ ) => {
32+ const { page, limit } = request . query ;
33+ const offset = ( page - 1 ) * limit ;
34+ const userId = request . userId ;
35+ if ( ! userId )
36+ return reply
37+ . status ( 401 )
38+ . send ( { success : false , error : "Unauthorized" } ) ;
39+ const user = await DrizzleClient . query . users . findFirst ( {
40+ where : ( u , { eq } ) => eq ( u . id , userId ) ,
41+ } ) ;
42+ if ( ! user )
43+ return reply
44+ . status ( 404 )
45+ . send ( { success : false , error : "User not found" } ) ;
46+ try {
47+ const topics = await DrizzleClient . query . topics . findMany ( {
48+ limit : limit ,
49+ offset : offset ,
50+ orderBy : ( table , { desc } ) => [ desc ( table . createdAt ) ] ,
51+ } ) ;
52+ return reply . status ( 200 ) . send ( {
53+ success : true ,
54+ data : topics ,
55+ pagination : {
56+ page,
57+ limit,
58+ count : topics . length ,
59+ } ,
60+ } ) ;
61+ } catch ( error ) {
62+ fastify . log . error ( { err : error } , "Failed to fetch topics" ) ;
63+ return reply
64+ . status ( 500 )
65+ . send ( { success : false , error : "Failed to fetch topics" } ) ;
66+ }
67+ }
5968 ) ;
6069
6170 fastify . post (
0 commit comments