stub front page works

This commit is contained in:
Moon Man 2023-12-31 12:03:17 -05:00
parent 31166bff3b
commit be53008a8c
3 changed files with 18 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import db from "./db.js";
import { z } from "zod";
import { fixDates } from "./util.js";
export const slugRegex = /^[a-zA-Z0-9_-]+$/;
@ -20,20 +21,22 @@ export const getBySlug = async (slug: string): Promise<Article | null> => {
return db<Article>("articles")
.where("slug", slug)
.first()
.then((rec) => !!rec ? rec : null)
.then((rec) => !!rec ? fixDates<Article>(rec) : null)
};
export const getById = async (articleId: number): Promise<Article | null> => {
return db<Article>("articles")
.where("id", articleId)
.first()
.then((rec) => !!rec ? rec : null)
.then((rec) => !!rec ? fixDates<Article>(rec) : null)
};
export const getByUserId = async (userId: number): Promise<Article[]> =>
db<Article>("articles")
.where("users_id", userId)
.orderBy("created_at", "desc");
.orderBy("created_at", "desc")
.then((articles) => articles.map((a) => fixDates<Article>(a)))
;
export const insert = async (userId: number, slug: string, title: string): Promise<Article> => {
const data: Record<string, any> = {
@ -58,4 +61,5 @@ export const getAll = async (count: number, lastId = Number.MAX_SAFE_INTEGER) =>
.where("id", "<", lastId)
.orderBy("created_at", "desc")
.limit(count)
.then((articles) => articles.map((a) => fixDates<Article>(a)))
;

View File

@ -32,7 +32,7 @@ app.get("/", async (req, res) => {
const renderedArticles = articles.map((a) => `
<li>
<a href="/${a.slug}.html">${escapeHtml(a.title)} - ${a.created_at.toDateString()}</a>
<a href="/${a.slug}.html">${escapeHtml(a.title)}</a> - ${a.created_at.toISOString()}
</li>
`).join("\n");
@ -45,6 +45,8 @@ app.get("/", async (req, res) => {
${renderedArticles}
</ul>
`;
res.set("Content-Type", "text/html").send(payload);
});
app.post(Routes.inbox, handleInboxPost);

View File

@ -10,3 +10,11 @@ export const streamToString = async (stream: ReadableStream<Uint8Array>) => {
const arr = new Uint8Array(await new Response(stream).arrayBuffer());
return new TextDecoder("utf-8").decode(arr);
};
export const fixDates = <T>(record: Record<string, any>, columns: string[] = ["created_at", "updated_at"]) => {
columns.forEach((column) => {
const value = record[column]
if (value && typeof value === "number") record[column] = new Date(record[column]);
});
return record as T;
};