Datumsauswahl korrigiert
This commit is contained in:
parent
94b50dbd21
commit
a866361fda
46
app.js
46
app.js
|
@ -5,7 +5,7 @@ const crypto = require('crypto');
|
||||||
const nodemailer = require('nodemailer');
|
const nodemailer = require('nodemailer');
|
||||||
const { Pool } = require('pg');
|
const { Pool } = require('pg');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const moment=require('moment')
|
const moment = require('moment');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
@ -134,12 +134,7 @@ const getTraining = async (selectedDate) => {
|
||||||
// Funktion zum Laden aller Spiele
|
// Funktion zum Laden aller Spiele
|
||||||
const getAllSpiele = async () => {
|
const getAllSpiele = async () => {
|
||||||
const result = await pool.query(`
|
const result = await pool.query(`
|
||||||
SELECT s.id, s.name,
|
SELECT * FROM spiele ORDER BY name ASC;
|
||||||
COALESCE(EXTRACT(EPOCH FROM (NOW() - MAX(tr.datum))) / 604800, EXTRACT(EPOCH FROM (NOW() - '1970-01-01'::date)) / 604800) AS weeks_since_last
|
|
||||||
FROM spiele s
|
|
||||||
LEFT JOIN trainings tr ON s.id = tr.spiel
|
|
||||||
GROUP BY s.id
|
|
||||||
ORDER BY weeks_since_last ASC;
|
|
||||||
`);
|
`);
|
||||||
return result.rows;
|
return result.rows;
|
||||||
};
|
};
|
||||||
|
@ -335,8 +330,6 @@ app.get('/profile', requireAuth, (req, res) => {
|
||||||
res.render('profile', { session: req.session}); // Stelle sicher, dass es eine profile.ejs gibt
|
res.render('profile', { session: req.session}); // Stelle sicher, dass es eine profile.ejs gibt
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.post('/profile', requireAuth, async (req, res) => {
|
app.post('/profile', requireAuth, async (req, res) => {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
try {
|
try {
|
||||||
|
@ -355,7 +348,19 @@ app.post('/profile', requireAuth, async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/update-training', async (req, res) => {
|
app.post('/update-training', async (req, res) => {
|
||||||
const { trainingId, type, spielId } = req.body;
|
const { trainingId, type, spielName } = req.body;
|
||||||
|
let spielId;
|
||||||
|
|
||||||
|
const spiel = await pool.query(`SELECT * FROM spiele WHERE name = $1`, [spielName]);
|
||||||
|
|
||||||
|
if (spiel.rows.length > 0) {
|
||||||
|
spielId = spiel.rows[0].id;
|
||||||
|
console.log('Spiel existiert, die ID ist $1', [ spielId ]);
|
||||||
|
} else {
|
||||||
|
const newSpiel = await pool.query('INSERT INTO spiele (name) VALUES ($1) RETURNING id', [spielName]);
|
||||||
|
spielId = newSpiel.rows[0].id;
|
||||||
|
console.log('Spiel existiert nicht, wurde mitID $1 angelegt', [ spielId ]);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (type === 'spiel') {
|
if (type === 'spiel') {
|
||||||
|
@ -395,7 +400,15 @@ app.get('/admin', requireAuth, requireAdmin, async (req, res) => {
|
||||||
// Teilnehmer_innen
|
// Teilnehmer_innen
|
||||||
app.get('/', requireAuth, async (req, res) => {
|
app.get('/', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const selectedDate = req.query.date;
|
let dateParam = req.query.date;
|
||||||
|
let selectedDate;
|
||||||
|
|
||||||
|
|
||||||
|
if (dateParam) {
|
||||||
|
selectedDate = moment(dateParam, 'DD.MM.YYYY').toDate();
|
||||||
|
} else {
|
||||||
|
selectedDate = moment().toDate();
|
||||||
|
}
|
||||||
|
|
||||||
const training =await getTraining(selectedDate);
|
const training =await getTraining(selectedDate);
|
||||||
|
|
||||||
|
@ -405,6 +418,12 @@ app.get('/', requireAuth, async (req, res) => {
|
||||||
rawDatum: tr.datum
|
rawDatum: tr.datum
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Vorheriges Training ermitteln
|
||||||
|
const previousTrainingResult = await pool.query('SELECT * FROM trainings WHERE datum < $1 ORDER BY datum DESC LIMIT 1',[ selectedDate ]);
|
||||||
|
const nextTrainingResult = await pool.query('SELECT * FROM trainings WHERE datum > $1 ORDER BY datum ASC LIMIT 1',[ selectedDate ]);
|
||||||
|
const previousTraining = previousTrainingResult.rows.length > 0 ? previousTrainingResult.rows[0] : null;
|
||||||
|
const nextTraining = nextTrainingResult.rows.length > 0 ? nextTrainingResult.rows[0] : null;
|
||||||
|
|
||||||
if (training) {
|
if (training) {
|
||||||
training.datum = formatDate(training.datum);
|
training.datum = formatDate(training.datum);
|
||||||
}
|
}
|
||||||
|
@ -422,7 +441,10 @@ app.get('/', requireAuth, async (req, res) => {
|
||||||
spielleiterCandidates,
|
spielleiterCandidates,
|
||||||
aufwaermenCandidates, // Übergeben der Kandidaten für Aufwärmleiter
|
aufwaermenCandidates, // Übergeben der Kandidaten für Aufwärmleiter
|
||||||
spielCandidates, // Übergeben der Spiele
|
spielCandidates, // Übergeben der Spiele
|
||||||
session: req.session
|
session: req.session,
|
||||||
|
previousTraining,
|
||||||
|
nextTraining,
|
||||||
|
moment
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
<%- include('partials/header') %>
|
<%- include('partials/header') %>
|
||||||
<h1>Training <%= training.datum %></h1>
|
<h1>
|
||||||
<h2> <%= training.datum + 7 %> </h2>
|
<div>
|
||||||
<form method="GET" action="/">
|
<% if (previousTraining) { %>
|
||||||
<label for="training-date">Datum wählen:</label>
|
<a href="/?date=<%= moment(previousTraining.datum).format('DD.MM.YYYY') %>"> < </a>
|
||||||
<select id="training-date" name="date" onchange="this.form.submit()">
|
<% } %>
|
||||||
<% trainingsDates.forEach(date => { %>
|
Training <%= training.datum %>
|
||||||
<option value="<%= date.rawDatum %>" <%= selectedDate === date.datum ? 'selected' : '' %>>
|
<% if (nextTraining) { %>
|
||||||
<%= date.datum %>
|
<a href="/?date=<%= moment(nextTraining.datum).format('DD.MM.YYYY') %>"> > </a>
|
||||||
</option>
|
<% } %>
|
||||||
<% }) %>
|
</h1>
|
||||||
</select>
|
</div>
|
||||||
</form>
|
|
||||||
<% if (training) { %>
|
<% if (training) { %>
|
||||||
<h2> Geräte </h2>
|
<h2> Geräte </h2>
|
||||||
<ul>
|
<ul>
|
||||||
|
@ -20,11 +20,16 @@
|
||||||
<li>Riege 4: <%= training.geraet_riege_4_name %></li>
|
<li>Riege 4: <%= training.geraet_riege_4_name %></li>
|
||||||
<li>Riege 5: <%= training.geraet_riege_5_name %></li>
|
<li>Riege 5: <%= training.geraet_riege_5_name %></li>
|
||||||
</ul>
|
</ul>
|
||||||
Aufwärmen:
|
|
||||||
|
<div id="aufwaermen">
|
||||||
|
<h3>Aufwärmen:</h3>
|
||||||
|
|
||||||
<% if (training.aufwaermleiter_name) { %>
|
<% if (training.aufwaermleiter_name) { %>
|
||||||
<%= training.aufwaermleiter_name %>
|
<p><strong>Leitung:</strong>
|
||||||
|
<%= training.aufwaermleiter_name %></p>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<form method="POST" action="/update-leader">
|
<form method="POST" action="/update-leader">
|
||||||
|
<p><strong>Leitung:</strong>
|
||||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||||
<input type="hidden" name="type" value="aufwaermleiter">
|
<input type="hidden" name="type" value="aufwaermleiter">
|
||||||
<select name="leaderId">
|
<select name="leaderId">
|
||||||
|
@ -33,35 +38,36 @@
|
||||||
<% }) %>
|
<% }) %>
|
||||||
</select>
|
</select>
|
||||||
<button type="submit">Speichern</button>
|
<button type="submit">Speichern</button>
|
||||||
</form>
|
</form></p>
|
||||||
<% } %>
|
<% } %>
|
||||||
</br>
|
|
||||||
<% if (training.aufwaermen_name) { %>
|
<% if (training.aufwaermen_name) { %>
|
||||||
<%= training.aufwaermen_name %>
|
<p><strong>Spiel:</strong>
|
||||||
|
<%= training.aufwaermen_name %></p>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<form method="POST" action="/update-training">
|
<form method="POST" action="/update-training">
|
||||||
|
<p><strong>Spiel:</strong>
|
||||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||||
<input type="hidden" name="type" value="aufwaermen">
|
<input type="hidden" name="type" value="aufwaermen">
|
||||||
<select name="spielId">
|
<datalist id="spiele">
|
||||||
<% aufwaermenCandidates.forEach(candidate => { %>
|
<% aufwaermenCandidates.forEach(candidate => { %>
|
||||||
<option value="<%= candidate.id %>"><%= candidate.name %> (<%= Math.floor(candidate.weeks_since_last) %> Wochen)</option>
|
<option value="<%= candidate.name %>"><%= candidate.name %></option>
|
||||||
<% }) %>
|
<% }) %>
|
||||||
</select>
|
</datalist>
|
||||||
|
<input list="spiele" id="spielName" name="spielName">
|
||||||
<button type="submit">Speichern</button>
|
<button type="submit">Speichern</button>
|
||||||
</form>
|
</form></p>
|
||||||
<% } %>
|
<% } %>
|
||||||
<form method="POST" action="/add-spiel">
|
</div>
|
||||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
<div id="spiel">
|
||||||
<input type="hidden" name="type" value="aufwaermen">
|
<h3>Spiel: </h3>
|
||||||
<input type="text" name="aufwaermenName" placeholder="Neues Aufwärmen">
|
|
||||||
<button type="submit">Hinzufügen</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
Spiel:
|
|
||||||
<% if (training.spielleiter_name) { %>
|
<% if (training.spielleiter_name) { %>
|
||||||
<%= training.spielleiter_name %>
|
<p><strong>Leitung:</strong>
|
||||||
|
<%= training.spielleiter_name %></p>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<form method="POST" action="/update-leader">
|
<form method="POST" action="/update-leader">
|
||||||
|
<p><strong>Leitung:</strong>
|
||||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||||
<input type="hidden" name="type" value="spielleiter">
|
<input type="hidden" name="type" value="spielleiter">
|
||||||
<select name="leaderId">
|
<select name="leaderId">
|
||||||
|
@ -70,31 +76,42 @@
|
||||||
<% }) %>
|
<% }) %>
|
||||||
</select>
|
</select>
|
||||||
<button type="submit">Speichern</button>
|
<button type="submit">Speichern</button>
|
||||||
</form>
|
</form></p>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
||||||
</br>
|
|
||||||
<!-- Für Spiel -->
|
|
||||||
<% if (training.spiel_name) { %>
|
<% if (training.spiel_name) { %>
|
||||||
|
<p><strong>Spiel:</strong>
|
||||||
<%= training.spiel_name %>
|
<%= training.spiel_name %>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
|
|
||||||
<form method="POST" action="/update-training">
|
<form method="POST" action="/update-training">
|
||||||
|
<p><strong>Spiel:</strong>
|
||||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||||
<input type="hidden" name="type" value="spiel">
|
<input type="hidden" name="type" value="spiel">
|
||||||
<select name="spielId">
|
<datalist id="spiele">
|
||||||
<% spielCandidates.forEach(candidate => { %>
|
<% aufwaermenCandidates.forEach(candidate => { %>
|
||||||
<option value="<%= candidate.id %>"><%= candidate.name %> (<%= Math.floor(candidate.weeks_since_last) %> Wochen)</option>
|
<option value="<%= candidate.name %>"><%= candidate.name %></option>
|
||||||
<% }) %>
|
<% }) %>
|
||||||
</select>
|
</datalist>
|
||||||
|
<input list="spiele" id="spielName" name="spielName">
|
||||||
<button type="submit">Speichern</button>
|
<button type="submit">Speichern</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<% } %>
|
<% } %>
|
||||||
<form method="POST" action="/add-spiel">
|
</div>
|
||||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
|
||||||
<input type="hidden" name="type" value="spiel">
|
<form method="GET" action="/">
|
||||||
<input type="text" name="spielName" placeholder="Neues Spiel">
|
<label for="training-date">Datum wählen:</label>
|
||||||
<button type="submit">Hinzufügen</button>
|
<select id="training-date" name="date" onchange="this.form.submit()">
|
||||||
|
<% trainingsDates.forEach(date => { %>
|
||||||
|
<option value="<%= date.datum %>" <%= selectedDate === date.datum ? 'selected' : '' %>>
|
||||||
|
<%= date.datum %>
|
||||||
|
</option>
|
||||||
|
<% }) %>
|
||||||
|
</select>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<p>Kein Training gefunden für das ausgewählte Datum.</p>
|
<p>Kein Training gefunden für das ausgewählte Datum.</p>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
Loading…
Reference in New Issue