92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
const { Worship, EventPlace, Sequelize } = require('../models');
|
|
const { Op, fn, literal } = require('sequelize'); // Importieren Sie die Operatoren von Sequelize
|
|
|
|
exports.getAllWorships = async (req, res) => {
|
|
try {
|
|
const worships = await Worship.findAll({
|
|
where: {
|
|
date: {
|
|
[Op.gt]: literal("DATE_SUB(NOW(), INTERVAL 4 WEEK)")
|
|
},
|
|
},
|
|
order: [
|
|
['date', 'DESC']
|
|
],
|
|
});
|
|
res.status(200).json(worships);
|
|
} catch (error) {
|
|
res.status(500).json({ message: 'Fehler beim Abrufen der Gottesdienste' });
|
|
}
|
|
};
|
|
|
|
exports.createWorship = async (req, res) => {
|
|
try {
|
|
const worship = await Worship.create(req.body);
|
|
res.status(201).json(worship);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).json({ message: 'Fehler beim Erstellen des Gottesdienstes' });
|
|
}
|
|
};
|
|
|
|
exports.updateWorship = async (req, res) => {
|
|
try {
|
|
const worship = await Worship.findByPk(req.params.id);
|
|
if (worship) {
|
|
await worship.update(req.body);
|
|
res.status(200).json(worship);
|
|
} else {
|
|
res.status(404).json({ message: 'Gottesdienst nicht gefunden' });
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ message: 'Fehler beim Aktualisieren des Gottesdienstes' });
|
|
}
|
|
};
|
|
|
|
exports.deleteWorship = async (req, res) => {
|
|
try {
|
|
const worship = await Worship.findByPk(req.params.id);
|
|
if (worship) {
|
|
await worship.destroy();
|
|
res.status(200).json({ message: 'Gottesdienst erfolgreich gelöscht' });
|
|
} else {
|
|
res.status(404).json({ message: 'Gottesdienst nicht gefunden' });
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ message: 'Fehler beim Löschen des Gottesdienstes' });
|
|
}
|
|
};
|
|
|
|
exports.getFilteredWorships = async (req, res) => {
|
|
const { location, order } = req.query;
|
|
const where = {};
|
|
if (order.trim() === '') {
|
|
order = 'date DESC';
|
|
}
|
|
const locations = JSON.parse(location);
|
|
if (location && locations.length > 0) {
|
|
where.eventPlaceId = {
|
|
[Sequelize.Op.in]: locations
|
|
}
|
|
}
|
|
|
|
where.date = {
|
|
[Op.gte]: fn('CURDATE'),
|
|
};
|
|
|
|
try {
|
|
const worships = await Worship.findAll({
|
|
where,
|
|
include: {
|
|
model: EventPlace,
|
|
as: 'eventPlace',
|
|
},
|
|
order: [order.split(' ')],
|
|
});
|
|
res.status(200).json(worships);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).json({ message: 'Fehler beim Abrufen der gefilterten Gottesdienste' });
|
|
}
|
|
};
|