35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireSessionUser } from "@/lib/server/session";
|
|
import { createGroup, listGroups } from "@/lib/server/groups";
|
|
import { toErrorResponse } from "@/lib/server/errors";
|
|
import { getRequestMeta } from "@/lib/server/request";
|
|
|
|
export async function GET() {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const groups = await listGroups(user.id);
|
|
return NextResponse.json({ requestId, request_id: requestId, groups });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "GET /api/groups", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const body = await req.json().catch(() => null);
|
|
const name = String(body?.name || "").trim();
|
|
if (!name) return NextResponse.json({ requestId, request_id: requestId, error: { code: "MISSING_NAME", message: "Name is required" } }, { status: 400 });
|
|
|
|
const group = await createGroup(user.id, name);
|
|
return NextResponse.json({ requestId, request_id: requestId, group });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "POST /api/groups", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|
|
|