feat: add recent data in top page
This commit is contained in:
+32
-15
@@ -4,29 +4,46 @@ import type { Content } from '$lib/article';
|
||||
import PG from '$lib/server/database';
|
||||
|
||||
let data: {
|
||||
recent: Content[],
|
||||
recent: {
|
||||
title: string,
|
||||
image: string,
|
||||
date: string,
|
||||
link: string,
|
||||
tags: string[]
|
||||
}[],
|
||||
tags: string[],
|
||||
updated: string
|
||||
updated: string,
|
||||
} = {
|
||||
recent: [
|
||||
{
|
||||
title: "title",
|
||||
image: "image",
|
||||
date: "date",
|
||||
link: "link",
|
||||
tags: ["tag1", "tag2"],
|
||||
},
|
||||
],
|
||||
tags: ["hoge", "fuga", "piyo"],
|
||||
recent: [],
|
||||
tags: [],
|
||||
updated: "",
|
||||
};
|
||||
|
||||
export const load: PageServerLoad = async ({ params }) => {
|
||||
const db = await PG();
|
||||
const now = await db.query('SELECT NOW()');
|
||||
await db.release();
|
||||
await db.begin();
|
||||
try {
|
||||
const recent_articles = await db.query(
|
||||
"SELECT * FROM article WHERE publish = 'public' ORDER BY updated_at DESC LIMIT 4"
|
||||
);
|
||||
data.recent = recent_articles.rows.map((row) => ({
|
||||
title: row.title,
|
||||
image: row.image,
|
||||
date: row.updated_at.toISOString().slice(0, 10),
|
||||
link: `/post/${row.category}/${row.id}`,
|
||||
tags: row.tags,
|
||||
}));
|
||||
const tags = await db.query("SELECT * FROM tag ORDER BY ref_count DESC");
|
||||
data.tags = tags.rows.map((row) => row.name + ":" + row.ref_count);
|
||||
} catch (e) {
|
||||
await db.rollback();
|
||||
throw error(500, e as Error);
|
||||
} finally {
|
||||
await db.release();
|
||||
}
|
||||
|
||||
data.updated = data.recent[0].date;
|
||||
|
||||
data.updated = now.rows[0].now;
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user