backend/api/routes/participantRoute.js

84 lines
2.1 KiB
JavaScript

const express = require('express');
const router = express.Router();
const { authenticate } = require('../../middlewares/authenticate');
const { validateParticipantID } = require('../../middlewares/validateParticipantID');
const Participant = require('../models/participantModel');
router.post('/', authenticate, async (req, res) => {
const data = { ...req.body };
try {
const [participant] = await Participant.addParticipant(data);
res.status(201).json(participant);
} catch (error) {
res.status(500).json({
message: 'Failed to add new participant.',
error,
});
}
});
router.put('/:account_id-:meeting_id', authenticate, validateParticipantID, async (req, res) => {
const data = { ...req.body };
const { account_id, meeting_id } = req.params;
try {
const participant = await Participant.updateParticipant(
data,
account_id,
meeting_id,
);
res.status(200).json(...participant);
} catch (error) {
res.status(500).json({
message: 'Failed to update participant.',
error,
});
}
});
router.delete('/:account_id-:meeting_id', authenticate, validateParticipantID, async (req, res) => {
const { account_id, meeting_id } = req.params;
try {
const participant = await Participant.deleteParticipant(
account_id,
meeting_id,
);
res.status(200).json({
message: `Participant with id ${account_id}-${meeting_id} successfully deleted.`,
});
} catch (error) {
res.status(500).json({
message: `Failed to delete participant with id ${account_id}-${meeting_id}.`,
error,
});
}
});
router.get('/:account_id-:meeting_id', authenticate, async (req, res) => {
const { account_id, meeting_id } = req.params;
try {
const participant = await Participant.getParticipantById(
account_id,
meeting_id,
);
if (typeof participant == 'undefined') {
res.status(404).json({
message: `Participant with id ${id} doesn't exist.`,
});
} else {
res.status(200).json(participant);
}
} catch (error) {
res.status(500).json({
message: `Failed to get participant with id ${account_id}-${meeting_id}.`,
error,
});
}
});
module.exports = router;