2025-12-08 16:31:30 +03:00

62 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { redis } from '@/lib/redis'
import { getPollById } from '@/lib/polls'
export async function GET(req: NextRequest) {
try {
const { searchParams } = new URL(req.url)
const fid = searchParams.get('fid')
if (!fid) return NextResponse.json({ bookmarks: [] })
const bookmarkIds = await redis.smembers(`bookmarks:${fid}`)
const bookmarks = []
for (const pollId of bookmarkIds || []) {
const poll = await getPollById(pollId)
if (poll) bookmarks.push(poll)
}
return NextResponse.json({ bookmarks })
} catch (error) {
console.error('Bookmarks GET error:', error)
return NextResponse.json({ bookmarks: [] })
}
}
export async function POST(req: NextRequest) {
try {
const { fid, pollId } = await req.json()
if (!fid || !pollId) {
return NextResponse.json({ error: 'FID and pollId required' }, { status: 400 })
}
await redis.sadd(`bookmarks:${fid}`, pollId)
return NextResponse.json({ success: true })
} catch (error) {
console.error('Bookmark POST error:', error)
return NextResponse.json({ error: 'Failed to bookmark' }, { status: 500 })
}
}
export async function DELETE(req: NextRequest) {
try {
const { searchParams } = new URL(req.url)
const fid = searchParams.get('fid')
const pollId = searchParams.get('pollId')
if (!fid || !pollId) {
return NextResponse.json({ error: 'FID and pollId required' }, { status: 400 })
}
await redis.srem(`bookmarks:${fid}`, pollId)
return NextResponse.json({ success: true })
} catch (error) {
console.error('Bookmark DELETE error:', error)
return NextResponse.json({ error: 'Failed to remove bookmark' }, { status: 500 })
}
}