feat: add backend API endpoints for highlights and cross-references
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,44 +1,73 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/db'
|
||||
import { verifyToken } from '@/lib/auth'
|
||||
import { getAuth } from '@clerk/nextjs/server'
|
||||
|
||||
// POST /api/highlights/bulk?locale=en - Get highlights for multiple verses
|
||||
export async function POST(req: NextRequest) {
|
||||
export const runtime = 'nodejs'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const authHeader = req.headers.get('authorization')
|
||||
if (!authHeader) {
|
||||
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
|
||||
const { userId } = await getAuth(request)
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const token = authHeader.replace('Bearer ', '')
|
||||
const decoded = await verifyToken(token)
|
||||
if (!decoded) {
|
||||
return NextResponse.json({ success: false, error: 'Invalid token' }, { status: 401 })
|
||||
const body = await request.json()
|
||||
const { highlights } = body
|
||||
|
||||
if (!Array.isArray(highlights)) {
|
||||
return NextResponse.json({ error: 'Invalid input' }, { status: 400 })
|
||||
}
|
||||
|
||||
const body = await req.json()
|
||||
const { verseIds } = body
|
||||
const synced = []
|
||||
const errors = []
|
||||
|
||||
if (!Array.isArray(verseIds)) {
|
||||
return NextResponse.json({ success: false, error: 'verseIds must be an array' }, { status: 400 })
|
||||
}
|
||||
for (const item of highlights) {
|
||||
try {
|
||||
const existing = await prisma.userHighlight.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
verseId: item.verseId
|
||||
}
|
||||
})
|
||||
|
||||
const highlights = await prisma.highlight.findMany({
|
||||
where: {
|
||||
userId: decoded.userId,
|
||||
verseId: { in: verseIds }
|
||||
if (existing) {
|
||||
await prisma.userHighlight.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
color: item.color,
|
||||
updatedAt: new Date()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
await prisma.userHighlight.create({
|
||||
data: {
|
||||
userId,
|
||||
verseId: item.verseId,
|
||||
color: item.color,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}
|
||||
})
|
||||
}
|
||||
synced.push(item.verseId)
|
||||
} catch (e) {
|
||||
errors.push({
|
||||
verseId: item.verseId,
|
||||
error: 'Failed to sync'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Convert array to object keyed by verseId for easier lookup
|
||||
const highlightsMap: { [key: string]: any } = {}
|
||||
highlights.forEach(highlight => {
|
||||
highlightsMap[highlight.verseId] = highlight
|
||||
return NextResponse.json({
|
||||
synced: synced.length,
|
||||
errors,
|
||||
serverTime: Date.now()
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, highlights: highlightsMap })
|
||||
} catch (error) {
|
||||
console.error('Error fetching highlights:', error)
|
||||
return NextResponse.json({ success: false, error: 'Failed to fetch highlights' }, { status: 500 })
|
||||
console.error('Error bulk syncing highlights:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to sync highlights' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user