17 lines
803 B
TypeScript
17 lines
803 B
TypeScript
import { toNoteDto } from "@/lib/notes";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { createNoteSchema } from "@/lib/validation";
|
|
|
|
export async function GET() {
|
|
const notes = await prisma.note.findMany({ orderBy: { createdAt: "desc" } });
|
|
return Response.json(notes.map(toNoteDto));
|
|
}
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const input = createNoteSchema.safeParse(await request.json());
|
|
if (!input.success) return Response.json({ error: input.error.issues[0]?.message ?? "Некорректные данные." }, { status: 400 });
|
|
const note = await prisma.note.create({ data: input.data });
|
|
return Response.json(toNoteDto(note), { status: 201 });
|
|
} catch { return Response.json({ error: "Некорректный JSON-запрос." }, { status: 400 }); }
|
|
}
|