InPactAI icon indicating copy to clipboard operation
InPactAI copied to clipboard

feat: Add API Pagination to Backend List Endpoints

Open sharma-sugurthi opened this issue 9 months ago β€’ 3 comments

Summary

This PR implements pagination support for all backend list endpoints to prevent performance degradation as the database grows.

Changes Made

  • New file: Backend/app/utils/pagination.py - Pagination utility module with parameter validation and response formatting
  • Updated: Backend/app/routes/post.py - Added pagination to all 7 GET endpoints:
    • GET /users/
    • GET /audience-insights/
    • GET /sponsorships/
    • GET /posts/
    • GET /sponsorship-applications/
    • GET /sponsorship-payments/
    • GET /collaborations/

Implementation Details

Query Parameters

All list endpoints now accept:

  • skip (int, default=0, min=0): Number of records to skip (offset)
  • limit (int, default=50, min=1, max=100): Maximum records to return per page

Response Format

Returns paginated response with metadata:

{
  "data": [...],
  "skip": 0,
  "limit": 50,
  "count": 50
}

Summary by CodeRabbit

  • New Features
    • Added query-based pagination to all GET endpoints: use skip and limit to navigate large result sets.
    • Default behavior: skip defaults to 0, limit defaults to 50, and limit is capped (max 100).
    • Paginated responses include metadata: returned data, skip, limit, count (returned items), and total_count.

sharma-sugurthi avatar Mar 01 '26 12:03 sharma-sugurthi

[!WARNING]

Rate limit exceeded

@sharma-sugurthi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 13 minutes and 24 seconds before requesting another review.

βŒ› How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between d0d260e6ddf7fb1595d5ffaf4ef021a9fdb0cc88 and d4840c9748c8df22bd8fb66e75c4fc9e6be6feb8.

πŸ“’ Files selected for processing (1)
  • Backend/app/routes/post.py
πŸ“ Walkthrough

Walkthrough

Adds query-based pagination to seven GET endpoints in Backend/app/routes/post.py and introduces Backend/app/utils/pagination.py with helpers to validate skip/limit and format paginated responses; endpoints now use range-based Supabase queries and return total counts.

Changes

Cohort / File(s) Summary
Pagination Utilities
Backend/app/utils/pagination.py
New module defining DEFAULT_SKIP, DEFAULT_LIMIT, MAX_LIMIT, plus get_pagination_params(skip, limit) and paginate_query(query_result, skip, limit, total_count) for sanitizing params and formatting paginated responses.
Paginated GET Endpoints
Backend/app/routes/post.py
Updated seven GET routes to accept skip: int = Query(...) and limit: int = Query(...), use get_pagination_params and paginate_query, perform .order("id") and .range(start, end) Supabase queries, and include total_count in responses.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant API as FastAPI (post routes)
  participant Pager as Pagination Utils
  participant DB as Supabase

  Client->>API: GET /posts?skip=50&limit=25
  API->>Pager: get_pagination_params(skip, limit)
  Pager-->>API: (start, end, sanitized_skip, sanitized_limit)
  API->>DB: query .order("id").range(start, end)
  DB-->>API: paged rows
  API->>DB: query count
  DB-->>API: total_count
  API->>Pager: paginate_query(query_result, skip, limit, total_count)
  Pager-->>API: {data, skip, limit, count, total_count}
  API-->>Client: 200 OK with paginated payload

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰
Hop, skip, and bound I go,
Counting rows in tidy flow,
Limit set and ranges made,
Pages fetch without a braid,
Data served in gentle rows.

πŸš₯ Pre-merge checks | βœ… 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (2 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title 'feat: Add API Pagination to Backend List Endpoints' directly and clearly describes the main change: implementing pagination for list endpoints.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Mar 01 '26 12:03 coderabbitai[bot]

Please review @Zahnentferner

sharma-sugurthi avatar Mar 01 '26 12:03 sharma-sugurthi

also fixed some issues that pointed from the review of bot!!!

sharma-sugurthi avatar Mar 01 '26 13:03 sharma-sugurthi