Skip to content

feat: add server-side search, filtering, and sorting to product listing - #465

Merged
Nsanjayboruds merged 3 commits into
Nsanjayboruds:mainfrom
bh462007:feature/424-product-search-filter-sort
Jul 16, 2026
Merged

feat: add server-side search, filtering, and sorting to product listing#465
Nsanjayboruds merged 3 commits into
Nsanjayboruds:mainfrom
bh462007:feature/424-product-search-filter-sort

Conversation

@bh462007

Copy link
Copy Markdown
Contributor

Fixes #424.

This PR adds server-side filtering, keyword search, and sorting to GET /api/product/list while preserving the existing pagination behavior.

Changes

backend/controller/productController.js (listProducts())

  • Added optional query parameters:
    • category
    • subCategory
    • minPrice
    • maxPrice
    • search
    • sort
  • Builds the MongoDB query dynamically so multiple filters can be combined.
  • Added case-insensitive product name search using $regex.
  • Added sorting options:
    • price_asc
    • price_desc
    • newest
  • Falls back to the existing insertion-order sorting when no valid sort option is provided.
  • Updated pagination metadata (total and pages) to reflect filtered results.
  • Existing requests without query parameters continue to behave exactly as before.

Example Requests

GET /api/product/list?category=Men&subCategory=Topwear&minPrice=500&maxPrice=2000&search=jacket&sort=price_asc&page=1&limit=20

GET /api/product/list?sort=price_desc

GET /api/product/list?search=jacket

Testing

Verified locally that:

  • ✅ Category and subcategory filtering return only matching products.
  • ✅ Price range filtering correctly includes and excludes products.
  • ✅ Search is case-insensitive.
  • ✅ Sorting works correctly for all supported options.
  • ✅ Multiple filters work together.
  • ✅ Pagination metadata reflects filtered results.
  • ✅ Existing pagination behavior remains unchanged when no filters are supplied.

Notes

  • Search currently uses a case-insensitive $regex. A MongoDB text index ($text) can be considered later for larger catalogs.
  • Database indexes for category, subCategory, and price are intentionally left as a follow-up optimization.

Branch Note

This branch is based on fix/415-index-startup-crash because main currently cannot start due to #415. Once that PR is merged, this branch can be rebased onto the updated main before merging.

Checklist

  • Supports category, subCategory, minPrice, maxPrice, search, and sort
  • Multiple filters work together
  • Pagination metadata reflects filtered results
  • Backward compatible when no query parameters are provided
  • Verified search, filtering, and sorting locally
  • Database indexes (follow-up improvement)

Copilot AI review requested due to automatic review settings July 12, 2026 16:10

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Nsanjayboruds

Copy link
Copy Markdown
Owner

@bh462007 fix the lint backend

@Nsanjayboruds
Nsanjayboruds merged commit 22d04bd into Nsanjayboruds:main Jul 16, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Add server-side product search, filtering, and sorting for product listing endpoint

3 participants