Skip to content

[Backend]: Add pagination, search, and filtering to the course listing API #43

Description

@Jidnyasa-P

Description

The /getallcourses endpoint currently returns every course from MongoDB in a single response.

As the course collection grows, this can increase response size, database load, and API latency.

Proposed solution

Add server-side query support for:

  • page
  • limit
  • search
  • category
  • educator
  • priceType
  • sort

Example:

GET /api/user/getallcourses?page=1&limit=12&search=javascript&category=programming&sort=newest

The response should include:

{
  "success": true,
  "data": [],
  "pagination": {
    "page": 1,
    "limit": 12,
    "totalItems": 0,
    "totalPages": 0,
    "hasNextPage": false,
    "hasPreviousPage": false
  }
}

Validation requirements

  • Default page to 1.
  • Default limit to a reasonable value such as 12.
  • Cap limit at 100.
  • Reject or safely normalize negative and malformed values.
  • Escape search input before constructing regular expressions.
  • Preserve the existing endpoint path.
  • Return an empty array rather than 404 when no courses match.

Acceptance criteria

  • Pagination works correctly.
  • Search supports course title and description.
  • Category filtering works.
  • Educator filtering works.
  • Free and paid filtering works.
  • Sorting works for newest, title, and enrollment count.
  • Pagination metadata is returned.
  • Oversized limits are capped.
  • Existing consumers remain compatible.
  • Backend tests cover filtering, sorting, and invalid query values.
  • No frontend changes are required.

Suggested files

backend/controllers/userControllers.js
backend/routers/userRoutes.js
backend/utils/pagination.js
backend/tests/course-listing.test.js

Metadata

Metadata

Assignees

Labels

ECSoC26Required label for a PR to be eligible for Sentinel scoringECSoC26-L1Easy difficulty, auto-assigned by Sentinel — 5 points

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions