beautifyed
This commit is contained in:
parent
ef495986a7
commit
264dcebf6c
380
app.js
380
app.js
|
@ -94,7 +94,7 @@ const getTraining = async (selectedDate) => {
|
|||
const today = selectedDate ? new Date(selectedDate) : new Date();
|
||||
const dayOfWeek = today.getDay();
|
||||
|
||||
const result = await pool.query(`
|
||||
const result = await pool.query(`
|
||||
SELECT tr.*, g1.name AS geraet_riege_1_name, g2.name AS geraet_riege_2_name,
|
||||
g3.name AS geraet_riege_3_name, g4.name AS geraet_riege_4_name,
|
||||
g5.name AS geraet_riege_5_name,
|
||||
|
@ -115,27 +115,27 @@ const getTraining = async (selectedDate) => {
|
|||
LIMIT 1
|
||||
`, [today]);
|
||||
|
||||
return result.rows[0];
|
||||
return result.rows[0];
|
||||
};
|
||||
|
||||
// Funktion zum Laden aller Spiele
|
||||
const getAllSpiele = async () => {
|
||||
const result = await pool.query(`
|
||||
const result = await pool.query(`
|
||||
SELECT * FROM spiele ORDER BY name ASC;
|
||||
`);
|
||||
return result.rows;
|
||||
};
|
||||
return result.rows;
|
||||
};
|
||||
|
||||
// Funktion zum Hinzufügen eines neuen Spiels, falls es noch nicht existiert
|
||||
const addNewSpiel = async (spielName) => {
|
||||
try {
|
||||
const result = await pool.query('INSERT INTO spiele (name) VALUES ($1) RETURNING id', [spielName]);
|
||||
return result.rows[0].id;
|
||||
} catch (error) {
|
||||
console.error('Error adding new spiel:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
// Funktion zum Hinzufügen eines neuen Spiels, falls es noch nicht existiert
|
||||
const addNewSpiel = async (spielName) => {
|
||||
try {
|
||||
const result = await pool.query('INSERT INTO spiele (name) VALUES ($1) RETURNING id', [spielName]);
|
||||
return result.rows[0].id;
|
||||
} catch (error) {
|
||||
console.error('Error adding new spiel:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Funktion zum Laden der vier Leute, die am längsten nicht Aufwärmen oder Spiel geleitet haben
|
||||
const getCandidatesForAufwaermleiter = async () => {
|
||||
|
@ -184,23 +184,23 @@ const getCandidatesForSpielleiter = async () => {
|
|||
|
||||
// Funktion zum Laden der Riegenteilnehmer absteigend nach Alter sortiert
|
||||
const getRiegenMitgliederSortedByAge = async () => {
|
||||
const result = await pool.query(`
|
||||
const result = await pool.query(`
|
||||
SELECT r.riegennummer, t.name, t.alter
|
||||
FROM riegen r
|
||||
JOIN teilnehmende t ON r.fremdid_teilnehmende = t.id
|
||||
ORDER BY r.riegennummer, t.alter DESC
|
||||
`);
|
||||
return result.rows;
|
||||
};
|
||||
return result.rows;
|
||||
};
|
||||
|
||||
const getAnwesenheit = function (id, anwesend) {
|
||||
for (let item of anwesend) {
|
||||
if (item.fid_teilnehmer === id) {
|
||||
return true;
|
||||
}
|
||||
const getAnwesenheit = function (id, anwesend) {
|
||||
for (let item of anwesend) {
|
||||
if (item.fid_teilnehmer === id) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// Registrierung
|
||||
app.post('/register', async (req, res) => {
|
||||
|
@ -220,16 +220,16 @@ app.post('/register', async (req, res) => {
|
|||
if (error) {
|
||||
console.error('Error sending email:', error);
|
||||
const message = 'Error sending Mail:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error registering user:', error);
|
||||
const message = 'Error registering user:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -238,7 +238,7 @@ app.post('/login', async (req, res) => {
|
|||
const { username, password } = req.body;
|
||||
|
||||
try {
|
||||
const userResult = await pool.query("SELECT *, CASE WHEN admin_temp IS NOT NULL AND (now() - admin_temp) > interval '22 hours' THEN 'expired' ELSE 'valid' END AS admin_status FROM users WHERE username = $1", [ username]);
|
||||
const userResult = await pool.query("SELECT *, CASE WHEN admin_temp IS NOT NULL AND (now() - admin_temp) > interval '22 hours' THEN 'expired' ELSE 'valid' END AS admin_status FROM users WHERE username = $1", [username]);
|
||||
if (userResult.rows.length > 0) {
|
||||
const user = userResult.rows[0];
|
||||
const match = await bcrypt.compare(password, user.password);
|
||||
|
@ -248,36 +248,36 @@ app.post('/login', async (req, res) => {
|
|||
req.session.userName = user.username;
|
||||
req.session.activeRiege = 1;
|
||||
req.session.activeTab = 'geraete';
|
||||
req.session.message = [ title = '', message = '', type = 'none' ];
|
||||
req.session.message = [title = '', message = '', type = 'none'];
|
||||
log.Info(username + ' ' + password);
|
||||
if (user.admin_status === 'expired') {
|
||||
await pool.query('UPDATE users SET role = $1, admin_temp = NULL WHERE id = $2', ['user', user.id]);
|
||||
req.session.role='user';
|
||||
} else {
|
||||
req.session.role=user.role;
|
||||
}
|
||||
await pool.query('UPDATE users SET role = $1, admin_temp = NULL WHERE id = $2', ['user', user.id]);
|
||||
req.session.role = 'user';
|
||||
} else {
|
||||
req.session.role = user.role;
|
||||
}
|
||||
res.redirect('/training');
|
||||
} else {
|
||||
} else {
|
||||
res.redirect('/freischaltung')
|
||||
}
|
||||
} else {
|
||||
const message = 'Falscher Benutzername oder falsches Passwort';
|
||||
res.render('login', {session: req.session, username, message});
|
||||
res.render('login', { session: req.session, username, message });
|
||||
}
|
||||
} else {
|
||||
const message = 'Falscher Benutzername oder falsches Passwort';
|
||||
res.render('login', {session: req.session, username, message});
|
||||
res.render('login', { session: req.session, username, message });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error logging in:', error);
|
||||
const message = 'Error logging in:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
//Wird angezeigt, wenn ein nicht freigeschalteter User sich anmelden will.
|
||||
app.get('/freischaltung', async (req, res) => {
|
||||
res.render('freischaltung', { session: req.session});
|
||||
res.render('freischaltung', { session: req.session });
|
||||
})
|
||||
|
||||
// Logout
|
||||
|
@ -294,38 +294,40 @@ app.get('/logout', (req, res) => {
|
|||
app.post('/userrights', requireAuth, requireAdmin, async (req, res) => {
|
||||
const { userId, type } = req.body;
|
||||
try {
|
||||
if (type === 'activate'){
|
||||
await pool.query('UPDATE users SET is_active = TRUE WHERE id = $1', [userId]);
|
||||
const userResult = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
|
||||
if (userResult.rows.length > 0) {
|
||||
if (userResult.rows[0].email){
|
||||
const mailOptions = {
|
||||
to: userResult.rows[0].email,
|
||||
from: 'admin@boergmann.it',
|
||||
subject: 'Freischaltung',
|
||||
text: `Hallo ${userResult.rows[0].username}, du wurdest soeben freigeschaltet.`
|
||||
};
|
||||
if (type === 'activate') {
|
||||
await pool.query('UPDATE users SET is_active = TRUE WHERE id = $1', [userId]);
|
||||
const userResult = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
|
||||
if (userResult.rows.length > 0) {
|
||||
if (userResult.rows[0].email) {
|
||||
const mailOptions = {
|
||||
to: userResult.rows[0].email,
|
||||
from: 'admin@boergmann.it',
|
||||
subject: 'Freischaltung',
|
||||
text: `Hallo ${userResult.rows[0].username}, du wurdest soeben freigeschaltet.`
|
||||
};
|
||||
|
||||
transporter.sendMail(mailOptions, (error) => {
|
||||
if (error) {
|
||||
console.error('Error sending email:', error);
|
||||
const message = 'Error sending Mail:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
transporter.sendMail(mailOptions, (error) => {
|
||||
if (error) {
|
||||
console.error('Error sending email:', error);
|
||||
const message = 'Error sending Mail:' + error;
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})}}
|
||||
} else if (type === 'admin') {
|
||||
await pool.query('UPDATE users SET role = $1 WHERE id = $2', ['admin', userId]);
|
||||
}
|
||||
else if (type === 'admint') {
|
||||
await pool.query('UPDATE users SET role = $1, admin_temp = $2 WHERE id = $3', ['admin', moment().toDate() , userId]);
|
||||
} else if (type === 'delete') {
|
||||
await pool.query('DELETE FROM users WHERE id = $1', [userId]);
|
||||
}
|
||||
} else if (type === 'admin') {
|
||||
await pool.query('UPDATE users SET role = $1 WHERE id = $2', ['admin', userId]);
|
||||
}
|
||||
else if (type === 'admint') {
|
||||
await pool.query('UPDATE users SET role = $1, admin_temp = $2 WHERE id = $3', ['admin', moment().toDate(), userId]);
|
||||
} else if (type === 'delete') {
|
||||
await pool.query('DELETE FROM users WHERE id = $1', [userId]);
|
||||
}
|
||||
res.redirect('/admin');
|
||||
} catch (error) {
|
||||
console.error('Error activating user:', error);
|
||||
const message = 'Error activating user:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -338,7 +340,7 @@ app.post('/send-password', async (req, res) => {
|
|||
const user = userResult.rows[0];
|
||||
const token = crypto.randomBytes(20).toString('hex');
|
||||
const resetLink = `http://tkd.boergmann.it/reset-password/${token}`;
|
||||
await pool.query('UPDATE users SET reset_password_token = $1, reset_password_expires = $2 WHERE id = $3', [token, selectedDate = moment().add(1,'d').toDate() , user.id]);
|
||||
await pool.query('UPDATE users SET reset_password_token = $1, reset_password_expires = $2 WHERE id = $3', [token, selectedDate = moment().add(1, 'd').toDate(), user.id]);
|
||||
|
||||
const mailOptions = {
|
||||
to: user.email,
|
||||
|
@ -351,25 +353,25 @@ app.post('/send-password', async (req, res) => {
|
|||
if (err) {
|
||||
console.error('Error sending email:', err);
|
||||
const message = 'Error sending Mail:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
} else {
|
||||
const message = 'Password reset link sent';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const message = 'Email not found';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in forgot-password:', error);
|
||||
const message = 'Error in forgot-password';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/forgot-password', async (req, res) => {
|
||||
res.render('forgot-password', {session: req.session})
|
||||
res.render('forgot-password', { session: req.session })
|
||||
})
|
||||
|
||||
// Passwort zurücksetzen
|
||||
|
@ -381,12 +383,12 @@ app.get('/reset-password/:token', async (req, res) => {
|
|||
res.render('reset-password', { token }); // Stelle sicher, dass es eine reset-password.ejs gibt
|
||||
} else {
|
||||
const message = 'Token ungültig oder abgelaufen';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in reset-password:', error);
|
||||
const message = 'Error in reset-password';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -402,18 +404,18 @@ app.post('/reset-password/:token', async (req, res) => {
|
|||
res.redirect('/login');
|
||||
} else {
|
||||
const message = 'Token ungültig oder abgelaufen';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in reset-password:', error);
|
||||
const message = 'Error in reset-password';
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
// Profilseite
|
||||
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) => {
|
||||
|
@ -430,7 +432,7 @@ app.post('/profile', requireAuth, async (req, res) => {
|
|||
} catch (error) {
|
||||
console.error('Error updating profile:', error);
|
||||
const message = 'Error updating profile:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -451,22 +453,22 @@ app.post('/update-training', requireAuth, async (req, res) => {
|
|||
|
||||
try {
|
||||
if (type === 'spiel') {
|
||||
await pool.query('UPDATE trainings SET spiel = $1 WHERE id = $2', [ spielId, trainingId ]);
|
||||
await pool.query('UPDATE trainings SET spiel = $1 WHERE id = $2', [spielId, trainingId]);
|
||||
} else if (type === 'aufwaermen') {
|
||||
await pool.query('UPDATE trainings SET aufwaermen = $1 WHERE id = $2', [ spielId, trainingId ]);
|
||||
await pool.query('UPDATE trainings SET aufwaermen = $1 WHERE id = $2', [spielId, trainingId]);
|
||||
}
|
||||
|
||||
res.redirect('/training');
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
const message = 'Error:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/update-leader', requireAuth , async (req, res) => {
|
||||
app.post('/update-leader', requireAuth, async (req, res) => {
|
||||
const { trainingId, type, leaderId } = req.body;
|
||||
req.session.activeTab = 'spiel';
|
||||
req.session.activeTab = 'spiel';
|
||||
try {
|
||||
if (type === 'aufwaermleiter') {
|
||||
await pool.query('UPDATE trainings SET aufwaermleiter = $1 WHERE id = $2', [leaderId, trainingId]);
|
||||
|
@ -477,7 +479,7 @@ req.session.activeTab = 'spiel';
|
|||
} catch (error) {
|
||||
console.error(error);
|
||||
const message = 'Error:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -487,19 +489,19 @@ app.get('/admin', requireAuth, requireAdmin, async (req, res) => {
|
|||
res.render('admin', { users: usersResult.rows, session: req.session }); // Stelle sicher, dass es eine admin.ejs gibt
|
||||
});
|
||||
|
||||
app.post('/new-member', requireAuth, requireAdmin ,async(req, res) => {
|
||||
const {vorname, nachname, geburt, riege, adresse} = req.body;
|
||||
app.post('/new-member', requireAuth, requireAdmin, async (req, res) => {
|
||||
const { vorname, nachname, geburt, riege, adresse } = req.body;
|
||||
const name = vorname + ' ' + nachname;
|
||||
console.log(name + ', ' + geburt + ', ' + riege + ', ' + adresse);
|
||||
try {
|
||||
const teilnehmerID = await pool.query('INSERT INTO teilnehmende (name, geburtsdatum, adresse, vorname, nachname) VALUES ($1, $2, $3, $4, $5) RETURNING id', [name, geburt, adresse, vorname, nachname]);
|
||||
console.log(teilnehmerID.rows[0].id);
|
||||
putInRiege(riege, teilnehmerID.rows[0].id);
|
||||
res.redirect('/riege');
|
||||
}catch (error) {
|
||||
const message = 'Error:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
}
|
||||
try {
|
||||
const teilnehmerID = await pool.query('INSERT INTO teilnehmende (name, geburtsdatum, adresse, vorname, nachname) VALUES ($1, $2, $3, $4, $5) RETURNING id', [name, geburt, adresse, vorname, nachname]);
|
||||
console.log(teilnehmerID.rows[0].id);
|
||||
putInRiege(riege, teilnehmerID.rows[0].id);
|
||||
res.redirect('/riege');
|
||||
} catch (error) {
|
||||
const message = 'Error:' + error;
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
})
|
||||
|
||||
// Teilnehmer_innen
|
||||
|
@ -514,7 +516,7 @@ app.get('/training', requireAuth, async (req, res) => {
|
|||
|
||||
|
||||
if (dateParam) {
|
||||
selectedDate = moment(dateParam, 'DD.MM.YYYY').toDate();
|
||||
selectedDate = moment(dateParam, 'DD.MM.YYYY').toDate();
|
||||
} else {
|
||||
selectedDate = moment().toDate();
|
||||
}
|
||||
|
@ -528,53 +530,53 @@ app.get('/training', requireAuth, async (req, res) => {
|
|||
selectetYear = moment(selectedDate).year();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
selectedKW = moment(selectedDate).isoWeek();
|
||||
selectetYear = moment(selectedDate).year();
|
||||
}
|
||||
console.log(selectedKW + ' ' + selectetYear);
|
||||
const training =await getTraining(selectedDate);
|
||||
} else {
|
||||
selectedKW = moment(selectedDate).isoWeek();
|
||||
selectetYear = moment(selectedDate).year();
|
||||
}
|
||||
console.log(selectedKW + ' ' + selectetYear);
|
||||
const training = await getTraining(selectedDate);
|
||||
|
||||
const trainingsResult = await pool.query('SELECT datum FROM trainings ORDER BY datum ASC');
|
||||
const anwesendResult = await pool.query('SELECT * FROM anwesend WHERE fid_training = $1',[ training.id ]);
|
||||
anwesend=anwesendResult.rows;
|
||||
const anwesendResult = await pool.query('SELECT * FROM anwesend WHERE fid_training = $1', [training.id]);
|
||||
anwesend = anwesendResult.rows;
|
||||
const trainingsDates = trainingsResult.rows.map(tr => ({
|
||||
datum: formatDate(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 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;
|
||||
|
||||
// Abrufen der Riegendaten einschließlich der Teilnehmer und deren Altersberechnung
|
||||
const result = await pool.query(`
|
||||
// Abrufen der Riegendaten einschließlich der Teilnehmer und deren Altersberechnung
|
||||
const result = await pool.query(`
|
||||
SELECT r.riegennummer, t.id, t.name, t.vorname, t.nachname, t.geburtsdatum, r.helfer
|
||||
FROM riegen r
|
||||
JOIN teilnehmende t ON r.fremdID_Teilnehmende = t.id
|
||||
ORDER BY r.riegennummer, t.geburtsdatum ASC
|
||||
`);
|
||||
|
||||
// Gruppieren der Riegenteilnehmer nach Riegennummer
|
||||
const riegen = {};
|
||||
result.rows.forEach(row => {
|
||||
const age = calculateAge(row.geburtsdatum);
|
||||
const tnAnwesend = getAnwesenheit(row.id, anwesend);
|
||||
if (!riegen[row.riegennummer]) {
|
||||
riegen[row.riegennummer] = [];
|
||||
}
|
||||
riegen[row.riegennummer].push({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
vorname: row.vorname,
|
||||
nachname: row.nachname,
|
||||
age: age,
|
||||
helfer: row.helfer,
|
||||
anwesend: tnAnwesend
|
||||
});
|
||||
// Gruppieren der Riegenteilnehmer nach Riegennummer
|
||||
const riegen = {};
|
||||
result.rows.forEach(row => {
|
||||
const age = calculateAge(row.geburtsdatum);
|
||||
const tnAnwesend = getAnwesenheit(row.id, anwesend);
|
||||
if (!riegen[row.riegennummer]) {
|
||||
riegen[row.riegennummer] = [];
|
||||
}
|
||||
riegen[row.riegennummer].push({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
vorname: row.vorname,
|
||||
nachname: row.nachname,
|
||||
age: age,
|
||||
helfer: row.helfer,
|
||||
anwesend: tnAnwesend
|
||||
});
|
||||
});
|
||||
|
||||
if (training) {
|
||||
training.datum = formatDate(training.datum);
|
||||
|
@ -604,7 +606,7 @@ app.get('/training', requireAuth, async (req, res) => {
|
|||
} catch (error) {
|
||||
console.error(error);
|
||||
const message = 'Error:' + error;
|
||||
res.render('error', {session: req.session, message});
|
||||
res.render('error', { session: req.session, message });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -638,8 +640,8 @@ app.get('/riege', requireAuth, async (req, res) => {
|
|||
res.render('riegen', { riegen: riegen, session: req.session });
|
||||
} catch (error) {
|
||||
console.error('Error fetching riegen:', error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
res.render('error', {session: req.session});
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.render('error', { session: req.session });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -655,7 +657,7 @@ app.get('/teilnehmer', requireAuth, async (req, res) => {
|
|||
res.render('teilnehmer', { teilnehmende, session: req.session });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.render('error', { session: req.session });
|
||||
}
|
||||
});
|
||||
|
@ -678,19 +680,19 @@ app.get('/mitglied/:id', requireAuth, async (req, res) => {
|
|||
}))
|
||||
res.render('mitglied', {
|
||||
id,
|
||||
mitglied: mitglied[0],
|
||||
numbers,
|
||||
riege,
|
||||
anwesend,
|
||||
session: req.session
|
||||
mitglied: mitglied[0],
|
||||
numbers,
|
||||
riege,
|
||||
anwesend,
|
||||
session: req.session
|
||||
});
|
||||
} else {
|
||||
req.session.message = ['Error', 'Mitglied existiert nicht' , 'error'];
|
||||
req.session.message = ['Error', 'Mitglied existiert nicht', 'error'];
|
||||
res.redirect('/teilnehmer')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in Mitglied:', error);
|
||||
req.session.message = ['Error', 'Mitglied existiert nicht' , 'error'];
|
||||
req.session.message = ['Error', 'Mitglied existiert nicht', 'error'];
|
||||
res.redirect('/teilnehmer');
|
||||
}
|
||||
});
|
||||
|
@ -702,35 +704,35 @@ app.post('/set-riege', requireAuth, async (req, res) => {
|
|||
})
|
||||
|
||||
app.post('/update-mitglied', requireAdmin, async (req, res) => {
|
||||
const { type, id, telid, adresse, name, verbindung, nummer } = req.body;
|
||||
try{
|
||||
if (type=="tel"){
|
||||
console.log('INSERT INTO telefonnummern (fid_teilnehmer, name, verbindung, nummer, stand) VALUES ($1, $2, $3, $4, $5)', [id, name, verbindung, nummer, moment().toDate()]);
|
||||
await pool.query('INSERT INTO telefonnummern (fid_teilnehmer, name, verbindung, nummer, stand) VALUES ($1, $2, $3, $4, $5)', [id, name, verbindung, nummer, moment().toDate()]);
|
||||
} else if (type=="tel-delete"){
|
||||
const { type, id, telid, adresse, name, verbindung, nummer } = req.body;
|
||||
try {
|
||||
if (type == "tel") {
|
||||
console.log('INSERT INTO telefonnummern (fid_teilnehmer, name, verbindung, nummer, stand) VALUES ($1, $2, $3, $4, $5)', [id, name, verbindung, nummer, moment().toDate()]);
|
||||
await pool.query('INSERT INTO telefonnummern (fid_teilnehmer, name, verbindung, nummer, stand) VALUES ($1, $2, $3, $4, $5)', [id, name, verbindung, nummer, moment().toDate()]);
|
||||
} else if (type == "tel-delete") {
|
||||
await pool.query('DELETE FROM telefonnummern WHERE id = $1', [telid]);
|
||||
console.log('delete numer $1', [telid]);
|
||||
} else if (type=="adresse"){
|
||||
} else if (type == "adresse") {
|
||||
await pool.query('UPDATE teilnehmende SET adresse = $1 WHERE id = $2', [adresse, id]);
|
||||
}
|
||||
|
||||
res.redirect('/mitglied/' + id);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.redirect('/mitglied/' + id);
|
||||
}
|
||||
});
|
||||
|
||||
// Login und Registrierung anzeigen
|
||||
app.get('/login', (req, res) => {
|
||||
req.session.message=['', '', 'none'];
|
||||
res.render('login', {session: req.session}); // Stelle sicher, dass es eine login.ejs gibt
|
||||
req.session.message = ['', '', 'none'];
|
||||
res.render('login', { session: req.session }); // Stelle sicher, dass es eine login.ejs gibt
|
||||
});
|
||||
|
||||
// Registrierung
|
||||
app.get('/register', (req, res) => {
|
||||
res.render('register', {session: req.session}); // Stelle sicher, dass es eine register.ejs gibt
|
||||
res.render('register', { session: req.session }); // Stelle sicher, dass es eine register.ejs gibt
|
||||
});
|
||||
|
||||
// Spieleliste
|
||||
|
@ -741,12 +743,12 @@ app.get('/spiele', async (req, res) => {
|
|||
res.render('spiele', { spiele, session: req.session });
|
||||
} catch (error) {
|
||||
console.error('Error in Mitglied:', error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.render('spiele', { spiele, session: req.session });
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/delete-spiel', requireAdmin ,async (req, res) => {
|
||||
app.post('/delete-spiel', requireAdmin, async (req, res) => {
|
||||
const { spielId } = req.body;
|
||||
console.log(spielId);
|
||||
await pool.query('DELETE FROM spiele WHERE id = $1', [spielId]);
|
||||
|
@ -759,38 +761,38 @@ app.get('/spiel/:id', async (req, res) => {
|
|||
try {
|
||||
const spieleResult = await pool.query('SELECT * FROM spiele WHERE id = $1', [id]);
|
||||
if (spieleResult.rows.length > 0) {
|
||||
const spiel = spieleResult.rows[0];
|
||||
res.render('spiel', { spiel, session: req.session });
|
||||
const spiel = spieleResult.rows[0];
|
||||
res.render('spiel', { spiel, session: req.session });
|
||||
} else {
|
||||
req.session.message = ['Error', 'Spiel existiert nicht', 'error'];
|
||||
res.redirect('/spiele');
|
||||
res.redirect('/spiele');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in Spiele:', error);
|
||||
req.session.message = ['Error', 'Spiel existiert nicht' , 'error'];
|
||||
req.session.message = ['Error', 'Spiel existiert nicht', 'error'];
|
||||
res.redirect('/spiele');
|
||||
}
|
||||
})
|
||||
|
||||
// Postseite für Änderungen
|
||||
app.post('/edit-spiel', requireAuth, async (req, res) => {
|
||||
const { material, dauer, regeln, variationen, type, id } = req.body;
|
||||
try {
|
||||
const queryResult = await pool.query('UPDATE spiele set material = $1, regeln = $2, dauer = $3, variationen = $4, type = $5 Where id = $6 ', [material, regeln, dauer, variationen, type, id]);
|
||||
req.session.message = ['Erfolg', 'Das Spiel wurde gespeichert', 'success'];
|
||||
res.redirect('/spiel/' + id);
|
||||
} catch (error) {
|
||||
console.error('Error in edit Spiel:', error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
res.redirect('/spiel/' + id);
|
||||
}
|
||||
const { material, dauer, regeln, variationen, type, id } = req.body;
|
||||
try {
|
||||
const queryResult = await pool.query('UPDATE spiele set material = $1, regeln = $2, dauer = $3, variationen = $4, type = $5 Where id = $6 ', [material, regeln, dauer, variationen, type, id]);
|
||||
req.session.message = ['Erfolg', 'Das Spiel wurde gespeichert', 'success'];
|
||||
res.redirect('/spiel/' + id);
|
||||
} catch (error) {
|
||||
console.error('Error in edit Spiel:', error);
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.redirect('/spiel/' + id);
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
// Startseite
|
||||
app.get('/', (req, res) => {
|
||||
req.session.message = ['', '', 'none']
|
||||
res.render('index', {session: req.session});
|
||||
res.render('index', { session: req.session });
|
||||
});
|
||||
|
||||
// Changelog
|
||||
|
@ -798,11 +800,11 @@ app.get('/changelog', async (req, res) => {
|
|||
try {
|
||||
const changeResult = await pool.query('SELECT * FROM changelog ORDER BY datetime DESC;');
|
||||
const changes = changeResult.rows;
|
||||
req.session.message = ['Erfolg', 'Log gespeichert' , 'success'];
|
||||
req.session.message = ['Erfolg', 'Log gespeichert', 'success'];
|
||||
res.render('changelog', { changes, session: req.session });
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.redirect('/changelog');
|
||||
}
|
||||
})
|
||||
|
@ -816,30 +818,32 @@ app.post('/changelog', requireAdmin, async (req, res) => {
|
|||
const changes = changeResult.rows;
|
||||
res.render('changelog', { changes, session: req.session });
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
res.render('changelog', { changes, session: req.session });
|
||||
}
|
||||
console.error('Error:', error);
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.render('changelog', { changes, session: req.session });
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/anwesend', requireAuth, async (req, res) => {
|
||||
const { anw, trainingId, riege } = req.body;
|
||||
req.session.activeTab = 'anwesend';
|
||||
req.session.message = [title = 'Erfolg', body = 'Die Anwesenheit wurde gespeichert', type = 'success'];
|
||||
try {
|
||||
for (const teilnehmer of anw) {
|
||||
const resultAnw = await pool.query('Select * FROM anwesend WHERE fid_teilnehmer = $1 AND fid_training = $2', [teilnehmer, trainingId]);
|
||||
if (resultAnw.rows.length == 0) {
|
||||
await pool.query('INSERT INTO anwesend (fid_teilnehmer, fid_training) VALUES ($1, $2)', [teilnehmer, trainingId]);
|
||||
}}
|
||||
req.session.activeRiege = riege;
|
||||
res.redirect('/training');
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
req.session.message = ['Error', error , 'error'];
|
||||
res.redirect('/training');
|
||||
}})
|
||||
const { anw, trainingId, riege } = req.body;
|
||||
req.session.activeTab = 'anwesend';
|
||||
req.session.message = [title = 'Erfolg', body = 'Die Anwesenheit wurde gespeichert', type = 'success'];
|
||||
try {
|
||||
for (const teilnehmer of anw) {
|
||||
const resultAnw = await pool.query('Select * FROM anwesend WHERE fid_teilnehmer = $1 AND fid_training = $2', [teilnehmer, trainingId]);
|
||||
if (resultAnw.rows.length == 0) {
|
||||
await pool.query('INSERT INTO anwesend (fid_teilnehmer, fid_training) VALUES ($1, $2)', [teilnehmer, trainingId]);
|
||||
}
|
||||
}
|
||||
req.session.activeRiege = riege;
|
||||
res.redirect('/training');
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
req.session.message = ['Error', error, 'error'];
|
||||
res.redirect('/training');
|
||||
}
|
||||
})
|
||||
|
||||
const server = app.listen(port, '0.0.0.0', () => {
|
||||
log.Info(`Server is running on ${process.env.HOST}:${port}/`);
|
||||
});
|
||||
log.Info(`Server is running on ${process.env.HOST}:${port}/`);
|
||||
});
|
Loading…
Reference in New Issue