-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy patheventControllers.js
More file actions
26 lines (24 loc) · 986 Bytes
/
eventControllers.js
File metadata and controls
26 lines (24 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const Event = require('../models/eventSchema');
// fetch 4 most recently updated events
exports.getLatestEvents = async (req, res) => {
try{
const latestEvents = await Event.find({})
.sort({updatedAt: -1})
.limit(4)
.select('title updatedAt schedule.venue status');
if(!latestEvents){
return res.status(404).json({message: "No events are created"});
}
const formatedEvents =latestEvents.map(event=>({
id: event._id,
title: event.title,
date: event.updatedAt?.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }),
venue: (event.schedule && event.schedule.venue) ? event.schedule.venue : 'TBA',
status: event.status || 'TBD'
}))
res.status(200).json(formatedEvents);
} catch (error) {
console.error('Error fetching latest events:', error);
res.status(500).json({ message: 'Server error' });
}
};