Add both chapter-level and verse-level bookmarking capabilities: Database Changes: - Add ChapterBookmark table for chapter-level bookmarks - Update schema with proper relationships to User and BibleBook models - Maintain existing Bookmark table for verse-level bookmarks API Endpoints: - /api/bookmarks/chapter (GET, POST, DELETE) with check endpoint - /api/bookmarks/verse (GET, POST, DELETE) with check and bulk-check endpoints - JWT authentication required for all bookmark operations - Multilingual error messages (Romanian/English) Frontend Implementation: - Chapter bookmark button in Bible page header with visual state feedback - Individual verse bookmark icons with hover-to-reveal UI - Highlighted background for bookmarked verses - Efficient bulk checking for verse bookmarks per chapter - Real-time UI updates without page refresh 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
199 lines
5.5 KiB
TypeScript
199 lines
5.5 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { getUserFromToken } from '@/lib/auth'
|
|
import { prisma } from '@/lib/db'
|
|
|
|
export const runtime = 'nodejs'
|
|
|
|
function getErrorMessages(locale: string = 'ro') {
|
|
const messages = {
|
|
ro: {
|
|
unauthorized: 'Nu ești autentificat',
|
|
bookmarkRequired: 'Informațiile bookmark-ului sunt obligatorii',
|
|
bookmarkError: 'Eroare la salvarea bookmark-ului',
|
|
bookmarkRemoved: 'Bookmark șters cu succes',
|
|
bookmarkSaved: 'Bookmark salvat cu succes'
|
|
},
|
|
en: {
|
|
unauthorized: 'Unauthorized',
|
|
bookmarkRequired: 'Bookmark information is required',
|
|
bookmarkError: 'Error saving bookmark',
|
|
bookmarkRemoved: 'Bookmark removed successfully',
|
|
bookmarkSaved: 'Bookmark saved successfully'
|
|
}
|
|
}
|
|
return messages[locale as keyof typeof messages] || messages.ro
|
|
}
|
|
|
|
// GET - Get user's chapter bookmarks
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const url = new URL(request.url)
|
|
const locale = url.searchParams.get('locale') || 'ro'
|
|
const messages = getErrorMessages(locale)
|
|
|
|
// Get token from authorization header
|
|
const authHeader = request.headers.get('authorization')
|
|
const token = authHeader?.replace('Bearer ', '')
|
|
|
|
if (!token) {
|
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
|
}
|
|
|
|
// Verify token and get user
|
|
const user = await getUserFromToken(token)
|
|
if (!user) {
|
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
|
}
|
|
|
|
// Get user's chapter bookmarks with book information
|
|
const bookmarks = await prisma.chapterBookmark.findMany({
|
|
where: {
|
|
userId: user.id
|
|
},
|
|
include: {
|
|
book: {
|
|
include: {
|
|
version: true
|
|
}
|
|
}
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({ bookmarks })
|
|
|
|
} catch (error) {
|
|
console.error('Bookmark fetch error:', error)
|
|
const url = new URL(request.url)
|
|
const locale = url.searchParams.get('locale') || 'ro'
|
|
const messages = getErrorMessages(locale)
|
|
|
|
return NextResponse.json({ error: messages.bookmarkError }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
// POST - Create chapter bookmark
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const url = new URL(request.url)
|
|
const locale = url.searchParams.get('locale') || 'ro'
|
|
const messages = getErrorMessages(locale)
|
|
|
|
// Get token from authorization header
|
|
const authHeader = request.headers.get('authorization')
|
|
const token = authHeader?.replace('Bearer ', '')
|
|
|
|
if (!token) {
|
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
|
}
|
|
|
|
// Verify token and get user
|
|
const user = await getUserFromToken(token)
|
|
if (!user) {
|
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
|
}
|
|
|
|
// Parse request body
|
|
const { bookId, chapterNum, note } = await request.json()
|
|
|
|
// Validate input
|
|
if (!bookId || !chapterNum) {
|
|
return NextResponse.json({ error: messages.bookmarkRequired }, { status: 400 })
|
|
}
|
|
|
|
// Create or update bookmark
|
|
const bookmark = await prisma.chapterBookmark.upsert({
|
|
where: {
|
|
userId_bookId_chapterNum: {
|
|
userId: user.id,
|
|
bookId: bookId,
|
|
chapterNum: chapterNum
|
|
}
|
|
},
|
|
update: {
|
|
note: note || null
|
|
},
|
|
create: {
|
|
userId: user.id,
|
|
bookId: bookId,
|
|
chapterNum: chapterNum,
|
|
note: note || null
|
|
},
|
|
include: {
|
|
book: {
|
|
include: {
|
|
version: true
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
message: messages.bookmarkSaved,
|
|
bookmark
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('Bookmark creation error:', error)
|
|
const url = new URL(request.url)
|
|
const locale = url.searchParams.get('locale') || 'ro'
|
|
const messages = getErrorMessages(locale)
|
|
|
|
return NextResponse.json({ error: messages.bookmarkError }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
// DELETE - Remove chapter bookmark
|
|
export async function DELETE(request: Request) {
|
|
try {
|
|
const url = new URL(request.url)
|
|
const locale = url.searchParams.get('locale') || 'ro'
|
|
const bookId = url.searchParams.get('bookId')
|
|
const chapterNum = parseInt(url.searchParams.get('chapterNum') || '0')
|
|
const messages = getErrorMessages(locale)
|
|
|
|
// Get token from authorization header
|
|
const authHeader = request.headers.get('authorization')
|
|
const token = authHeader?.replace('Bearer ', '')
|
|
|
|
if (!token) {
|
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
|
}
|
|
|
|
// Verify token and get user
|
|
const user = await getUserFromToken(token)
|
|
if (!user) {
|
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
|
}
|
|
|
|
// Validate input
|
|
if (!bookId || !chapterNum) {
|
|
return NextResponse.json({ error: messages.bookmarkRequired }, { status: 400 })
|
|
}
|
|
|
|
// Delete bookmark
|
|
await prisma.chapterBookmark.delete({
|
|
where: {
|
|
userId_bookId_chapterNum: {
|
|
userId: user.id,
|
|
bookId: bookId,
|
|
chapterNum: chapterNum
|
|
}
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
message: messages.bookmarkRemoved
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('Bookmark deletion error:', error)
|
|
const url = new URL(request.url)
|
|
const locale = url.searchParams.get('locale') || 'ro'
|
|
const messages = getErrorMessages(locale)
|
|
|
|
return NextResponse.json({ error: messages.bookmarkError }, { status: 500 })
|
|
}
|
|
} |