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:
2025-11-12 07:07:21 +00:00
parent ea2a848f73
commit 7ca2076ca8
4 changed files with 162 additions and 93 deletions

View File

@@ -0,0 +1,33 @@
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
export const runtime = 'nodejs'
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url)
const verseId = searchParams.get('verseId')
if (!verseId) {
return NextResponse.json(
{ error: 'verseId parameter required' },
{ status: 400 }
)
}
// For now, return empty cross-references
// TODO: Implement actual cross-reference lookup in Phase 2.1B
// This would require a cross_references table mapping verses to related verses
return NextResponse.json({
verseId,
references: []
})
} catch (error) {
console.error('Error fetching cross-references:', error)
return NextResponse.json(
{ error: 'Failed to fetch cross-references' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,42 @@
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getAuth } from '@clerk/nextjs/server'
export const runtime = 'nodejs'
export async function GET(request: Request) {
try {
const { userId } = await getAuth(request)
if (!userId) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const highlights = await prisma.userHighlight.findMany({
where: { userId },
select: {
id: true,
verseId: true,
color: true,
createdAt: true,
updatedAt: true
}
})
return NextResponse.json({
highlights: highlights.map(h => ({
id: h.id,
verseId: h.verseId,
color: h.color,
createdAt: h.createdAt.getTime(),
updatedAt: h.updatedAt.getTime()
})),
serverTime: Date.now()
})
} catch (error) {
console.error('Error fetching highlights:', error)
return NextResponse.json(
{ error: 'Failed to fetch highlights' },
{ status: 500 }
)
}
}

View File

@@ -1,44 +1,73 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' 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 const runtime = 'nodejs'
export async function POST(req: NextRequest) {
export async function POST(request: Request) {
try { try {
const authHeader = req.headers.get('authorization') const { userId } = await getAuth(request)
if (!authHeader) { if (!userId) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 }) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
} }
const token = authHeader.replace('Bearer ', '') const body = await request.json()
const decoded = await verifyToken(token) const { highlights } = body
if (!decoded) {
return NextResponse.json({ success: false, error: 'Invalid token' }, { status: 401 }) if (!Array.isArray(highlights)) {
return NextResponse.json({ error: 'Invalid input' }, { status: 400 })
} }
const body = await req.json() const synced = []
const { verseIds } = body const errors = []
if (!Array.isArray(verseIds)) { for (const item of highlights) {
return NextResponse.json({ success: false, error: 'verseIds must be an array' }, { status: 400 }) try {
} const existing = await prisma.userHighlight.findFirst({
where: {
userId,
verseId: item.verseId
}
})
const highlights = await prisma.highlight.findMany({ if (existing) {
where: { await prisma.userHighlight.update({
userId: decoded.userId, where: { id: existing.id },
verseId: { in: verseIds } 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 return NextResponse.json({
const highlightsMap: { [key: string]: any } = {} synced: synced.length,
highlights.forEach(highlight => { errors,
highlightsMap[highlight.verseId] = highlight serverTime: Date.now()
}) })
return NextResponse.json({ success: true, highlights: highlightsMap })
} catch (error) { } catch (error) {
console.error('Error fetching highlights:', error) console.error('Error bulk syncing highlights:', error)
return NextResponse.json({ success: false, error: 'Failed to fetch highlights' }, { status: 500 }) return NextResponse.json(
{ error: 'Failed to sync highlights' },
{ status: 500 }
)
} }
} }

View File

@@ -1,81 +1,46 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
import { verifyToken } from '@/lib/auth' import { getAuth } from '@clerk/nextjs/server'
// GET /api/highlights?locale=en - Get all highlights for user export const runtime = 'nodejs'
// POST /api/highlights?locale=en - Create new highlight
export async function GET(req: NextRequest) { export async function POST(request: Request) {
try { try {
const authHeader = req.headers.get('authorization') const { userId } = await getAuth(request)
if (!authHeader) { if (!userId) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 }) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
} }
const token = authHeader.replace('Bearer ', '') const body = await request.json()
const decoded = await verifyToken(token) const { verseId, color } = body
if (!decoded) {
return NextResponse.json({ success: false, error: 'Invalid token' }, { status: 401 }) if (!verseId || !['yellow', 'orange', 'pink', 'blue'].includes(color)) {
return NextResponse.json({ error: 'Invalid input' }, { status: 400 })
} }
const highlights = await prisma.highlight.findMany({ const highlight = await prisma.userHighlight.create({
where: { userId: decoded.userId },
orderBy: { createdAt: 'desc' }
})
return NextResponse.json({ success: true, highlights })
} catch (error) {
console.error('Error fetching highlights:', error)
return NextResponse.json({ success: false, error: 'Failed to fetch highlights' }, { status: 500 })
}
}
export async function POST(req: NextRequest) {
try {
const authHeader = req.headers.get('authorization')
if (!authHeader) {
return NextResponse.json({ success: false, 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 req.json()
const { verseId, color, note, tags } = body
if (!verseId || !color) {
return NextResponse.json({ success: false, error: 'Missing required fields' }, { status: 400 })
}
// Check if highlight already exists
const existingHighlight = await prisma.highlight.findUnique({
where: {
userId_verseId: {
userId: decoded.userId,
verseId
}
}
})
if (existingHighlight) {
return NextResponse.json({ success: false, error: 'Highlight already exists' }, { status: 400 })
}
const highlight = await prisma.highlight.create({
data: { data: {
userId: decoded.userId, userId,
verseId, verseId,
color, color,
note, createdAt: new Date(),
tags: tags || [] updatedAt: new Date()
} }
}) })
return NextResponse.json({ success: true, highlight }) return NextResponse.json({
id: highlight.id,
verseId: highlight.verseId,
color: highlight.color,
createdAt: highlight.createdAt.getTime(),
updatedAt: highlight.updatedAt.getTime(),
syncStatus: 'synced'
})
} catch (error) { } catch (error) {
console.error('Error creating highlight:', error) console.error('Error creating highlight:', error)
return NextResponse.json({ success: false, error: 'Failed to create highlight' }, { status: 500 }) return NextResponse.json(
{ error: 'Failed to create highlight' },
{ status: 500 }
)
} }
} }