Download src/app/api/settings/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/settings/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/settings/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/settings/route.ts
2.48 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { prisma } from "@/lib/db"; | |
| import { withApiError } from "@/lib/api-error"; | |
| // GET /api/settings — return current user settings | |
| export const GET = withApiError(async () => { | |
| const user = await prisma.user.findFirst({ where: { id: 1 } }); | |
| if (!user) { | |
| return NextResponse.json({ error: "No user found" }, { status: 404 }); | |
| } | |
| return NextResponse.json({ | |
| name: user.name, | |
| dailyGoalXp: user.dailyGoalXp, | |
| lichessUsername: user.lichessUsername ?? "", | |
| chesscomUsername: user.chesscomUsername ?? "", | |
| }); | |
| }); | |
| // PATCH /api/settings — update user settings | |
| export const PATCH = withApiError(async (req: NextRequest) => { | |
| let body: unknown; | |
| try { | |
| body = await req.json(); | |
| } catch { | |
| return NextResponse.json({ error: "Invalid JSON" }, { status: 400 }); | |
| } | |
| const { | |
| name, | |
| dailyGoalXp, | |
| lichessUsername, | |
| chesscomUsername, | |
| } = body as { | |
| name?: string; | |
| dailyGoalXp?: number; | |
| lichessUsername?: string; | |
| chesscomUsername?: string; | |
| }; | |
| // Validate | |
| const updates: Record<string, unknown> = {}; | |
| if (name !== undefined) { | |
| const trimmed = String(name).trim(); | |
| if (trimmed.length === 0 || trimmed.length > 50) { | |
| return NextResponse.json( | |
| { error: "Name must be 1–50 characters" }, | |
| { status: 400 }, | |
| ); | |
| } | |
| updates.name = trimmed; | |
| } | |
| if (dailyGoalXp !== undefined) { | |
| const valid = [50, 100, 150, 200, 300]; | |
| if (!valid.includes(Number(dailyGoalXp))) { | |
| return NextResponse.json( | |
| { error: `dailyGoalXp must be one of ${valid.join(", ")}` }, | |
| { status: 400 }, | |
| ); | |
| } | |
| updates.dailyGoalXp = Number(dailyGoalXp); | |
| } | |
| if (lichessUsername !== undefined) { | |
| const trimmed = String(lichessUsername).trim(); | |
| updates.lichessUsername = trimmed.length > 0 ? trimmed : null; | |
| } | |
| if (chesscomUsername !== undefined) { | |
| const trimmed = String(chesscomUsername).trim(); | |
| updates.chesscomUsername = trimmed.length > 0 ? trimmed : null; | |
| } | |
| if (Object.keys(updates).length === 0) { | |
| return NextResponse.json({ error: "No valid fields to update" }, { status: 400 }); | |
| } | |
| const user = await prisma.user.update({ | |
| where: { id: 1 }, | |
| data: updates, | |
| }); | |
| return NextResponse.json({ | |
| name: user.name, | |
| dailyGoalXp: user.dailyGoalXp, | |
| lichessUsername: user.lichessUsername ?? "", | |
| chesscomUsername: user.chesscomUsername ?? "", | |
| }); | |
| }); | |