import { sqlite } from "https://esm.town/v/std/sqlite";
import { blob } from "https://esm.town/v/std/blob";
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
}
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;