33 lines
893 B
TypeScript
33 lines
893 B
TypeScript
import { type } from "arktype"
|
|
import { db } from "~/database.ts"
|
|
import { HttpError } from "~/error.ts"
|
|
import type { User } from "~/user/user.ts"
|
|
|
|
const BOOKMARK_PAGINATION_LIMIT = 100
|
|
|
|
const ListUserBookmarksParams = type({
|
|
limit: ["number", "=", BOOKMARK_PAGINATION_LIMIT],
|
|
skip: ["number", "=", 5],
|
|
})
|
|
|
|
const listBookmarksQuery = db.query(
|
|
"SELECT id, kind, title, url FROM bookmarks WHERE user_id = $userId LIMIT $limit OFFSET $skip",
|
|
)
|
|
|
|
async function listUserBookmarks(request: Bun.BunRequest<"/api/bookmarks">, user: User) {
|
|
const queryParams = ListUserBookmarksParams(request.params)
|
|
if (queryParams instanceof type.errors) {
|
|
throw new HttpError(400, queryParams.summary)
|
|
}
|
|
|
|
const results = listBookmarksQuery.all({
|
|
userId: user.id,
|
|
limit: queryParams.limit,
|
|
skip: queryParams.skip,
|
|
})
|
|
|
|
return Response.json(results, { status: 200 })
|
|
}
|
|
|
|
export { listUserBookmarks }
|