mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
- /bookmarks route for viewing and adding bookmarks - Real-time duplicate URL detection as user types - Session-authenticated API endpoints for list/add/delete
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { createFileRoute } from "@tanstack/react-router"
|
|
import { eq, and } from "drizzle-orm"
|
|
import { getDb } from "@/db/connection"
|
|
import { bookmarks } from "@/db/schema"
|
|
import { getAuth } from "@/lib/auth"
|
|
|
|
const json = (data: unknown, status = 200) =>
|
|
new Response(JSON.stringify(data), {
|
|
status,
|
|
headers: { "content-type": "application/json" },
|
|
})
|
|
|
|
export const Route = createFileRoute("/api/bookmarks-delete")({
|
|
server: {
|
|
handlers: {
|
|
DELETE: async ({ request }) => {
|
|
try {
|
|
const auth = getAuth()
|
|
const session = await auth.api.getSession({ headers: request.headers })
|
|
if (!session?.user?.id) {
|
|
return json({ error: "Unauthorized" }, 401)
|
|
}
|
|
|
|
const url = new URL(request.url)
|
|
const bookmarkId = url.searchParams.get("id")
|
|
|
|
if (!bookmarkId) {
|
|
return json({ error: "Bookmark ID is required" }, 400)
|
|
}
|
|
|
|
const db = getDb(process.env.DATABASE_URL!)
|
|
|
|
// Delete only if it belongs to the user
|
|
const [deleted] = await db
|
|
.delete(bookmarks)
|
|
.where(
|
|
and(
|
|
eq(bookmarks.id, bookmarkId),
|
|
eq(bookmarks.user_id, session.user.id)
|
|
)
|
|
)
|
|
.returning()
|
|
|
|
if (!deleted) {
|
|
return json({ error: "Bookmark not found" }, 404)
|
|
}
|
|
|
|
return json({ success: true })
|
|
} catch (error) {
|
|
console.error("Error deleting bookmark:", error)
|
|
return json({ error: "Failed to delete bookmark" }, 500)
|
|
}
|
|
},
|
|
},
|
|
},
|
|
})
|