-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement olympiks endpoints and controller
- Loading branch information
1 parent
2d2d7af
commit f112f36
Showing
2 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const OlympikEvent = require('../models/OlympiksEventModel'); | ||
|
||
exports.signupForEvent = async (req, res) => { | ||
const { eventId, discipline } = req.body; | ||
|
||
try { | ||
const event = await OlympikEvent.findById(eventId); | ||
if (!event) { | ||
return res.status(404).json({ message: 'Event not found' }); | ||
} | ||
|
||
const currentField = `current${discipline}`; | ||
const maxField = `max${discipline}`; | ||
|
||
if (event[currentField] >= event[maxField]) { | ||
return res.status(400).json({ message: 'No spots lef for this event.' }); | ||
} | ||
|
||
event[currentField] += 1; | ||
await event.save(); | ||
|
||
return res.status(200).json({ message: 'Signed up successfully!' }); | ||
} catch (error) { | ||
return res.status(500).json({ message: 'An error occurred', error }); | ||
} | ||
}; | ||
|
||
exports.updateEventSpots = async (req, res) => { | ||
const { id } = req.params; | ||
const { maxSpots, currentSpots } = req.body; | ||
|
||
try { | ||
const event = await OlympikEvent.findById(id); | ||
if (!event) { | ||
return res.status(404).json({ message: 'Event not found' }); | ||
} | ||
|
||
// udate spots | ||
for (const discipline in maxSpots) { | ||
event[`max${discipline}`] = maxSpots[discipline]; | ||
} | ||
for (const discipline in currentSpots) { | ||
event[`current${discipline}`] = currentSpots[discipline]; | ||
} | ||
|
||
await event.save(); | ||
|
||
return res.status(200).json({ message: 'Event updated successfully!', event }); | ||
} catch (error) { | ||
return res.status(500).json({ message: 'An error occurred', error }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters