Search filters now backed by DB per locale/version: fetch books from /api/bible/books; send bookKeys + locale to API; API constrains by versionId and bookKey/name; keeps testament filter.

This commit is contained in:
andupetcu
2025-09-20 18:10:50 +03:00
parent 0f31dfca8e
commit a1b92be2a5
2 changed files with 89 additions and 44 deletions

View File

@@ -10,6 +10,9 @@ export async function GET(request: NextRequest) {
const testament = searchParams.get('testament') || 'all'
const exactMatch = searchParams.get('exactMatch') === 'true'
const books = searchParams.get('books')?.split(',').filter(Boolean) || []
const bookKeys = searchParams.get('bookKeys')?.split(',').filter(Boolean) || []
const locale = (searchParams.get('locale') || 'ro').toLowerCase()
const versionAbbr = searchParams.get('version') || undefined
if (!query) {
return NextResponse.json(
@@ -22,6 +25,28 @@ export async function GET(request: NextRequest) {
)
}
// Resolve Bible version to constrain search to selected language/version
let bibleVersion: any
if (versionAbbr) {
bibleVersion = await prisma.bibleVersion.findFirst({
where: { abbreviation: versionAbbr, language: { in: [locale, locale.toLowerCase(), locale.toUpperCase()] } }
})
} else {
bibleVersion = await prisma.bibleVersion.findFirst({
where: { language: { in: [locale, locale.toLowerCase(), locale.toUpperCase()] }, isDefault: true }
})
if (!bibleVersion) {
bibleVersion = await prisma.bibleVersion.findFirst({
where: { language: { in: [locale, locale.toLowerCase(), locale.toUpperCase()] } },
orderBy: { createdAt: 'asc' }
})
}
}
if (!bibleVersion) {
return NextResponse.json({ success: true, results: [], total: 0 })
}
// Build search conditions
const searchConditions: any = {}
@@ -39,51 +64,57 @@ export async function GET(request: NextRequest) {
}
}
// Testament filter
let testamentFilter = {}
// Testament filter (by orderNum threshold)
let testamentFilter: any = {}
if (testament === 'old') {
// Old Testament books (approximate book IDs 1-39)
testamentFilter = {
chapter: {
book: {
orderNum: {
lte: 39
}
versionId: bibleVersion.id,
orderNum: { lte: 39 }
}
}
}
} else if (testament === 'new') {
// New Testament books (approximate book IDs 40+)
testamentFilter = {
chapter: {
book: {
orderNum: {
gt: 39
}
versionId: bibleVersion.id,
orderNum: { gt: 39 }
}
}
}
}
// Books filter
let booksFilter = {}
if (books.length > 0) {
let booksFilter: any = {}
if (bookKeys.length > 0) {
booksFilter = {
chapter: {
book: {
name: {
in: books
}
versionId: bibleVersion.id,
bookKey: { in: bookKeys }
}
}
}
} else if (books.length > 0) {
booksFilter = {
chapter: {
book: {
versionId: bibleVersion.id,
name: { in: books }
}
}
}
}
// Combine all filters
const baseVersionScope = { chapter: { book: { versionId: bibleVersion.id } } }
const whereCondition = {
...searchConditions,
...baseVersionScope,
...testamentFilter,
...booksFilter
...booksFilter,
}
// Search verses