Update participant model and routes

This commit is contained in:
2020-08-28 14:08:13 +02:00
parent 9cdf4eedd2
commit d59a562fe2
4 changed files with 36 additions and 39 deletions

View File

@@ -1,12 +1,16 @@
const express = require('express');
const router = express.Router();
const { v4: uuidv4 } = require('uuid');
const { authenticate } = require('../../middlewares/authenticate');
const { validateParticipantID } = require('../../middlewares/validateParticipantID');
const {
validateParticipantID,
} = require('../../middlewares/validateParticipantID');
const Participant = require('../models/participantModel');
router.post('/', authenticate, async (req, res) => {
const data = { ...req.body };
router.post('/:id', authenticate, async (req, res) => {
id = uuidv4();
const data = { id, ...req.body };
try {
const [participant] = await Participant.addParticipant(data);
@@ -19,16 +23,12 @@ router.post('/', authenticate, async (req, res) => {
}
});
router.put('/:account_id-:meeting_id', authenticate, validateParticipantID, async (req, res) => {
router.put('/:id', authenticate, validateParticipantID, async (req, res) => {
const data = { ...req.body };
const { account_id, meeting_id } = req.params;
const { id } = req.params;
try {
const participant = await Participant.updateParticipant(
data,
account_id,
meeting_id,
);
const participant = await Participant.updateParticipant(data, id);
res.status(200).json(...participant);
} catch (error) {
res.status(500).json({
@@ -38,33 +38,27 @@ router.put('/:account_id-:meeting_id', authenticate, validateParticipantID, asyn
}
});
router.delete('/:account_id-:meeting_id', authenticate, validateParticipantID, async (req, res) => {
const { account_id, meeting_id } = req.params;
router.delete('/:id', authenticate, validateParticipantID, async (req, res) => {
const { id } = req.params;
try {
const participant = await Participant.deleteParticipant(
account_id,
meeting_id,
);
const participant = await Participant.deleteParticipant(id);
res.status(200).json({
message: `Participant with id ${account_id}-${meeting_id} successfully deleted.`,
message: `Participant with id ${id} successfully deleted.`,
});
} catch (error) {
res.status(500).json({
message: `Failed to delete participant with id ${account_id}-${meeting_id}.`,
message: `Failed to delete participant with id ${id}.`,
error,
});
}
});
router.get('/:account_id-:meeting_id', authenticate, async (req, res) => {
const { account_id, meeting_id } = req.params;
router.get('/:id', authenticate, async (req, res) => {
const { id } = req.params;
try {
const participant = await Participant.getParticipantById(
account_id,
meeting_id,
);
const participant = await Participant.getParticipantById(id);
if (typeof participant == 'undefined') {
res.status(404).json({
message: `Participant with id ${id} doesn't exist.`,
@@ -74,7 +68,7 @@ router.get('/:account_id-:meeting_id', authenticate, async (req, res) => {
}
} catch (error) {
res.status(500).json({
message: `Failed to get participant with id ${account_id}-${meeting_id}.`,
message: `Failed to get participant with id ${id}.`,
error,
});
}