import { NextRequest, NextResponse } from "next/server"; import { getDb } from "@/lib/db"; export async function GET() { const db = getDb(); const members = await db.householdMember.findMany({ orderBy: { name: "asc" } }); return NextResponse.json(members); } export async function POST(req: NextRequest) { const body = await req.json().catch(() => ({})); const name = String(body.name ?? "").trim(); if (!name) return NextResponse.json({ errors: ["name"] }, { status: 400 }); if (name.length > 100) return NextResponse.json({ errors: ["name"] }, { status: 400 }); const db = getDb(); const existing = await db.householdMember.findUnique({ where: { name } }); if (existing) return NextResponse.json({ errors: ["name"] }, { status: 409 }); const created = await db.householdMember.create({ data: { name } }); return NextResponse.json(created, { status: 201 }); } export async function DELETE(req: NextRequest) { const id = Number(req.nextUrl.searchParams.get("id")); if (!Number.isInteger(id)) return NextResponse.json({ error: "not found" }, { status: 404 }); const db = getDb(); const existing = await db.householdMember.findUnique({ where: { id } }); if (!existing) return NextResponse.json({ error: "not found" }, { status: 404 }); await db.householdMember.delete({ where: { id } }); return NextResponse.json({ ok: true }); }