// Public REST: rare furni trade values (website_rare_values). // AtomCMS JSON API parity — read-only catalog of rares with their credit / // currency values. Supports ?category= filter; paginated, ordered by name. import { asc, count, eq } from "drizzle-orm"; import { apiError, apiJson, pagination, positiveBigInt } from "@/lib/api"; import { db, WebsiteRareValues } from "@/lib/db"; export async function GET(req: Request) { const sp = new URL(req.url).searchParams; const { page, perPage, skip, take } = pagination(sp); // Optional ?category= filter (category_id is a BigInt). const categoryRaw = sp.get("category"); let categoryId: bigint | undefined; if (categoryRaw !== null) { const parsed = positiveBigInt(categoryRaw); if (!parsed) return apiError("A valid category id is required", 422); categoryId = parsed; } try { const where = categoryId !== undefined ? eq(WebsiteRareValues.categoryId, categoryId) : undefined; const [totalRows, data] = await Promise.all([ db.select({ total: count() }).from(WebsiteRareValues).where(where), db .select({ id: WebsiteRareValues.id, categoryId: WebsiteRareValues.categoryId, itemId: WebsiteRareValues.itemId, name: WebsiteRareValues.name, creditValue: WebsiteRareValues.creditValue, currencyValue: WebsiteRareValues.currencyValue, currencyType: WebsiteRareValues.currencyType, furnitureIcon: WebsiteRareValues.furnitureIcon, }) .from(WebsiteRareValues) .where(where) .orderBy(asc(WebsiteRareValues.name)) .limit(take) .offset(skip), ]); const total = totalRows[0]?.total ?? 0; return apiJson({ data, meta: { page, perPage, total, lastPage: Math.max(1, Math.ceil(total / perPage)), }, }); } catch { // DB unreachable — never 500; return an empty, well-formed payload. return apiJson( { data: [], meta: { page, perPage, total: 0, lastPage: 1 } }, { status: 200 }, ); } }