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,81 +1,46 @@
|
||||
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'
|
||||
|
||||
// GET /api/highlights?locale=en - Get all highlights for user
|
||||
// POST /api/highlights?locale=en - Create new highlight
|
||||
export async function GET(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 { verseId, color } = body
|
||||
|
||||
if (!verseId || !['yellow', 'orange', 'pink', 'blue'].includes(color)) {
|
||||
return NextResponse.json({ error: 'Invalid input' }, { status: 400 })
|
||||
}
|
||||
|
||||
const highlights = await prisma.highlight.findMany({
|
||||
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({
|
||||
const highlight = await prisma.userHighlight.create({
|
||||
data: {
|
||||
userId: decoded.userId,
|
||||
userId,
|
||||
verseId,
|
||||
color,
|
||||
note,
|
||||
tags: tags || []
|
||||
createdAt: new Date(),
|
||||
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) {
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user