feat(search): add api-backed filtering and sorting
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { mockPlayers, mockServices, mockShops } from "@/lib/mock"
|
||||
import { searchCatalog } from "@/lib/search/search-catalog"
|
||||
import type { SearchSort } from "@/lib/search/types"
|
||||
|
||||
export const dynamic = "force-dynamic"
|
||||
|
||||
const SEARCH_SORTS: ReadonlySet<SearchSort> = new Set([
|
||||
"composite",
|
||||
"rating",
|
||||
"orders",
|
||||
"price_asc",
|
||||
"price_desc",
|
||||
])
|
||||
|
||||
function numberParam(value: string | null, fallback: number) {
|
||||
if (value === null) return fallback
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed) ? parsed : fallback
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
|
||||
const q = searchParams.get("q") ?? undefined
|
||||
const selectedGames = searchParams.getAll("game")
|
||||
const min = searchParams.get("min") ?? ""
|
||||
const max = searchParams.get("max") ?? ""
|
||||
const onlyOnline = (searchParams.get("online") ?? "0") === "1"
|
||||
const minRating = searchParams.get("minRating") ?? "0"
|
||||
|
||||
const sortParam = (searchParams.get("sort") ?? "composite") as SearchSort
|
||||
const sort: SearchSort = SEARCH_SORTS.has(sortParam) ? sortParam : "composite"
|
||||
|
||||
const limit = numberParam(searchParams.get("limit"), 12)
|
||||
const offset = numberParam(searchParams.get("offset"), 0)
|
||||
|
||||
const response = searchCatalog(
|
||||
{
|
||||
q,
|
||||
selectedGames,
|
||||
min,
|
||||
max,
|
||||
onlyOnline,
|
||||
minRating,
|
||||
sort,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
{
|
||||
players: mockPlayers,
|
||||
shops: mockShops,
|
||||
services: mockServices,
|
||||
},
|
||||
)
|
||||
|
||||
return NextResponse.json(response, {
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user