Files
famapp/src/app/api/v1/notes/[id]/route.ts
T

33 lines
1.3 KiB
TypeScript

import { apiJson, withApiHandler } from "@/lib/api-handler";
import { deleteNoteForScope, updateNoteForScope } from "@/modules/notes/server/actions";
import { updateNoteInput } from "@/modules/notes/server/schemas";
import { getNoteForScope } from "@/modules/notes/server/queries";
import { z } from "zod";
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
return withApiHandler(request, async (scope) => {
const note = await getNoteForScope(scope.householdId, id);
return apiJson(note);
});
}
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
return withApiHandler(request, async (scope, req) => {
const body: unknown = await req.json();
const parsed = updateNoteInput.omit({ id: true }).parse(body);
const note = await updateNoteForScope(scope, { id, ...parsed });
return apiJson(note);
});
}
export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
return withApiHandler(request, async (scope) => {
z.string().uuid().parse(id);
await deleteNoteForScope(scope, { id });
return apiJson({ ok: true });
});
}