implement bookmark delete
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
import { type User, DEMO_USER } from "@markone/core/user"
|
||||
import { DEMO_USER, type User } from "@markone/core/user"
|
||||
import { ulid } from "ulid"
|
||||
import { db } from "~/database.ts"
|
||||
import { DEMO_BOOKMARKS } from "~/bookmark/demo-bookmarks.ts"
|
||||
|
||||
interface UserWithPassword extends User {
|
||||
password: string
|
||||
}
|
||||
|
||||
const findUserByIdQuery = db.query("SELECT id, username FROM users WHERE id = $userId")
|
||||
|
||||
const findUserByUsernameQuery = db.query("SELECT id, username FROM users WHERE username = $username")
|
||||
const findUserByUsernameWithPwQuery = db.query("SELECT id, username, password FROM users WHERE username = $username")
|
||||
|
||||
const createUserQuery = db.query("INSERT INTO users(id, username, password) VALUES ($id, $username, $password)")
|
||||
|
||||
function findUserByUsername(username: string, opts: { password: true }): UserWithPassword | null
|
||||
function findUserByUsername(username: string, { password }: { password?: boolean }): User | UserWithPassword | null {
|
||||
const findUserByUsernameQuery = db.query("SELECT id, username FROM users WHERE username = $username")
|
||||
const findUserByUsernameWithPwQuery = db.query("SELECT id, username, password FROM users WHERE username = $username")
|
||||
|
||||
const row = (password ? findUserByUsernameWithPwQuery : findUserByUsernameQuery).get({ username })
|
||||
if (!row) {
|
||||
return null
|
||||
@@ -23,6 +20,7 @@ function findUserByUsername(username: string, { password }: { password?: boolean
|
||||
}
|
||||
|
||||
function findUserById(userId: string): User | null {
|
||||
const findUserByIdQuery = db.query("SELECT id, username FROM users WHERE id = $userId")
|
||||
const row = findUserByIdQuery.get({ userId })
|
||||
if (!row) {
|
||||
return null
|
||||
@@ -31,6 +29,7 @@ function findUserById(userId: string): User | null {
|
||||
}
|
||||
|
||||
function createUser(username: string, password: string): User {
|
||||
const createUserQuery = db.query("INSERT INTO users(id, username, password) VALUES ($id, $username, $password)")
|
||||
const userId = ulid()
|
||||
createUserQuery.run({
|
||||
id: userId,
|
||||
@@ -43,19 +42,17 @@ function createUser(username: string, password: string): User {
|
||||
}
|
||||
}
|
||||
|
||||
async function createDemoUser() {
|
||||
const row = findUserByUsernameQuery.get({ username: DEMO_USER.username })
|
||||
if (row) {
|
||||
return
|
||||
}
|
||||
|
||||
async function createDemoUser(): Promise<User> {
|
||||
const createUserQuery = db.query(
|
||||
"INSERT OR REPLACE INTO users(id, username, password) VALUES ($id, $username, $password)",
|
||||
)
|
||||
const hashedPassword = await Bun.password.hash(DEMO_USER.unhashedPassword, "argon2id")
|
||||
|
||||
createUserQuery.run({
|
||||
id: DEMO_USER.id,
|
||||
username: DEMO_USER.username,
|
||||
password: hashedPassword,
|
||||
})
|
||||
return DEMO_USER
|
||||
}
|
||||
|
||||
export type { User }
|
||||
|
Reference in New Issue
Block a user