38 lines
1.6 KiB
TypeScript
38 lines
1.6 KiB
TypeScript
|
|
import { Router } from 'express';
|
||
|
|
import db from '../db/db';
|
||
|
|
|
||
|
|
const router = Router();
|
||
|
|
|
||
|
|
// Get meals for a date range (e.g. ?start=2024-01-01&end=2024-01-31)
|
||
|
|
router.get('/', (req, res) => {
|
||
|
|
const { start, end } = req.query as { start?: string; end?: string };
|
||
|
|
if (start && end) {
|
||
|
|
return res.json(db.prepare('SELECT * FROM meals WHERE date >= ? AND date <= ? ORDER BY date ASC').all(start, end));
|
||
|
|
}
|
||
|
|
res.json(db.prepare('SELECT * FROM meals ORDER BY date ASC').all());
|
||
|
|
});
|
||
|
|
|
||
|
|
router.get('/:date', (req, res) => {
|
||
|
|
const row = db.prepare('SELECT * FROM meals WHERE date = ?').get(req.params.date);
|
||
|
|
if (!row) return res.status(404).json({ error: 'No meal for this date' });
|
||
|
|
res.json(row);
|
||
|
|
});
|
||
|
|
|
||
|
|
router.put('/:date', (req, res) => {
|
||
|
|
const { title, description, recipe_url } = req.body as { title: string; description?: string; recipe_url?: string };
|
||
|
|
if (!title?.trim()) return res.status(400).json({ error: 'Title is required' });
|
||
|
|
db.prepare(`
|
||
|
|
INSERT INTO meals (date, title, description, recipe_url) VALUES (?, ?, ?, ?)
|
||
|
|
ON CONFLICT(date) DO UPDATE SET title=excluded.title, description=excluded.description, recipe_url=excluded.recipe_url
|
||
|
|
`).run(req.params.date, title.trim(), description ?? null, recipe_url ?? null);
|
||
|
|
res.json(db.prepare('SELECT * FROM meals WHERE date = ?').get(req.params.date));
|
||
|
|
});
|
||
|
|
|
||
|
|
router.delete('/:date', (req, res) => {
|
||
|
|
const result = db.prepare('DELETE FROM meals WHERE date = ?').run(req.params.date);
|
||
|
|
if (result.changes === 0) return res.status(404).json({ error: 'No meal for this date' });
|
||
|
|
res.status(204).end();
|
||
|
|
});
|
||
|
|
|
||
|
|
export default router;
|