Skip to content

Commit 8a9c566

Browse files
committed
add pagination limit 10
1 parent ce9f16a commit 8a9c566

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

backend/question/src/controller/search-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { searchQuestionsByTitleService } from '../services/get/index';
33

44
export const searchQuestionsByTitle = async (req: Request, res: Response): Promise<Response> => {
55
const { title } = req.query;
6+
const page = parseInt(req.query.page as string) || 1;
7+
const limit = parseInt(req.query.limit as string) || 10;
68

79
if (!title) {
810
return res.status(400).json({ message: 'Title is required' });
911
}
1012

1113
try {
12-
const result = await searchQuestionsByTitleService(title.toString());
14+
const result = await searchQuestionsByTitleService(title.toString(), page, limit);
1315
return res.status(200).json(result);
1416
} catch (error) {
1517
return res.status(500).json({ success: false, message: 'An error occurred', error });

backend/question/src/services/get/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import { questions } from '../../lib/db/schema';
44
import { IGetQuestionsResponse } from '../get/types';
55

66
export const searchQuestionsByTitleService = async (
7-
title: string
7+
title: string,
8+
page: number,
9+
limit: number
810
): Promise<IGetQuestionsResponse> => {
911
const searchPattern = `%${title}%`;
12+
const effectivePage = page ?? 1;
13+
const effectiveLimit = limit ?? 10;
14+
const offset = (effectivePage - 1) * effectiveLimit;
1015

1116
// Query the database for questions matching the title
1217
const results = await db
@@ -17,7 +22,9 @@ export const searchQuestionsByTitleService = async (
1722
topic: questions.topic,
1823
})
1924
.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);
2128

2229
// Return the results as per IGetQuestionsResponse format
2330
return {

0 commit comments

Comments
 (0)