File tree Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -3,13 +3,15 @@ import { searchQuestionsByTitleService } from '../services/get/index';
3
3
4
4
export const searchQuestionsByTitle = async ( req : Request , res : Response ) : Promise < Response > => {
5
5
const { title } = req . query ;
6
+ const page = parseInt ( req . query . page as string ) || 1 ;
7
+ const limit = parseInt ( req . query . limit as string ) || 10 ;
6
8
7
9
if ( ! title ) {
8
10
return res . status ( 400 ) . json ( { message : 'Title is required' } ) ;
9
11
}
10
12
11
13
try {
12
- const result = await searchQuestionsByTitleService ( title . toString ( ) ) ;
14
+ const result = await searchQuestionsByTitleService ( title . toString ( ) , page , limit ) ;
13
15
return res . status ( 200 ) . json ( result ) ;
14
16
} catch ( error ) {
15
17
return res . status ( 500 ) . json ( { success : false , message : 'An error occurred' , error } ) ;
Original file line number Diff line number Diff line change @@ -4,9 +4,14 @@ import { questions } from '../../lib/db/schema';
4
4
import { IGetQuestionsResponse } from '../get/types' ;
5
5
6
6
export const searchQuestionsByTitleService = async (
7
- title : string
7
+ title : string ,
8
+ page : number ,
9
+ limit : number
8
10
) : Promise < IGetQuestionsResponse > => {
9
11
const searchPattern = `%${ title } %` ;
12
+ const effectivePage = page ?? 1 ;
13
+ const effectiveLimit = limit ?? 10 ;
14
+ const offset = ( effectivePage - 1 ) * effectiveLimit ;
10
15
11
16
// Query the database for questions matching the title
12
17
const results = await db
@@ -17,7 +22,9 @@ export const searchQuestionsByTitleService = async (
17
22
topic : questions . topic ,
18
23
} )
19
24
. from ( questions )
20
- . where ( sql `${ questions . title } ILIKE ${ searchPattern } ` ) ; // Use ILIKE for case-insensitive matching
25
+ . where ( sql `${ questions . title } ILIKE ${ searchPattern } ` ) // Use ILIKE for case-insensitive matching
26
+ . limit ( effectiveLimit )
27
+ . offset ( offset ) ;
21
28
22
29
// Return the results as per IGetQuestionsResponse format
23
30
return {
You can’t perform that action at this time.
0 commit comments