ls-deno/routes/note/create.tsx

30 lines
845 B
TypeScript
Raw Normal View History

2022-09-30 15:14:57 -05:00
import { Handlers, PageProps } from "$fresh/server.ts";
2022-10-01 14:03:15 -05:00
import { query } from "@/db.ts";
import { Page } from "@/components/Page.tsx";
2022-09-30 15:14:57 -05:00
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";
2022-10-01 14:03:15 -05:00
const result = await query<{ id: string }>(
2022-09-30 15:14:57 -05:00
"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>
);
}