Skip to content

Implement Sorting for Products API #98

@Alexandrbig1

Description

@Alexandrbig1

Priority: Medium
Difficulty: Advanced
Type: Backend Feature


Description

Enhance the Products API to support sorting via query parameters.
Currently, sorting is done in the frontend. This task adds backend-side sorting for scalability and performance.


Requirements

  1. Endpoint Update

    • Update GET /api/products to accept a sort query parameter.
    • Supported values:
      • best_selling (default).
      • a_z (alphabetical ascending).
      • z_a (alphabetical descending).
      • price_asc (low to high).
      • price_desc (high to low).
      • rating_asc.
      • rating_desc.
  2. Database Query

    • Use Mongoose’s .sort() to return products in the requested order.
    • Ensure products are still filterable by category/goals/etc.
  3. Error Handling

    • If invalid sort parameter is provided, fallback to default (best_selling).

Acceptance Criteria

  • GET /api/products?sort=price_asc returns products sorted by ascending price.
  • All 7 options are supported.
  • Invalid options gracefully fallback.

Metadata

Metadata

Assignees

Labels

advancedComplex/advanced tasks or featuresapiAPI endpoints & integrationbackendIssues related to backendhacktoberfestSpecial issue for Hacktoberfesthacktoberfest-2025Special issue for Hacktoberfest 2025mongodbMongoDB & database managementpriority: mediumImportant but not urgent

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions