@@ -51,12 +51,34 @@ router.post(
51
51
52
52
// Retrieve all questions
53
53
router . get ( "/all" , async ( req : Request , res : Response ) => {
54
+ const pagination = parseInt ( req . body . pagination as string , 10 ) || 1 ; // Default page is 1
55
+ const page_size = parseInt ( req . body . page_size as string , 10 ) || 10 ; // Default limit is 10
56
+ const skip = ( pagination - 1 ) * page_size ; // Calculate how many documents to skip
54
57
try {
55
- const questions = await Question . find ( )
58
+ const questions = await Question . find (
59
+ { } ,
60
+ {
61
+ questionid : 1 ,
62
+ title : 1 ,
63
+ description : 1 ,
64
+ complexity : 1 ,
65
+ category : 1 ,
66
+ }
67
+ )
56
68
. lean ( )
57
69
. sort ( { questionid : "ascending" } )
70
+ . skip ( skip )
71
+ . limit ( page_size )
58
72
. exec ( ) ;
59
- return res . json ( questions ) ;
73
+
74
+ const total = await Question . countDocuments ( ) . exec ( ) ;
75
+
76
+ return res . json ( {
77
+ questions,
78
+ currentPage : pagination ,
79
+ totalPages : Math . ceil ( total / page_size ) ,
80
+ totalQuestions : total ,
81
+ } ) ;
60
82
} catch ( error ) {
61
83
return res . status ( 500 ) . send ( "Internal server error" ) ;
62
84
}
@@ -70,7 +92,10 @@ router.get("/:id", [...idValidators], async (req: Request, res: Response) => {
70
92
}
71
93
const questionId = parseInt ( req . params . id , 10 ) ;
72
94
try {
73
- const question = await Question . findOne ( { questionid : questionId } ) . exec ( ) ;
95
+ const question = await Question . findOne (
96
+ { questionid : questionId } ,
97
+ { questionid : 1 , title : 1 , description : 1 , complexity : 1 , category : 1 }
98
+ ) . exec ( ) ;
74
99
if ( ! question ) {
75
100
return res . status ( 404 ) . json ( { message : "Question not found" } ) ;
76
101
}
0 commit comments