ls-deno/routes/note.tsx

39 lines
972 B
TypeScript
Raw Normal View History

2022-09-27 15:49:41 -05:00
import { Handlers, PageProps } from "$fresh/server.ts";
2022-09-29 20:22:30 -05:00
import { query } from "../db.ts";
2022-09-27 15:49:41 -05:00
interface Note {
id: string;
created_at: Date;
content: string;
}
export const handler: Handlers<Note[] | null> = {
async GET(request, context) {
console.debug({ request, context });
const result = await query("select * from notes");
if (result == null) throw "unable to fetch from database";
2022-09-29 20:22:30 -05:00
const notes = result.rows as Note[];
2022-09-27 15:49:41 -05:00
console.debug(notes);
return await context.render(notes);
},
};
export default function Page({ data }: PageProps<Note[] | null>) {
return (
<div>
2022-09-29 20:22:30 -05:00
<h1>List of Notes</h1>
<nav>
<a href="/" class="text-blue-500 p-2">Back to Index</a>
</nav>
<p>Create a note:</p>
<form action="./create" method="POST">
<textarea name="content"></textarea>
<button>Post</button>
</form>
2022-09-27 15:49:41 -05:00
<pre>
{JSON.stringify(data, null, 2)}
</pre>
</div>
);
}