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 { Pool } = require('pg');
|
||||
const path = require('path');
|
||||
const moment=require('moment')
|
||||
const moment = require('moment');
|
||||
require('dotenv').config();
|
||||
|
||||
const app = express();
|
||||
|
@ -134,12 +134,7 @@ const getTraining = async (selectedDate) => {
|
|||
// Funktion zum Laden aller Spiele
|
||||
const getAllSpiele = async () => {
|
||||
const result = await pool.query(`
|
||||
SELECT s.id, s.name,
|
||||
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;
|
||||
SELECT * FROM spiele ORDER BY name ASC;
|
||||
`);
|
||||
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
|
||||
});
|
||||
|
||||
|
||||
|
||||
app.post('/profile', requireAuth, async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
try {
|
||||
|
@ -355,7 +348,19 @@ app.post('/profile', requireAuth, 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 {
|
||||
if (type === 'spiel') {
|
||||
|
@ -395,7 +400,15 @@ app.get('/admin', requireAuth, requireAdmin, async (req, res) => {
|
|||
// Teilnehmer_innen
|
||||
app.get('/', requireAuth, async (req, res) => {
|
||||
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);
|
||||
|
||||
|
@ -405,6 +418,12 @@ app.get('/', requireAuth, async (req, res) => {
|
|||
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) {
|
||||
training.datum = formatDate(training.datum);
|
||||
}
|
||||
|
@ -422,7 +441,10 @@ app.get('/', requireAuth, async (req, res) => {
|
|||
spielleiterCandidates,
|
||||
aufwaermenCandidates, // Übergeben der Kandidaten für Aufwärmleiter
|
||||
spielCandidates, // Übergeben der Spiele
|
||||
session: req.session
|
||||
session: req.session,
|
||||
previousTraining,
|
||||
nextTraining,
|
||||
moment
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
<%- include('partials/header') %>
|
||||
<h1>Training <%= training.datum %></h1>
|
||||
<h2> <%= training.datum + 7 %> </h2>
|
||||
<form method="GET" action="/">
|
||||
<label for="training-date">Datum wählen:</label>
|
||||
<select id="training-date" name="date" onchange="this.form.submit()">
|
||||
<% trainingsDates.forEach(date => { %>
|
||||
<option value="<%= date.rawDatum %>" <%= selectedDate === date.datum ? 'selected' : '' %>>
|
||||
<%= date.datum %>
|
||||
</option>
|
||||
<% }) %>
|
||||
</select>
|
||||
</form>
|
||||
<h1>
|
||||
<div>
|
||||
<% if (previousTraining) { %>
|
||||
<a href="/?date=<%= moment(previousTraining.datum).format('DD.MM.YYYY') %>"> < </a>
|
||||
<% } %>
|
||||
Training <%= training.datum %>
|
||||
<% if (nextTraining) { %>
|
||||
<a href="/?date=<%= moment(nextTraining.datum).format('DD.MM.YYYY') %>"> > </a>
|
||||
<% } %>
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
<% if (training) { %>
|
||||
<h2> Geräte </h2>
|
||||
<ul>
|
||||
|
@ -20,11 +20,16 @@
|
|||
<li>Riege 4: <%= training.geraet_riege_4_name %></li>
|
||||
<li>Riege 5: <%= training.geraet_riege_5_name %></li>
|
||||
</ul>
|
||||
Aufwärmen:
|
||||
|
||||
<div id="aufwaermen">
|
||||
<h3>Aufwärmen:</h3>
|
||||
|
||||
<% if (training.aufwaermleiter_name) { %>
|
||||
<%= training.aufwaermleiter_name %>
|
||||
<p><strong>Leitung:</strong>
|
||||
<%= training.aufwaermleiter_name %></p>
|
||||
<% } else { %>
|
||||
<form method="POST" action="/update-leader">
|
||||
<p><strong>Leitung:</strong>
|
||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||
<input type="hidden" name="type" value="aufwaermleiter">
|
||||
<select name="leaderId">
|
||||
|
@ -33,35 +38,36 @@
|
|||
<% }) %>
|
||||
</select>
|
||||
<button type="submit">Speichern</button>
|
||||
</form>
|
||||
</form></p>
|
||||
<% } %>
|
||||
</br>
|
||||
|
||||
<% if (training.aufwaermen_name) { %>
|
||||
<%= training.aufwaermen_name %>
|
||||
<p><strong>Spiel:</strong>
|
||||
<%= training.aufwaermen_name %></p>
|
||||
<% } else { %>
|
||||
<form method="POST" action="/update-training">
|
||||
<p><strong>Spiel:</strong>
|
||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||
<input type="hidden" name="type" value="aufwaermen">
|
||||
<select name="spielId">
|
||||
<datalist id="spiele">
|
||||
<% 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>
|
||||
</form>
|
||||
</form></p>
|
||||
<% } %>
|
||||
<form method="POST" action="/add-spiel">
|
||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||
<input type="hidden" name="type" value="aufwaermen">
|
||||
<input type="text" name="aufwaermenName" placeholder="Neues Aufwärmen">
|
||||
<button type="submit">Hinzufügen</button>
|
||||
</form>
|
||||
</div>
|
||||
<div id="spiel">
|
||||
<h3>Spiel: </h3>
|
||||
|
||||
Spiel:
|
||||
<% if (training.spielleiter_name) { %>
|
||||
<%= training.spielleiter_name %>
|
||||
<p><strong>Leitung:</strong>
|
||||
<%= training.spielleiter_name %></p>
|
||||
<% } else { %>
|
||||
<form method="POST" action="/update-leader">
|
||||
<p><strong>Leitung:</strong>
|
||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||
<input type="hidden" name="type" value="spielleiter">
|
||||
<select name="leaderId">
|
||||
|
@ -70,31 +76,42 @@
|
|||
<% }) %>
|
||||
</select>
|
||||
<button type="submit">Speichern</button>
|
||||
</form>
|
||||
</form></p>
|
||||
<% } %>
|
||||
|
||||
</br>
|
||||
<!-- Für Spiel -->
|
||||
|
||||
<% if (training.spiel_name) { %>
|
||||
<p><strong>Spiel:</strong>
|
||||
<%= training.spiel_name %>
|
||||
<% } else { %>
|
||||
|
||||
<form method="POST" action="/update-training">
|
||||
<p><strong>Spiel:</strong>
|
||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||
<input type="hidden" name="type" value="spiel">
|
||||
<select name="spielId">
|
||||
<% spielCandidates.forEach(candidate => { %>
|
||||
<option value="<%= candidate.id %>"><%= candidate.name %> (<%= Math.floor(candidate.weeks_since_last) %> Wochen)</option>
|
||||
<datalist id="spiele">
|
||||
<% aufwaermenCandidates.forEach(candidate => { %>
|
||||
<option value="<%= candidate.name %>"><%= candidate.name %></option>
|
||||
<% }) %>
|
||||
</select>
|
||||
</datalist>
|
||||
<input list="spiele" id="spielName" name="spielName">
|
||||
<button type="submit">Speichern</button>
|
||||
</form>
|
||||
|
||||
<% } %>
|
||||
<form method="POST" action="/add-spiel">
|
||||
<input type="hidden" name="trainingId" value="<%= training.id %>">
|
||||
<input type="hidden" name="type" value="spiel">
|
||||
<input type="text" name="spielName" placeholder="Neues Spiel">
|
||||
<button type="submit">Hinzufügen</button>
|
||||
</div>
|
||||
|
||||
<form method="GET" action="/">
|
||||
<label for="training-date">Datum wählen:</label>
|
||||
<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>
|
||||
|
||||
<% } else { %>
|
||||
<p>Kein Training gefunden für das ausgewählte Datum.</p>
|
||||
<% } %>
|
||||
|
|
Loading…
Reference in New Issue