diff --git a/src/server.js b/src/server.js index 6742721..31b31a0 100644 --- a/src/server.js +++ b/src/server.js @@ -158,6 +158,86 @@ app.post('/tracker/run', async (req, res) => { } }); +// ---------------------------------------------------- +// 🔍 SUCHAUFTRÄGE VERWALTEN (GET, PUT, DELETE) +// ---------------------------------------------------- + +// 1. Alle gespeicherten Suchaufträge anzeigen +app.get('/searches', async (req, res) => { + try { + const result = await pool.query('SELECT * FROM tracked_searches ORDER BY id ASC'); + res.json({ + success: true, + count: result.rows.length, + searches: result.rows + }); + } catch (error) { + res.status(500).json({ success: false, error: error.message }); + } +}); + +// 2. Einen bestimmten Suchauftrag bearbeiten (Query, Negativ-Keywords, Aktiv-Status) +app.put('/searches/:id', async (req, res) => { + const { id } = req.params; + const { query, negative_keywords, is_active } = req.body; + + try { + // Array sicherstellen, falls Negativ-Keywords als Komma-String gesendet werden + let formattedNegatives = negative_keywords; + if (typeof negative_keywords === 'string') { + formattedNegatives = negative_keywords.split(',').map(k => k.trim()).filter(Boolean); + } + + const result = await pool.query(` + UPDATE tracked_searches + SET query = COALESCE($1, query), + negative_keywords = COALESCE($2, negative_keywords), + is_active = COALESCE($3, is_active), + updated_at = CURRENT_TIMESTAMP + WHERE id = $4 + RETURNING *; + `, [ + query || null, + formattedNegatives || null, + is_active !== undefined ? is_active : null, + id + ]); + + if (result.rows.length === 0) { + return res.status(404).json({ success: false, message: 'Suchauftrag nicht gefunden.' }); + } + + res.json({ + success: true, + message: 'Suchauftrag erfolgreich aktualisiert!', + search: result.rows[0] + }); + } catch (error) { + res.status(500).json({ success: false, error: error.message }); + } +}); + +// 3. Einen Suchauftrag löschen +app.delete('/searches/:id', async (req, res) => { + const { id } = req.params; + + try { + const result = await pool.query('DELETE FROM tracked_searches WHERE id = $1 RETURNING *', [id]); + + if (result.rows.length === 0) { + return res.status(404).json({ success: false, message: 'Suchauftrag nicht gefunden.' }); + } + + res.json({ + success: true, + message: `Suchauftrag "${result.rows[0].query}" (ID: ${id}) wurde gelöscht.` + }); + } catch (error) { + res.status(500).json({ success: false, error: error.message }); + } +}); + + // Server-Start async function startServer() { await initDb();