Skip to content

feat(games): add filtering and pagination to GET /games endpoint#13

Merged
Rex-8 merged 10 commits into
devfrom
sandy
Mar 14, 2026
Merged

feat(games): add filtering and pagination to GET /games endpoint#13
Rex-8 merged 10 commits into
devfrom
sandy

Conversation

@Thisissandy07
Copy link
Copy Markdown
Contributor

@Thisissandy07 Thisissandy07 commented Feb 20, 2026

GET/Games endpoint route


Summary

API endpoint route what allows active users to access games


Type

  • feat: new feature

  • refactor: code restructuring


Changes

-pagination added
-filtering of games by likes and date added



Checklist

  • Code follows project style( I've tried by best to make it match the format of the pre-existing profile and auth routes)
  • Commit messages follow Conventional Commits
  • No API keys or sensitive data in code

I have zero idea whether this works or not but iv'e tried to make it as accurate to the db template as possible. Will add filtering later
small edits
Updated the games route to include query parameters for pagination and filtering. Added zod validation. Tried to make it match the format of pre-existed profile and auth routes
@mebinthattil mebinthattil requested a review from Rex-8 February 20, 2026 18:32
@ashyune
Copy link
Copy Markdown
Member

ashyune commented Feb 21, 2026

@Thisissandy07 do try to follow conventional commits when commit-ing,
also in your PR instead of removing points you can mark them as checked like this - [x] or unchecked - [ ]

as for the code @Rex-8 will review soon

@Thisissandy07 Thisissandy07 changed the title Sandy feat(games): add filtering and pagination to GET /games endpoint Feb 22, 2026
@mebinthattil mebinthattil requested a review from Copilot February 23, 2026 17:48
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a new GET /games endpoint that allows authenticated users to retrieve a paginated list of active games with filtering capabilities.

Changes:

  • Added a new games route file with GET /games endpoint supporting pagination and filtering by search term, likes count, and creation date
  • Implemented Zod schema validation for query parameters (page, limit, search, maxLikes, createdAfter, createdBefore)
  • Added filtering logic for active games with support for title search and date range filtering

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/src/routes/game.ts Outdated

const whereClause = and(...conditions);

/*Data retrival*/
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after comment delimiter. Comment should be formatted as "/* Data retrieval */" to match standard formatting conventions. Also, there's a spelling error: "retrival" should be "retrieval".

Suggested change
/*Data retrival*/
/* Data retrieval */

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
Comment thread backend/src/routes/game.ts Outdated
/**
* GET /games
*
* gives all active games
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comment has inconsistent capitalization. It should start with a capital letter like other route documentation in the codebase. Change "gives all active games" to "Retrieves all active games" or "Returns all active games" to match the documentation style seen in auth.ts and profile.ts (e.g., "Initiates Google OAuth flow", "Handles Google OAuth callback", "Complete initial profile setup").

Suggested change
* gives all active games
* Retrieves all active games

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
* - createdAfter (YYYY-MM-DD)
* - createdBefore (YYYY-MM-DD)
*

Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation has an extra blank line at line 45 before the closing comment delimiter. This is inconsistent with the documentation style in other routes (see auth.ts and profile.ts) which don't have blank lines before the closing delimiter. Remove this blank line for consistency.

Suggested change

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
* - page
* - limit
* - search
* - minLikes
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comment mentions 'minLikes' as a query parameter, but this parameter is not defined in the Zod schema (line 19-26) and is not implemented in the filtering logic. Either add the minLikes parameter to the schema and implement the filtering logic, or remove it from the documentation.

Suggested change
* - minLikes

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
Comment thread backend/src/routes/game.ts Outdated
Comment on lines +87 to +90
const afterTimestamp = Math.floor(
new Date(createdAfter).getTime() / 1000,
);
conditions.push(gte(games.createdAt, afterTimestamp));
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an invalid date string is provided (e.g., "invalid-date"), new Date() will create an Invalid Date object, and getTime() will return NaN. Math.floor(NaN) will also return NaN, which will then be used in the database query and could cause unexpected behavior. The date validation should be improved in the schema (as noted in another comment), or you should add a check here to handle invalid dates and return an appropriate error message.

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
Comment on lines +94 to +97
const beforeTimestamp = Math.floor(
new Date(createdBefore).getTime() / 1000,
);
conditions.push(lte(games.createdAt, beforeTimestamp));
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an invalid date string is provided (e.g., "invalid-date"), new Date() will create an Invalid Date object, and getTime() will return NaN. Math.floor(NaN) will also return NaN, which will then be used in the database query and could cause unexpected behavior. The date validation should be improved in the schema (as noted in another comment), or you should add a check here to handle invalid dates and return an appropriate error message.

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
Comment on lines +126 to +131
const totalResult = await db
.select({ value: count() })
.from(games)
.where(whereClause);

const total = totalResult[0]?.value ?? 0;
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The total count query runs separately from the main data query, which means two database queries are executed for every request. For large datasets with complex filters, the count query could become a performance bottleneck. Consider adding database indexes on frequently filtered columns (isActive, countLikes, createdAt) to improve query performance. Additionally, for very large datasets, you might want to consider caching the total count or using cursor-based pagination instead of offset-based pagination.

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
Rex-8 and others added 4 commits February 24, 2026 14:46
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
zod date validation

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Rex-8
Copy link
Copy Markdown
Contributor

Rex-8 commented Mar 9, 2026

@Thisissandy07 Is there a reason for choosing offset pagination over cursor pagination.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +82 to +94
if (createdAfter) {
const afterTimestamp = Math.floor(
new Date(createdAfter).getTime() / 1000,
);
conditions.push(gte(games.createdAt, afterTimestamp));
}

if (createdBefore) {
const beforeTimestamp = Math.floor(
new Date(createdBefore).getTime() / 1000,
);
conditions.push(lte(games.createdAt, beforeTimestamp));
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createdAt is defined as an integer timestamp column with mode: "timestamp" in the schema (used elsewhere with new Date() values). For consistency and to avoid type/serialization issues, compare games.createdAt against Date objects instead of manually converting to epoch seconds. Also, with createdBefore as YYYY-MM-DD, using midnight with lte will exclude most of that day; consider filtering by < startOfNextDay(createdBefore) (or otherwise making the end date inclusive).

Copilot uses AI. Check for mistakes.
Comment on lines +149 to +150

export default gamesRoute;
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This route is exported but is not currently mounted anywhere in src/index.ts (only /auth and /profile are routed), so GET /games will never be reachable. Add it to the main app router (e.g., app.route("/games", gamesRoute)) to actually expose the endpoint.

Copilot uses AI. Check for mistakes.
Comment thread backend/src/routes/game.ts Outdated
Copy link
Copy Markdown

Copilot AI commented Mar 12, 2026

@Rex-8 I've opened a new pull request, #31, to work on those changes. Once the pull request is ready, I'll request review from you.

Thisissandy07 and others added 2 commits March 12, 2026 20:14
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

@Thisissandy07 Thisissandy07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added cursor based pagination instead of page based. Restructured comments to be more organized

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/src/routes/game.ts
Comment thread backend/src/routes/game.ts
Comment on lines +101 to +109
const hasNextPage = gamesList.length > limit;
const results = hasNextPage ? gamesList.slice(0, limit) : gamesList;
const nextCursor = hasNextPage ? results[results.length - 1].createdAt : null;

return c.json({
success: true,
limit,
nextCursor,
games: results,
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextCursor is returned as results[...].createdAt. If createdAt is a Date, the JSON response will contain an ISO string cursor, but the request schema currently expects a numeric cursor, so the next request will fail validation. Make the response and request cursor formats consistent.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try returning the timestamps as numbers instead of ISO strings.

Comment thread backend/src/routes/game.ts
Comment thread backend/src/routes/game.ts
Comment thread backend/src/routes/game.ts
@Rex-8 Rex-8 merged commit 0b0c3cc into dev Mar 14, 2026
4 checks passed
@Thisissandy07 Thisissandy07 deleted the sandy branch April 3, 2026 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants