Implement complete bookmark functionality for Bible reading
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>
This commit is contained in:
73
app/api/bookmarks/verse/check/route.ts
Normal file
73
app/api/bookmarks/verse/check/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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 verificarea bookmark-ului'
|
||||
},
|
||||
en: {
|
||||
unauthorized: 'Unauthorized',
|
||||
bookmarkRequired: 'Bookmark information is required',
|
||||
bookmarkError: 'Error checking bookmark'
|
||||
}
|
||||
}
|
||||
return messages[locale as keyof typeof messages] || messages.ro
|
||||
}
|
||||
|
||||
// GET - Check if verse is bookmarked
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const url = new URL(request.url)
|
||||
const locale = url.searchParams.get('locale') || 'ro'
|
||||
const verseId = url.searchParams.get('verseId')
|
||||
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 (!verseId) {
|
||||
return NextResponse.json({ error: messages.bookmarkRequired }, { status: 400 })
|
||||
}
|
||||
|
||||
// Check if bookmark exists
|
||||
const bookmark = await prisma.bookmark.findUnique({
|
||||
where: {
|
||||
userId_verseId: {
|
||||
userId: user.id,
|
||||
verseId: verseId
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
isBookmarked: !!bookmark,
|
||||
bookmark: bookmark || null
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('Verse bookmark check 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 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user