API: Add GET /api/comparisons endpoint for public feed #1

Closed
opened 2026-04-26 08:15:01 +00:00 by TopherMayor · 0 comments
Owner

Summary

The Explore page currently uses hardcoded mock data. We need a REST endpoint that returns paginated public comparisons from the database.

Backend

The server action getPublicComparisons() already exists in src/app/actions/comparison.ts (line 102-114). Create a proper API route that:

  1. Create src/app/api/comparisons/route.ts

    • GET /api/comparisons?page=1&limit=20&search=&category=
    • Query comparisons table where isPublic=true AND status='completed'
    • Return paginated results with: id, title, summary, slug, tags, viewCount, createdAt, item names
    • Support search query param (ILIKE on title)
    • Support category filter (tags array overlap)
    • Join with comparisonItems to include item names in response
    • Include totalCount for pagination
  2. Create src/app/api/comparisons/[slug]/route.ts

    • GET /api/comparisons/:slug — already partially called by results page
    • Increment viewCount on each GET
    • Return full ComparisonData shape

Response Shape

{
  "comparisons": [{
    "id": "...",
    "title": "...",
    "summary": "...",
    "slug": "...",
    "tags": ["Tech"],
    "items": ["React", "Vue"],
    "viewCount": 1247,
    "createdAt": "2024-01-15T..."
  }],
  "total": 42,
  "page": 1,
  "limit": 20
}

Files

  • Create: src/app/api/comparisons/route.ts
  • Create: src/app/api/comparisons/[slug]/route.ts
  • Reference: src/app/actions/comparison.ts (existing queries to reuse)
  • Reference: src/lib/db/schema.ts (table schemas)

Acceptance Criteria

  • GET /api/comparisons returns paginated public completed comparisons
  • Search query filters by title (case-insensitive)
  • GET /api/comparisons/:slug returns full comparison with items
  • viewCount increments on slug GET
  • Proper error handling (404 for missing slug)
## Summary The Explore page currently uses hardcoded mock data. We need a REST endpoint that returns paginated public comparisons from the database. ## Backend The server action `getPublicComparisons()` already exists in `src/app/actions/comparison.ts` (line 102-114). Create a proper API route that: 1. **Create** `src/app/api/comparisons/route.ts` - `GET /api/comparisons?page=1&limit=20&search=&category=` - Query `comparisons` table where `isPublic=true AND status='completed'` - Return paginated results with: id, title, summary, slug, tags, viewCount, createdAt, item names - Support `search` query param (ILIKE on title) - Support `category` filter (tags array overlap) - Join with `comparisonItems` to include item names in response - Include `totalCount` for pagination 2. **Create** `src/app/api/comparisons/[slug]/route.ts` - `GET /api/comparisons/:slug` — already partially called by results page - Increment `viewCount` on each GET - Return full `ComparisonData` shape ## Response Shape ```json { "comparisons": [{ "id": "...", "title": "...", "summary": "...", "slug": "...", "tags": ["Tech"], "items": ["React", "Vue"], "viewCount": 1247, "createdAt": "2024-01-15T..." }], "total": 42, "page": 1, "limit": 20 } ``` ## Files - Create: `src/app/api/comparisons/route.ts` - Create: `src/app/api/comparisons/[slug]/route.ts` - Reference: `src/app/actions/comparison.ts` (existing queries to reuse) - Reference: `src/lib/db/schema.ts` (table schemas) ## Acceptance Criteria - [ ] GET /api/comparisons returns paginated public completed comparisons - [ ] Search query filters by title (case-insensitive) - [ ] GET /api/comparisons/:slug returns full comparison with items - [ ] viewCount increments on slug GET - [ ] Proper error handling (404 for missing slug)
TopherMayor added this to the v0.2 - Feed & Profile milestone 2026-04-26 08:15:01 +00:00
TopherMayor added the featurebackend labels 2026-04-26 08:15:01 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TopherMayor/comparaison#1