Skip to content

Commit

Permalink
implement olympiks endpoints and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyleal committed Aug 22, 2024
1 parent 2d2d7af commit f112f36
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
52 changes: 52 additions & 0 deletions server/src/controllers/OlympikEventController.js
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 });
}
};
143 changes: 143 additions & 0 deletions server/src/routes/froshRoutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');

const FroshController = require('../controllers/FroshController');
const OlympikEventController = require('../controllers/OlympikEventController');
const checkLoggedIn = require('../middlewares/checkLoggedIn');
const checkUserType = require('../middlewares/checkUserType');
const hasAuthScopes = require('../middlewares/hasAuthScopes');
Expand Down Expand Up @@ -75,4 +76,146 @@ router.post(
FroshController.reassignFrosh,
);

/**
* @swagger
* /frosh/olympiks-signup:
* post:
* summary: Sign up for an Olympik event
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* eventId:
* type: string
* description: The ID of the event to sign up for
* discipline:
* type: string
* enum: [MSE, Mech, CivMin, Indy, TrackOne, ECE, EngSci, Chem]
* description: The discipline chosen for the event
* responses:
* '200':
* description: Successfully signed up for the event
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Signed up successfully!
* '400':
* description: No spots available for this event
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: No spots available for this event.
* '404':
* description: Event not found
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Event not found
*/
router.post('/olympiks-signup', checkLoggedIn, OlympikEventController.signupForEvent);

/**
* @swagger
* /frosh/olympiks-update/{id}:
* put:
* summary: Update the number of spots available for an Olympik event
* parameters:
* - in: path
* name: id
* required: true
* description: The ID of the event to update
* schema:
* type: string
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* maxSpots:
* type: object
* properties:
* MSE:
* type: number
* Mech:
* type: number
* CivMin:
* type: number
* Indy:
* type: number
* TrackOne:
* type: number
* ECE:
* type: number
* EngSci:
* type: number
* Chem:
* type: number
* currentSpots:
* type: object
* properties:
* MSE:
* type: number
* Mech:
* type: number
* CivMin:
* type: number
* Indy:
* type: number
* TrackOne:
* type: number
* ECE:
* type: number
* EngSci:
* type: number
* Chem:
* type: number
* responses:
* '200':
* description: Successfully updated the event
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Event updated successfully!
* event:
* type: object
* $ref: '#/components/schemas/OlympikEvent'
* '404':
* description: Event not found
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Event not found
*/
router.put(
'/olympiks-update/:id',
checkLoggedIn,
hasAuthScopes(['admin:all']),
OlympikEventController.updateEventSpots,
);

module.exports = router;

0 comments on commit f112f36

Please sign in to comment.