Files
archived-linsa/packages/web/src/routes/api/bookmarks-delete.ts
Nikita d165605a68 Add bookmarks page with duplicate URL validation
- /bookmarks route for viewing and adding bookmarks
- Real-time duplicate URL detection as user types
- Session-authenticated API endpoints for list/add/delete
2025-12-31 19:56:32 +02:00

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)
}
},
},
},
})