26 lines
962 B
TypeScript
26 lines
962 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getDb } from "@/lib/db";
|
|
import { validateContact } from "@/lib/contacts";
|
|
|
|
export async function GET() {
|
|
const db = getDb();
|
|
const all = await db.contact.findMany({ orderBy: { name: "asc" } });
|
|
return NextResponse.json(all);
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const body = await req.json().catch(() => ({}));
|
|
const input = {
|
|
name: String(body.name ?? ""),
|
|
email: body.email ? String(body.email) : null,
|
|
telegramChatId: body.telegramChatId ? String(body.telegramChatId) : null,
|
|
};
|
|
const errs = validateContact(input);
|
|
if (errs.length) return NextResponse.json({ errors: errs }, { status: 400 });
|
|
const db = getDb();
|
|
const created = await db.contact.create({
|
|
data: { name: input.name.trim(), email: input.email?.trim() || null, telegramChatId: input.telegramChatId?.trim() || null },
|
|
});
|
|
return NextResponse.json(created, { status: 201 });
|
|
}
|