Meal Tracker - full feature set with auth, favorites, admin panel
This commit is contained in:
@@ -1,120 +1,75 @@
|
||||
import express from 'express';
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { fileURLToPath } from 'url';
|
||||
import 'dotenv/config';
|
||||
|
||||
import db from '../models/db.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
import { getDb } from '../models/db.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const UPLOAD_DIR = process.env.UPLOAD_DIR || path.join(__dirname, '../../uploads');
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, UPLOAD_DIR);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const timestamp = Date.now();
|
||||
const ext = path.extname(file.originalname);
|
||||
cb(null, `${timestamp}${ext}`);
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
fileFilter: (req, file, cb) => {
|
||||
const allowedTypes = /jpeg|jpg|png|gif|webp/;
|
||||
const ext = allowedTypes.test(path.extname(file.originalname).toLowerCase());
|
||||
const mime = allowedTypes.test(file.mimetype);
|
||||
|
||||
if (ext && mime) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Only image files are allowed'), false);
|
||||
}
|
||||
},
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/entries
|
||||
router.get('/', (req, res) => {
|
||||
try {
|
||||
const { date } = req.query;
|
||||
const entries = db.getAll(date);
|
||||
res.json(entries);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
const db = getDb();
|
||||
const { user_id } = req.query;
|
||||
const userId = user_id || 1;
|
||||
|
||||
// Return all entries, don't filter by date
|
||||
const entries = db.prepare('SELECT * FROM entries WHERE user_id = ? ORDER BY created_at DESC').all(userId);
|
||||
res.json(entries);
|
||||
});
|
||||
|
||||
router.get('/recent', (req, res) => {
|
||||
const db = getDb();
|
||||
const { user_id, limit = 10 } = req.query;
|
||||
const userId = user_id || 1;
|
||||
|
||||
const entries = db.prepare('SELECT DISTINCT name, description, calories, protein, carbs, fat FROM entries WHERE user_id = ? ORDER BY created_at DESC LIMIT ?').all(userId, limit);
|
||||
res.json(entries);
|
||||
});
|
||||
|
||||
// GET /api/entries/:id
|
||||
router.get('/:id', (req, res) => {
|
||||
try {
|
||||
const entry = db.getById(req.params.id);
|
||||
if (!entry) {
|
||||
return res.status(404).json({ error: 'Entry not found' });
|
||||
}
|
||||
res.json(entry);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
const db = getDb();
|
||||
const { id } = req.params;
|
||||
const { user_id } = req.query;
|
||||
const userId = user_id || 1;
|
||||
|
||||
const entry = db.prepare('SELECT * FROM entries WHERE id = ? AND user_id = ?').get(id, userId);
|
||||
if (!entry) return res.status(404).json({ error: 'Entry not found' });
|
||||
res.json(entry);
|
||||
});
|
||||
|
||||
// POST /api/entries
|
||||
router.post('/', upload.single('image'), (req, res) => {
|
||||
try {
|
||||
const { name, description, notes, calories, protein, carbs, fat } = req.body;
|
||||
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'Name is required' });
|
||||
}
|
||||
|
||||
const image_url = req.file ? `/uploads/${req.file.filename}` : null;
|
||||
|
||||
const entry = db.create({
|
||||
name,
|
||||
description,
|
||||
image_url,
|
||||
notes,
|
||||
calories: calories ? parseFloat(calories) : 0,
|
||||
protein: protein ? parseFloat(protein) : 0,
|
||||
carbs: carbs ? parseFloat(carbs) : 0,
|
||||
fat: fat ? parseFloat(fat) : 0
|
||||
});
|
||||
|
||||
res.status(201).json(entry);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
router.post('/', (req, res) => {
|
||||
const db = getDb();
|
||||
const { name, description, notes, calories, protein, carbs, fat, meal_time, user_id } = req.body;
|
||||
const userId = user_id || 1;
|
||||
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO entries (name, description, notes, calories, protein, carbs, fat, meal_time, user_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
const result = stmt.run(name, description, notes, calories, protein, carbs, fat, meal_time, userId);
|
||||
res.json({ id: result.lastInsertRowid, name, description, notes, calories, protein, carbs, fat, meal_time });
|
||||
});
|
||||
|
||||
router.put('/:id', (req, res) => {
|
||||
const db = getDb();
|
||||
const { id } = req.params;
|
||||
const { name, description, notes, calories, protein, carbs, fat, meal_time, user_id } = req.body;
|
||||
const userId = user_id || 1;
|
||||
|
||||
const stmt = db.prepare(`
|
||||
UPDATE entries SET name = ?, description = ?, notes = ?, calories = ?, protein = ?, carbs = ?, fat = ?, meal_time = ?
|
||||
WHERE id = ? AND user_id = ?
|
||||
`);
|
||||
|
||||
stmt.run(name, description, notes, calories, protein, carbs, fat, meal_time, id, userId);
|
||||
res.json({ id, name, description, notes, calories, protein, carbs, fat, meal_time });
|
||||
});
|
||||
|
||||
// DELETE /api/entries/:id
|
||||
router.delete('/:id', (req, res) => {
|
||||
try {
|
||||
const entry = db.getById(req.params.id);
|
||||
if (!entry) {
|
||||
return res.status(404).json({ error: 'Entry not found' });
|
||||
}
|
||||
|
||||
if (entry.image_url) {
|
||||
const imagePath = path.join(__dirname, '../../', entry.image_url);
|
||||
if (fs.existsSync(imagePath)) {
|
||||
fs.unlinkSync(imagePath);
|
||||
}
|
||||
}
|
||||
|
||||
db.remove(req.params.id);
|
||||
res.json({ message: 'Entry deleted' });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
const db = getDb();
|
||||
const { id } = req.params;
|
||||
const { user_id } = req.query;
|
||||
const userId = user_id || 1;
|
||||
|
||||
db.prepare('DELETE FROM entries WHERE id = ? AND user_id = ?').run(id, userId);
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user