ls-deno/routes/note/create.tsx

30 lines
849 B
TypeScript

import { Handlers, PageProps } from "$fresh/server.ts";
import { query } from "@/db/mod.ts";
import { Page } from "@/components/Page.tsx";
type NoteID = string;
export const handler: Handlers<NoteID> = {
async POST(request, context) {
const content = (await request.formData()).get("content");
if (!content) throw "no content provided";
const result = await query<{ id: string }>(
"insert into note (content) values ($1) returning id",
[content],
);
if (!result) throw "insert failed";
const { rows: [{ id }] } = result;
return await context.render(id);
},
};
export default function NotesPage({ data: noteId }: PageProps<NoteID>) {
return (
<Page>
<h1>You created a note!</h1>
<a href="/note">Back to notes</a>
<a href={`/note/${noteId}`}>View your note</a>
</Page>
);
}