const { Worship, EventPlace } = require('../models'); const { Op } = require('sequelize'); // Importieren Sie die Operatoren von Sequelize exports.getAllWorships = async (req, res) => { try { const worships = await Worship.findAll(); 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, orderBy } = req.query; const where = {}; if (location && location !== '-1') { where.eventPlaceId = location; } where.date = { [Op.gte]: new Date(), // Only include events from today onwards }; try { const worships = await Worship.findAll({ where, include: { model: EventPlace, as: 'eventPlace', }, order: [orderBy.split(' ')], }); res.status(200).json(worships); } catch (error) { console.log(error); res.status(500).json({ message: 'Fehler beim Abrufen der gefilterten Gottesdienste' }); } };