180 lines
5.2 KiB
JavaScript
180 lines
5.2 KiB
JavaScript
const { Event, Institution, EventPlace, ContactPerson, EventType } = require('../models');
|
|
const { Op } = require('sequelize');
|
|
const moment = require('moment'); // Import von Moment.js
|
|
|
|
const getAllEvents = async (req, res) => {
|
|
try {
|
|
const events = await Event.findAll({
|
|
include: [
|
|
{ model: Institution, as: 'institution' },
|
|
{ model: EventPlace, as: 'eventPlace' },
|
|
{ model: EventType, as: 'eventType' },
|
|
{ model: ContactPerson, as: 'contactPersons', through: { attributes: [] } }
|
|
]
|
|
});
|
|
res.json(events);
|
|
} catch (error) {
|
|
res.status(500).json({ error: 'Failed to fetch events' });
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
const filterEvents = async (req, res) => {
|
|
try {
|
|
const { id, 'event-places': eventPlaces, 'event-types': eventTypes, display } = req.query;
|
|
const where = {
|
|
[Op.or]: [
|
|
{
|
|
date: {
|
|
[Op.or]: [
|
|
{ [Op.gte]: moment().startOf('day').toDate() },
|
|
{ [Op.eq]: null }
|
|
]
|
|
}
|
|
},
|
|
{ dayOfWeek: { [Op.ne]: null } }
|
|
]
|
|
};
|
|
|
|
if (id === 'all') {
|
|
const events = await Event.findAll({
|
|
where,
|
|
include: [
|
|
{ model: Institution, as: 'institution' },
|
|
{ model: EventPlace, as: 'eventPlace' },
|
|
{ model: EventType, as: 'eventType' },
|
|
{ model: ContactPerson, as: 'contactPersons', through: { attributes: [] } }
|
|
]
|
|
});
|
|
return res.json({ events });
|
|
}
|
|
|
|
if (id === 'home') {
|
|
const events = await Event.findAll({
|
|
where: {
|
|
alsoOnHomepage: 1,
|
|
date: { [Op.gte]: moment().startOf('day').toDate() }
|
|
},
|
|
include: [
|
|
{ model: Institution, as: 'institution' },
|
|
{ model: EventPlace, as: 'eventPlace' },
|
|
{ model: EventType, as: 'eventType' },
|
|
{ model: ContactPerson, as: 'contactPersons', through: { attributes: [] } }
|
|
]
|
|
});
|
|
return res.json({ events });
|
|
}
|
|
|
|
if (!id && !eventPlaces && !eventTypes) {
|
|
return res.json({ events: [], eventPlaces: [], eventTypes: [], contactPersons: [] });
|
|
}
|
|
|
|
if (id) {
|
|
where.id = id;
|
|
}
|
|
|
|
if (eventPlaces) {
|
|
where.event_place_id = {
|
|
[Op.in]: eventPlaces.split('|').map(id => parseInt(id))
|
|
};
|
|
}
|
|
|
|
if (eventTypes) {
|
|
where.eventTypeId = {
|
|
[Op.in]: eventTypes.split('|').map(id => parseInt(id))
|
|
};
|
|
}
|
|
|
|
const events = await Event.findAll({
|
|
where,
|
|
include: [
|
|
{ model: Institution, as: 'institution' },
|
|
{ model: EventPlace, as: 'eventPlace' },
|
|
{ model: EventType, as: 'eventType' },
|
|
{ model: ContactPerson, as: 'contactPersons', through: { attributes: [] } }
|
|
]
|
|
});
|
|
|
|
const displayFields = display ? display.split('|') : [];
|
|
|
|
const filteredEvents = events.map(event => {
|
|
const filteredEvent = { ...event.toJSON() };
|
|
|
|
if (!displayFields.includes('name')) delete filteredEvent.name;
|
|
if (!displayFields.includes('type')) delete filteredEvent.eventType;
|
|
if (!displayFields.includes('place')) delete filteredEvent.eventPlace;
|
|
if (!displayFields.includes('description')) delete filteredEvent.description;
|
|
if (!displayFields.includes('time')) delete filteredEvent.time;
|
|
if (!displayFields.includes('time')) delete filteredEvent.endTime;
|
|
if (!displayFields.includes('contactPerson')) delete filteredEvent.contactPersons;
|
|
if (!displayFields.includes('day')) delete filteredEvent.dayOfWeek;
|
|
if (!displayFields.includes('institution')) delete filteredEvent.institution;
|
|
|
|
return filteredEvent;
|
|
});
|
|
|
|
res.json({ events: filteredEvents });
|
|
} catch (error) {
|
|
res.status(500).json({ error: 'Failed to filter events' });
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
const createEvent = async (req, res) => {
|
|
try {
|
|
const { contactPersonIds, ...eventData } = req.body;
|
|
const event = await Event.create(eventData);
|
|
if (contactPersonIds) {
|
|
await event.setContactPersons(contactPersonIds);
|
|
}
|
|
res.status(201).json(event);
|
|
} catch (error) {
|
|
res.status(500).json({ error: 'Failed to create event' });
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
const updateEvent = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { contactPersonIds, ...eventData } = req.body;
|
|
const event = await Event.findByPk(id);
|
|
if (!event) {
|
|
return res.status(404).json({ error: 'Event not found' });
|
|
}
|
|
await event.update(eventData);
|
|
if (contactPersonIds) {
|
|
await event.setContactPersons(contactPersonIds);
|
|
}
|
|
res.status(200).json(event);
|
|
} catch (error) {
|
|
res.status(500).json({ error: 'Failed to update event' });
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
const deleteEvent = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const deleted = await Event.destroy({
|
|
where: { id: id }
|
|
});
|
|
if (deleted) {
|
|
res.status(204).json();
|
|
} else {
|
|
res.status(404).json({ error: 'Event not found' });
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ error: 'Failed to delete event' });
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
getAllEvents,
|
|
createEvent,
|
|
updateEvent,
|
|
deleteEvent,
|
|
filterEvents
|
|
};
|