v1.0 - Meal planner with favorites, history, and notes
This commit is contained in:
124
server.js
Normal file
124
server.js
Normal file
@@ -0,0 +1,124 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3001;
|
||||
const DATA_FILE = path.join(__dirname, 'data.json');
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(express.static(path.join(__dirname, 'dist')));
|
||||
|
||||
// Get all data
|
||||
app.get('/api/meals', (req, res) => {
|
||||
try {
|
||||
const data = JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
|
||||
res.json(data);
|
||||
} catch (err) {
|
||||
res.json({
|
||||
meals: [[Array(7).fill('').map(() => Array(4).fill(''))], [Array(7).fill('').map(() => Array(4).fill(''))]],
|
||||
favorites: [],
|
||||
history: []
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Save meals and optionally add to history
|
||||
app.post('/api/meals', (req, res) => {
|
||||
try {
|
||||
const data = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')) : {};
|
||||
data.meals = req.body.meals;
|
||||
if (req.body.addToHistory) {
|
||||
if (!data.history) data.history = [];
|
||||
data.history.unshift({
|
||||
week: req.body.meals[0],
|
||||
date: new Date().toISOString()
|
||||
});
|
||||
// Keep last 8 weeks
|
||||
data.history = data.history.slice(0, 8);
|
||||
}
|
||||
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Toggle favorite
|
||||
app.post('/api/favorites', (req, res) => {
|
||||
try {
|
||||
const data = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')) : {};
|
||||
if (!data.favorites) data.favorites = [];
|
||||
|
||||
const { meal, day, mealType } = req.body;
|
||||
const existing = data.favorites.find(f => f.meal === meal);
|
||||
|
||||
if (existing) {
|
||||
data.favorites = data.favorites.filter(f => f.meal !== meal);
|
||||
} else {
|
||||
data.favorites.push({ meal, day, mealType, date: new Date().toISOString() });
|
||||
}
|
||||
|
||||
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
||||
res.json({ success: true, favorites: data.favorites });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Get favorites
|
||||
app.get('/api/favorites', (req, res) => {
|
||||
try {
|
||||
const data = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')) : {};
|
||||
res.json({ favorites: data.favorites || [] });
|
||||
} catch (err) {
|
||||
res.json({ favorites: [] });
|
||||
}
|
||||
});
|
||||
|
||||
// Get history
|
||||
app.get('/api/history', (req, res) => {
|
||||
try {
|
||||
const data = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')) : {};
|
||||
res.json({ history: data.history || [] });
|
||||
} catch (err) {
|
||||
res.json({ history: [] });
|
||||
}
|
||||
});
|
||||
|
||||
// Get notes
|
||||
app.get('/api/notes', (req, res) => {
|
||||
try {
|
||||
const data = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')) : {};
|
||||
res.json({ notes: data.notes || {} });
|
||||
} catch (err) {
|
||||
res.json({ notes: {} });
|
||||
}
|
||||
});
|
||||
|
||||
// Save notes
|
||||
app.post('/api/notes', (req, res) => {
|
||||
try {
|
||||
const data = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')) : {};
|
||||
data.notes = req.body.notes;
|
||||
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Serve React app for all other routes
|
||||
app.get('/{*path}', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
|
||||
});
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
|
||||
});
|
||||
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user