const express = require('express'); const fs = require('fs'); const path = require('path'); const router = express.Router(); const ALGORITHMS_PATH = path.join(__dirname, '../../data/algorithms.json'); router.get('/', (req, res) => { try { const data = fs.readFileSync(ALGORITHMS_PATH, 'utf8'); const algorithms = JSON.parse(data); res.json(algorithms); } catch (error) { res.status(500).json({ error: 'Failed to load algorithms' }); } }); router.put('/', (req, res) => { try { const updatedAlgorithms = req.body; fs.writeFileSync(ALGORITHMS_PATH, JSON.stringify(updatedAlgorithms, null, 2)); res.json({ message: 'Algorithms updated successfully' }); } catch (error) { res.status(500).json({ error: 'Failed to update algorithms' }); } }); router.get('/categories', (req, res) => { try { const data = fs.readFileSync(ALGORITHMS_PATH, 'utf8'); const algorithms = JSON.parse(data); const categories = {}; Object.values(algorithms.algorithms).forEach(algo => { if (!categories[algo.category]) { categories[algo.category] = []; } categories[algo.category].push(algo.name); }); res.json(categories); } catch (error) { res.status(500).json({ error: 'Failed to get categories' }); } }); module.exports = router;