implement dailyDish generator

This commit is contained in:
Mateo Körber Belmonte
2025-02-19 22:55:11 +01:00
parent d179de6147
commit 69ccbd3ee1
7 changed files with 444 additions and 7 deletions
+66 -3
View File
@@ -42,9 +42,27 @@ db.exec(`
) )
`); `);
db.exec(`
CREATE TABLE IF NOT EXISTS daily_entries (
date TEXT PRIMARY KEY,
week TEXT,
day_index INTEGER,
date_title TEXT,
daily_main TEXT,
daily_side TEXT,
halal BOOLEAN,
veggi BOOLEAN,
daily_price TEXT
)
`);
// Routes // Routes
app.get('/', (req, res) => { app.get('/edit/menu', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/public', 'menu.html')); res.sendFile(path.join(__dirname, '../frontend/public/edit', 'menu.html'));
});
app.get('/edit/dailyDish', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/public/edit', 'dailyDish.html'));
}); });
app.post('/save-week', (req, res) => { app.post('/save-week', (req, res) => {
@@ -82,6 +100,39 @@ app.post('/save-week', (req, res) => {
} }
}); });
app.post('/save-week-daily', (req, res) => {
const { week, entries } = req.body;
const insert = db.prepare(`
INSERT OR REPLACE INTO daily_entries (date, week, day_index, date_title, daily_main, daily_side, halal, veggi, daily_price)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
const transaction = db.transaction((entries) => {
for (const entry of entries) {
insert.run(
entry.date,
entry.week,
entry.dayIndex,
entry.dateTitle,
entry.dailyMain,
entry.dailySide,
entry.halal,
entry.veggi,
entry.dailyPrice
);
}
});
try {
transaction(entries);
res.json({ message: 'Woche erfolgreich gespeichert' });
} catch (error) {
console.error('Error saving week:', error);
res.status(500).json({ error: 'Fehler beim Speichern der Woche' });
}
});
app.get('/load-week', (req, res) => { app.get('/load-week', (req, res) => {
const { week } = req.query; const { week } = req.query;
@@ -94,6 +145,18 @@ app.get('/load-week', (req, res) => {
} }
}); });
app.get('/load-week-daily', (req, res) => {
const { week } = req.query;
try {
const entries = db.prepare('SELECT * FROM daily_entries WHERE week = ?').all(week);
res.json(entries);
} catch (error) {
console.error('Error loading week:', error);
res.status(500).json({ error: 'Fehler beim Laden der Woche' });
}
});
const { exec } = require('child_process'); const { exec } = require('child_process');
app.post('/generate-pdf', (req, res) => { app.post('/generate-pdf', (req, res) => {
@@ -106,7 +169,7 @@ app.post('/generate-pdf', (req, res) => {
} }
if (stderr) { if (stderr) {
console.error(`Error generating PDF: ${stderr}`); console.error(`Error generating PDF: ${stderr}`);
return res.status(500).json({ success: false, error: stderr}); return res.status(500).json({ success: false, error: stderr });
} }
console.log(`PDF generated: ${stdout}`); console.log(`PDF generated: ${stdout}`);
res.json({ success: true }); res.json({ success: true });
+61
View File
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Speiseplan Generator</title>
<meta name="google" content="notranslate">
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<div class="container">
<div class="week-controls">
<button onclick="previousWeek()">&lt; Vorherige Woche</button>
<span id="currentWeek"></span>
<button onclick="nextWeek()">Nächste Woche &gt;</button>
</div>
<div class="day-controls">
<button onclick="previousDay()">&lt; Vorheriger Tag</button>
<span id="currentDay"></span>
<button onclick="nextDay()">Nächster Tag &gt;</button>
</div>
<div class="template-container">
<div class="template-page dailyDish-bg">
<div class="form-overlay">
<form id="menuForm">
<script>
for (let i = 0; i < 5; i++) {
document.write(`
<div id="dayForm${i}" class="day-form ${i === 0 ? 'active' : ''}">
<input type="text" class="input-field-20" name="dateTitle${i}" style="bottom: 60.5%; left: 13.4%" placeholder="Datum">
<select class="input-field-19-h" name="halalVeggi${i}" style="bottom: 43.5%; left: 58%">
<option value="" style="color:red;">----</option>
<option value="halal" style="color:#00B050;">„HALAL“</option>
<option value="veggi" style="color:#00B050;">„veggi“</option>
</select>
<input type="text" class="input-field-19-b" name="dailyMain${i}" style="bottom: 30.5%; left: 13.4%" placeholder="Tagesgericht">
<input type="text" class="input-field-19" name="dailySide${i}" style="bottom: 24%; left: 13.4%" placeholder="Beilage">
<input type="text" class="input-field-14-i" name="dailyPrice${i}" style="bottom: 13.3%; left: 13.4%" placeholder="Preis">
</div>
`);
}
</script>
</form>
</div>
</div>
</div>
<div class="bottom-controls">
<button onclick="saveWeek()">Woche speichern</button>
<button onclick="generatePDF()">PDF generieren</button>
</div>
</div>
<script src="../src/dailyDish.js"></script>
</body>
</html>
@@ -5,7 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Speiseplan Generator</title> <title>Speiseplan Generator</title>
<meta name="google" content="notranslate"> <meta name="google" content="notranslate">
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="../style.css">
</head> </head>
<body> <body>
@@ -23,7 +23,7 @@
</div> </div>
<div class="template-container"> <div class="template-container">
<div class="template-page"> <div class="template-page menu-bg">
<div class="form-overlay"> <div class="form-overlay">
<form id="menuForm"> <form id="menuForm">
<script> <script>
@@ -56,7 +56,7 @@
</div> </div>
</div> </div>
<script src="src/menu.js"></script> <script src="../src/menu.js"></script>
</body> </body>
View File
+250
View File
@@ -0,0 +1,250 @@
let currentWeek = new Date();
currentWeek.setDate(currentWeek.getDate() - currentWeek.getDay() + 1); // Set to Monday
let currentDayIndex = 0; // 0 = Monday, 4 = Friday
function getWeekNumber(date) {
const target = new Date(date.valueOf());
const dayNumber = (date.getDay() + 6) % 7;
target.setDate(target.getDate() - dayNumber + 3);
const firstThursday = target.valueOf();
target.setMonth(0, 1);
if (target.getDay() !== 4) {
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
return 1 + Math.ceil((firstThursday - target) / 604800000);
}
function updateWeekDisplay() {
const startOfWeek = new Date(currentWeek);
const endOfWeek = new Date(currentWeek);
endOfWeek.setDate(startOfWeek.getDate() + 4); // Friday
const weekNumber = getWeekNumber(startOfWeek);
const startDate = startOfWeek.toLocaleDateString('de-DE', {
day: '2-digit',
month: '2-digit',
year: '2-digit'
});
const endDate = endOfWeek.toLocaleDateString('de-DE', {
day: '2-digit',
month: '2-digit',
year: '2-digit'
});
document.getElementById('currentWeek').textContent =
`KW${weekNumber} ${startDate} - ${endDate}`;
}
function updateDayDisplay() {
const currentDate = new Date(currentWeek);
currentDate.setDate(currentDate.getDate() + currentDayIndex);
const dayName = currentDate.toLocaleDateString('de-DE', { weekday: 'long' });
const dateStr = currentDate.toLocaleDateString('de-DE', {
day: '2-digit',
month: '2-digit',
year: '2-digit'
});
document.getElementById('currentDay').textContent = `${dayName} ${dateStr}`;
// Show the correct form for the current day
document.querySelectorAll('.day-form').forEach((form, index) => {
form.classList.toggle('active', index === currentDayIndex);
});
}
function previousDay() {
if (currentDayIndex > 0) {
currentDayIndex--;
updateDayDisplay();
}
}
function nextDay() {
if (currentDayIndex < 4) {
currentDayIndex++;
updateDayDisplay();
}
}
function previousWeek() {
currentWeek.setDate(currentWeek.getDate() - 7);
currentDayIndex = 0; // Reset to Monday
updateWeekDisplay();
updateDayDisplay();
const weekNumber = getWeekNumber(currentWeek);
const year = currentWeek.getFullYear();
const week = `KW${weekNumber} ${year}`;
loadWeekData(week);
}
function nextWeek() {
currentWeek.setDate(currentWeek.getDate() + 7);
currentDayIndex = 0; // Reset to Monday
updateWeekDisplay();
updateDayDisplay();
const weekNumber = getWeekNumber(currentWeek);
const year = currentWeek.getFullYear();
const week = `KW${weekNumber} ${year}`;
loadWeekData(week);
}
function saveWeek() {
const weekNumber = getWeekNumber(currentWeek);
const year = currentWeek.getFullYear();
const week = `KW${weekNumber} ${year}`;
const entries = [];
for (let i = 0; i < 5; i++) {
const currentDate = new Date(currentWeek);
currentDate.setDate(currentDate.getDate() + i);
// Anpassen der Werte entsprechend der daily_entries Tabelle
const halalVeggiValue = document.querySelector(`select[name="halalVeggi${i}"]`).value;
const entry = {
date: currentDate.toISOString().split('T')[0],
week: week,
dayIndex: i,
dateTitle: document.querySelector(`input[name="dateTitle${i}"]`).value,
dailyMain: document.querySelector(`input[name="dailyMain${i}"]`).value,
dailySide: document.querySelector(`input[name="dailySide${i}"]`).value,
halal: halalVeggiValue === 'halal' ? 1 : 0,
veggi: halalVeggiValue === 'veggi' ? 1 : 0,
dailyPrice: document.querySelector(`input[name="dailyPrice${i}"]`).value
};
entries.push(entry);
}
fetch('/save-week-daily', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ week, entries })
})
.then(response => response.json())
.then(data => {
alert(data.message);
})
.catch(error => {
console.error('Error saving week:', error);
alert('Fehler beim Speichern der Woche');
});
}
function loadWeekData(week) {
fetch(`/load-week-daily?week=${encodeURIComponent(week)}`)
.then(response => response.json())
.then(data => {
// Clear all input fields
for (let i = 0; i < 5; i++) {
document.querySelector(`input[name="dateTitle${i}"]`).value = '';
document.querySelector(`input[name="dailyMain${i}"]`).value = '';
document.querySelector(`input[name="dailySide${i}"]`).value = '';
document.querySelector(`select[name="halalVeggi${i}"]`).value = '';
document.querySelector(`input[name="dailyPrice${i}"]`).value = '';
}
// Fill input fields with loaded data
const loadedDays = new Set(data.map(entry => entry.day_index));
data.forEach(entry => {
const dayIndex = entry.day_index;
const dateTitleInput = document.querySelector(`input[name="dateTitle${dayIndex}"]`);
const dailyPriceInput = document.querySelector(`input[name="dailyPrice${dayIndex}"]`);
const halalVeggiSelect = document.querySelector(`select[name="halalVeggi${dayIndex}"]`);
dateTitleInput.value = entry.date_title;
document.querySelector(`input[name="dailyMain${dayIndex}"]`).value = entry.daily_main;
document.querySelector(`input[name="dailySide${dayIndex}"]`).value = entry.daily_side;
dailyPriceInput.value = entry.daily_price;
if (entry.halal) {
halalVeggiSelect.value = 'halal';
} else if (entry.veggi) {
halalVeggiSelect.value = 'veggi';
} else {
halalVeggiSelect.value = '';
}
// Automatically fill dateTitle if empty
if (!dateTitleInput.value) {
const currentDate = new Date(currentWeek);
currentDate.setDate(currentDate.getDate() + dayIndex);
const dayName = currentDate.toLocaleDateString('de-DE', { weekday: 'long' }).toUpperCase();
const dateStr = currentDate.toLocaleDateString('de-DE', {
day: '2-digit',
month: 'long'
}).toUpperCase();
dateTitleInput.value = `${dayName}, ${dateStr}`;
}
// Automatically fill dailyPrice if empty
if (!dailyPriceInput.value) {
dailyPriceInput.value = "6,50 € / 4,80 €";
}
});
// Automatically fill dateTitle, dailyPrice if not in DB
for (let i = 0; i < 5; i++) {
if (!loadedDays.has(i)) {
const dateTitleInput = document.querySelector(`input[name="dateTitle${i}"]`);
const dailyPriceInput = document.querySelector(`input[name="dailyPrice${i}"]`);
if (!dateTitleInput.value) {
const currentDate = new Date(currentWeek);
currentDate.setDate(currentDate.getDate() + i);
const dayName = currentDate.toLocaleDateString('de-DE', { weekday: 'long' }).toUpperCase();
const dateStr = currentDate.toLocaleDateString('de-DE', {
day: '2-digit',
month: 'long'
}).toUpperCase();
dateTitleInput.value = `${dayName}, ${dateStr}`;
}
if (!dailyPriceInput.value) {
dailyPriceInput.value = "6,50 € / 4,80 €";
}
}
}
})
.catch(error => {
console.error('Error loading week:', error);
alert('Fehler beim Laden der Woche.\n\nBitte kontaktieren Sie Ihren zuständigen Systemadministrator.');
});
}
function generatePDF() {
const weekNumber = getWeekNumber(currentWeek);
const year = currentWeek.getFullYear();
const week = `KW${weekNumber}-${year}`;
fetch('/generate-pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ week })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('PDF erfolgreich generiert');
} else {
const errorMessage = data.error || 'Unbekannter Fehler';
alert(`Fehler beim Generieren der PDF:\n\n${errorMessage}\n\nBitte kontaktieren Sie Ihren zuständigen Systemadministrator.`);
}
})
.catch(error => {
console.error('Error generating PDF:', error);
alert('Fehler beim Generieren der PDF');
});
}
// Initialize the display when page loads
updateWeekDisplay();
updateDayDisplay();
const weekNumber = getWeekNumber(currentWeek);
const year = currentWeek.getFullYear();
const week = `KW${weekNumber} ${year}`;
loadWeekData(week);
+1 -1
View File
@@ -220,7 +220,7 @@ function loadWeekData(week) {
}) })
.catch(error => { .catch(error => {
console.error('Error loading week:', error); console.error('Error loading week:', error);
alert('Fehler beim Laden der Woche'); alert('Fehler beim Laden der Woche!\n\nBitte kontaktieren Sie Ihren zuständigen Systemadministrator.');
}); });
} }
+63
View File
@@ -64,10 +64,18 @@ button {
position: relative; position: relative;
height: 0; height: 0;
padding-bottom: 60%; padding-bottom: 60%;
}
.menu-bg {
background: url('/resources/img/speiseplan_vorlage_Seite_1.png') no-repeat center; background: url('/resources/img/speiseplan_vorlage_Seite_1.png') no-repeat center;
background-size: contain; background-size: contain;
} }
.dailyDish-bg {
background: url('/resources/img/tagesgericht.png') no-repeat center;
background-size: contain;
}
.form-overlay { .form-overlay {
position: absolute; position: absolute;
top: 0; top: 0;
@@ -131,6 +139,61 @@ button {
font-family: 'Fira Sans', sans-serif font-family: 'Fira Sans', sans-serif
} }
.input-field-20 {
position: absolute;
background: transparent;
border: 1px solid rgba(0, 0, 0, 0.07);
padding: 2px 4px;
font-size: 32px;
color: rgb(159, 28, 12);
font-weight: bold;
width: 333px;
font-family: 'Fira Sans', sans-serif
}
.input-field-19-b {
position: absolute;
background: transparent;
border: 1px solid rgba(0, 0, 0, 0.07);
padding: 2px 4px;
font-size: 30px;
font-weight: bold;
width: 500px;
font-family: 'Fira Sans', sans-serif
}
.input-field-19 {
position: absolute;
background: transparent;
border: 1px solid rgba(0, 0, 0, 0.07);
padding: 2px 4px;
font-size: 30px;
width: 500px;
font-family: 'Fira Sans', sans-serif
}
.input-field-19-h {
position: absolute;
background: transparent;
border: 1px solid rgba(0, 0, 0, 0.07);
padding: 2px 4px;
font-size: 36px;
color: #00B050;
font-weight: 500;
font-family: 'Fira Sans', sans-serif
}
.input-field-14-i {
position: absolute;
background: transparent;
border: 1px solid rgba(0, 0, 0, 0.07);
padding: 2px 4px;
font-size: 23px;
font-style: italic;
width: 333px;
font-family: 'Fira Sans', sans-serif
}
.bottom-controls { .bottom-controls {
text-align: center; text-align: center;
margin: 20px 0; margin: 20px 0;