117 lines
2.8 KiB
JavaScript
117 lines
2.8 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { fullTextSearch, getAvailableTables, getTableColumns, SEARCH_CONFIG } = require('../services/searchService');
|
|
|
|
/**
|
|
* GET /api/search?q=suchbegriff
|
|
* Führt eine Volltextsuche durch
|
|
*/
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const searchTerm = req.query.q;
|
|
|
|
if (!searchTerm) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Suchbegriff fehlt. Verwenden Sie ?q=IhrSuchbegriff'
|
|
});
|
|
}
|
|
|
|
const results = await fullTextSearch(searchTerm);
|
|
|
|
const totalMatches = results.reduce((sum, table) => sum + table.matchCount, 0);
|
|
const tableErrors = results.filter(table => table && table.error);
|
|
|
|
// Wenn es Konfigurations-/SQL-Fehler gibt und gleichzeitig keine Treffer gefunden wurden,
|
|
// deutet das sehr wahrscheinlich auf ein Problem mit der Suchkonfiguration hin
|
|
if (totalMatches === 0 && tableErrors.length > 0) {
|
|
return res.status(500).json({
|
|
success: false,
|
|
searchTerm: searchTerm,
|
|
totalMatches: totalMatches,
|
|
tablesSearched: results.length,
|
|
results: results,
|
|
error: 'Fehler bei der Suche in einer oder mehreren Tabellen. Bitte prüfen Sie die Suchkonfiguration (Spaltennamen, Tabellen).',
|
|
tableErrors: tableErrors
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
searchTerm: searchTerm,
|
|
totalMatches: totalMatches,
|
|
tablesSearched: results.length,
|
|
results: results,
|
|
tableErrors: tableErrors
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Suchfehler:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/search/tables
|
|
* Gibt alle verfügbaren Tabellen zurück
|
|
*/
|
|
router.get('/tables', async (req, res) => {
|
|
try {
|
|
const tables = await getAvailableTables();
|
|
|
|
res.json({
|
|
success: true,
|
|
tables: tables,
|
|
configured: SEARCH_CONFIG.map(t => t.tableName)
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Fehler beim Abrufen der Tabellen:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/search/tables/:tableName
|
|
* Gibt die Spalten einer Tabelle zurück
|
|
*/
|
|
router.get('/tables/:tableName', async (req, res) => {
|
|
try {
|
|
const tableName = req.params.tableName;
|
|
const columns = await getTableColumns(tableName);
|
|
|
|
res.json({
|
|
success: true,
|
|
tableName: tableName,
|
|
columns: columns
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Fehler beim Abrufen der Spalten:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/search/config
|
|
* Gibt die aktuelle Suchkonfiguration zurück
|
|
*/
|
|
router.get('/config', (req, res) => {
|
|
res.json({
|
|
success: true,
|
|
config: SEARCH_CONFIG
|
|
});
|
|
});
|
|
|
|
module.exports = router;
|
|
|