Public
Script
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
import { sqlite } from "https://esm.town/v/std/sqlite";
import { blob } from "https://esm.town/v/std/blob";
// Fetch user by username
export async function getUserByUsername(username: string) {
const userResult = await sqlite.execute({
sql: `SELECT id, name, bio, username, email, location, currently_listening, currently_reading_title,
currently_reading_author, currently_reading_cover_image, currently_watching_title,
currently_watching_platform, currently_watching_poster_image, profile_theme, updated_at, profile_img
FROM users WHERE username = ?`,
args: [username],
});
if (userResult.rows.length === 0) {
throw new Error("User not found");
}
const [id, name, bio, usernameRetrieved, email, location, currentlyListening, currentlyReadingTitle, currentlyReadingAuthor, currentlyReadingCoverImage, currentlyWatchingTitle, currentlyWatchingPlatform, currentlyWatchingPosterImage, profile_theme, updat
const linksResult = await sqlite.execute({
sql: `SELECT id, label, url FROM user_links WHERE user_id = ?`,
args: [id],
});
const links = linksResult.rows.map(([id, label, url]) => ({ id, label, url }));
return { id, name, bio, username: usernameRetrieved, email, location, currentlyListening, currentlyReading: { title: currentlyReadingTitle || '', author: currentlyReadingAuthor || '', coverImage: currentlyReadingCoverImage || '' }, currentlyWatching: { t
}
// Update user profile
export async function updateUser(userId, name, bio, location, currentlyListening, currentlyReading, currentlyWatching, profile_theme, profile_img) {
if (typeof userId !== "number") {
throw new Error("userId must be a number.");
}
if (typeof name !== "string" || typeof bio !== "string") {
throw new Error("name and bio must be strings.");
}
await sqlite.execute({
sql: `UPDATE users SET name = ?, bio = ?, location = ?, currently_listening = ?, currently_reading_title = ?,
currently_reading_author = ?, currently_reading_cover_image = ?, currently_watching_title = ?,
currently_watching_platform = ?, currently_watching_poster_image = ?, profile_theme = ?,
updated_at = CURRENT_TIMESTAMP WHERE id = ? profile_img = ?`,
args: [name, bio, location || null, currentlyListening || null, currentlyReading?.title || null, currentlyReading?.author || null, currentlyReading?.coverImage || null, currentlyWatching?.title || null, currentlyWatching?.platform || null, currentlyWat
});
}
export async function insertUserLink(userId: number, label: string, url: string) {
await sqlite.execute({
sql: `INSERT INTO user_links (user_id, label, url) VALUES (?, ?, ?)`,
args: [userId, label, url],
});
}
export async function getLinkById(linkId: number) {
const result = await sqlite.execute({
sql: `SELECT id, label, url, user_id FROM user_links WHERE id = ?`,
args: [linkId],
});
if (result.rows.length === 0) {
throw new Error("Link not found");
}
const [id, label, url, userId] = result.rows[0];
return { id, label, url, userId };
}
export async function updateUserLink(linkId: number, label: string, url: string) {
await sqlite.execute({
sql: `UPDATE user_links SET label = ?, url = ? WHERE id = ?`,
args: [label, url, linkId],
});
}
export async function deleteUserLink(linkId: number, userId: number) {
await sqlite.execute({
sql: `DELETE FROM user_links WHERE id = ? AND user_id = ?`,
args: [linkId, userId],
});
}
export async function getUserLinks(userId) {
if (typeof userId !== "number") {
throw new Error("userId must be a number.");
}
const result = await sqlite.execute({
sql: `SELECT id, label, url FROM user_links WHERE user_id = ?`,
args: [userId],
});
const links = result.rows.map(([id, label, url]) => ({
id,
label,
url,
}));
return links;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 23, 2024