33 lines
947 B
JavaScript
33 lines
947 B
JavaScript
import { getDb } from '../models/db.js';
|
|
|
|
export default function setupRoutes(app) {
|
|
// Get all favorites
|
|
app.get('/api/favorites', (req, res) => {
|
|
const db = getDb();
|
|
const stmt = db.prepare('SELECT * FROM entries WHERE favorite = 1 ORDER BY name ASC');
|
|
const favorites = stmt.all();
|
|
res.json(favorites);
|
|
});
|
|
|
|
// Toggle favorite
|
|
app.post('/api/entries/:id/favorite', (req, res) => {
|
|
const db = getDb();
|
|
const { id } = req.params;
|
|
|
|
// Get current favorite status
|
|
const getStmt = db.prepare('SELECT favorite FROM entries WHERE id = ?');
|
|
const entry = getStmt.get(id);
|
|
|
|
if (!entry) {
|
|
return res.status(404).json({ error: 'Entry not found' });
|
|
}
|
|
|
|
// Toggle
|
|
const newValue = entry.favorite ? 0 : 1;
|
|
const updateStmt = db.prepare('UPDATE entries SET favorite = ? WHERE id = ?');
|
|
updateStmt.run(newValue, id);
|
|
|
|
res.json({ id, favorite: newValue });
|
|
});
|
|
}
|