-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add pagination support to GET /api/orders #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,33 +102,52 @@ const server = createServer(async (req, res) => { | |
| } | ||
|
|
||
| if (method === 'GET' && path === '/api/orders') { | ||
| const spotId = parsedUrl.searchParams.get('spotId'); | ||
| const userId = parsedUrl.searchParams.get('userId'); | ||
| const spotId = parsedUrl.searchParams.get('spotId'); | ||
| const userId = parsedUrl.searchParams.get('userId'); | ||
|
|
||
| const orders = database.getOrders({ spotId, userId }); | ||
| sendJson(res, 200, orders); | ||
| return; | ||
| const limitParam = parsedUrl.searchParams.get('limit'); | ||
| const offsetParam = parsedUrl.searchParams.get('offset'); | ||
|
|
||
| let limit = 10; // default page size | ||
| let offset = 0; // default start | ||
|
|
||
| // Validate pagination params if provided | ||
| if (limitParam !== null) { | ||
| limit = Number(limitParam); | ||
| if (!Number.isInteger(limit) || limit <= 0) { | ||
| sendJson(res, 400, { error: 'Invalid limit parameter' }); | ||
| return; | ||
| } | ||
| } | ||
| if (method === 'GET' && path.startsWith('/api/orders/')) { | ||
| console.log("I AM INSIDE ORDER ID ROUTE"); | ||
| const orderId = path.replace('/api/orders/', ''); | ||
|
|
||
|
|
||
| const authHeader = req.headers.authorization; | ||
| if (!authHeader) { | ||
| sendJson(res, 401, { error: 'Unauthorized' }); | ||
| return; | ||
|
|
||
| if (offsetParam !== null) { | ||
| offset = Number(offsetParam); | ||
| if (!Number.isInteger(offset) || offset < 0) { | ||
| sendJson(res, 400, { error: 'Invalid offset parameter' }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const orders = database.getOrders({}); | ||
| const order = orders.find(o => o.id === orderId); | ||
| // Get full filtered result first | ||
| const allOrders = database.getOrders({ spotId, userId }); | ||
|
|
||
| if (!order) { | ||
| sendJson(res, 404, { error: `Order not found: ${orderId}` }); | ||
| return; | ||
| } | ||
| const total = allOrders.length; | ||
|
|
||
| // Apply pagination | ||
| const paginatedOrders = allOrders.slice(offset, offset + limit); | ||
|
|
||
|
Comment on lines
+131
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move pagination to the database layer to avoid full fetches. Line 132 fetches all filtered orders, then Line 137 slices in memory. Per Suggested direction- const allOrders = database.getOrders({ spotId, userId });
- const total = allOrders.length;
- const paginatedOrders = allOrders.slice(offset, offset + limit);
+ const { data: paginatedOrders, total } = database.getOrders({
+ spotId,
+ userId,
+ limit,
+ offset
+ });Then update
🤖 Prompt for AI Agents |
||
| const hasMore = offset + limit < total; | ||
|
|
||
| sendJson(res, 200, { | ||
| data: paginatedOrders, | ||
| meta: { | ||
| total, | ||
| limit, | ||
| offset, | ||
| hasMore | ||
| } | ||
| }); | ||
|
|
||
| sendJson(res, 200, order); | ||
| return; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject empty/whitespace
offsetas malformed.Line 124 uses
Number(offsetParam), so?offset=and?offset=are coerced to0and currently pass. That conflicts with the “malformed values return 400” behavior.Suggested fix
if (offsetParam !== null) { - offset = Number(offsetParam); - if (!Number.isInteger(offset) || offset < 0) { + if (!/^\d+$/.test(offsetParam)) { + sendJson(res, 400, { error: 'Invalid offset parameter' }); + return; + } + offset = Number(offsetParam); + if (!Number.isInteger(offset) || offset < 0) { sendJson(res, 400, { error: 'Invalid offset parameter' }); return; } }📝 Committable suggestion
🤖 Prompt for AI Agents