-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpages.ts
More file actions
114 lines (102 loc) · 3 KB
/
pages.ts
File metadata and controls
114 lines (102 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Pages table operations
*/
import type { PageData } from "@wdprlib/parser";
import { getUserInfo, buildFullname } from "@wdmock/shared";
export interface PageApiData {
page_id: number;
title: string;
source: string;
is_locked?: boolean;
}
export async function findPage(
db: D1Database,
category: string,
name: string,
): Promise<PageApiData | null> {
return db
.prepare(
"SELECT page_id, title, source, is_locked FROM pages WHERE category = ? AND unix_name = ?",
)
.bind(category, name)
.first<PageApiData>();
}
export async function getAllPageSources(db: D1Database): Promise<Map<string, string>> {
const result = await db
.prepare("SELECT category, unix_name, source FROM pages")
.all<{ category: string; unix_name: string; source: string }>();
const map = new Map<string, string>();
for (const row of result.results || []) {
map.set(buildFullname(row.category, row.unix_name), row.source);
}
return map;
}
export async function createPage(
db: D1Database,
category: string,
name: string,
title: string,
source: string,
): Promise<number> {
const result = await db
.prepare(
`INSERT INTO pages (site_id, category, unix_name, title, source, owner_user_id)
VALUES (1, ?, ?, ?, ?, 2)`,
)
.bind(category, name, title, source)
.run();
return result.meta.last_row_id as number;
}
export async function updatePage(
db: D1Database,
pageId: number,
title: string,
source: string,
): Promise<void> {
await db
.prepare(
`UPDATE pages SET title = ?, source = ?, date_last_edited = datetime('now')
WHERE page_id = ?`,
)
.bind(title, source, pageId)
.run();
}
export async function deletePage(db: D1Database, pageId: number): Promise<void> {
await db.batch([
db.prepare("DELETE FROM page_tags WHERE page_id = ?").bind(pageId),
db.prepare("DELETE FROM page_rate_vote WHERE page_id = ?").bind(pageId),
db.prepare("DELETE FROM pages WHERE page_id = ?").bind(pageId),
]);
}
export async function rowToPageData(
db: D1Database,
row: Record<string, unknown>,
): Promise<PageData> {
const pageId = row.page_id as number;
const tagsResult = await db
.prepare("SELECT tag FROM page_tags WHERE page_id = ?")
.bind(pageId)
.all();
const tags = (tagsResult.results || []).map((r) => r.tag as string);
const category = row.category as string;
const unixName = row.unix_name as string;
return {
name: unixName,
category,
fullname: buildFullname(category, unixName),
title: (row.title as string) || unixName,
createdAt: new Date(row.date_created as string),
createdBy: getUserInfo(row.owner_user_id as number),
updatedAt: new Date(row.date_last_edited as string),
updatedBy: getUserInfo(row.owner_user_id as number),
tags,
hiddenTags: [],
children: 0,
comments: 0,
size: ((row.source as string) || "").length,
rating: (row.rate as number) || 0,
ratingVotes: 0,
revisions: 1,
content: (row.source as string) || undefined,
};
}