2025-05-07 15:47:08 +01:00
|
|
|
import { DEMO_USER } from "@markone/core/user"
|
2025-05-06 11:00:35 +01:00
|
|
|
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],
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2025-05-07 15:47:08 +01:00
|
|
|
const listBookmarksQuery = db.query(
|
2025-05-07 16:57:09 +01:00
|
|
|
`
|
|
|
|
SELECT bookmarks.id, bookmarks.kind, bookmarks.title, bookmarks.url, tags.name as tag FROM bookmarks
|
|
|
|
LEFT JOIN tags
|
|
|
|
ON bookmarks.id = tags.bookmark_id
|
|
|
|
WHERE bookmarks.user_id = $userId
|
|
|
|
ORDER BY bookmarks.id LIMIT $limit OFFSET $skip
|
|
|
|
`,
|
2025-05-07 15:47:08 +01:00
|
|
|
)
|
|
|
|
|
2025-05-06 11:00:35 +01:00
|
|
|
const results = listBookmarksQuery.all({
|
|
|
|
userId: user.id,
|
|
|
|
limit: queryParams.limit,
|
|
|
|
skip: queryParams.skip,
|
|
|
|
})
|
|
|
|
|
|
|
|
return Response.json(results, { status: 200 })
|
|
|
|
}
|
|
|
|
|
2025-05-07 15:47:08 +01:00
|
|
|
async function deleteUserBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user: User) {
|
|
|
|
console.log("askldjlskajdkl")
|
|
|
|
if (user.id !== DEMO_USER.id) {
|
|
|
|
const deleteBookmarkQuery = db.query("DELETE FROM bookmarks WHERE user_id = $userId AND id = $id")
|
|
|
|
const tx = db.transaction(() => {
|
|
|
|
deleteBookmarkQuery.run({ userId: user.id, id: request.params.id })
|
|
|
|
})
|
|
|
|
tx()
|
|
|
|
}
|
|
|
|
return Response.json(undefined, { status: 204 })
|
|
|
|
}
|
|
|
|
|
|
|
|
export { listUserBookmarks, deleteUserBookmark }
|