diff --git a/api/models/availabilityModel.js b/api/models/availabilityModel.js index f12db59..b256715 100644 --- a/api/models/availabilityModel.js +++ b/api/models/availabilityModel.js @@ -10,7 +10,7 @@ function addAvailability(data) { .insert(data) .returning([ 'id', - 'account_id', + 'participant_id', 'meeting_id', 'possible_date_id', 'preference', diff --git a/api/models/participantModel.js b/api/models/participantModel.js index a895283..2624846 100644 --- a/api/models/participantModel.js +++ b/api/models/participantModel.js @@ -12,6 +12,8 @@ function addParticipant(data) { return db('participant') .insert(data) .returning([ + 'id', + 'email', 'account_id', 'meeting_id', 'quorum', @@ -21,11 +23,13 @@ function addParticipant(data) { ]); } -function updateParticipant(data, account_id, meeting_id) { +function updateParticipant(data, id) { return db('participant') - .where({ account_id, meeting_id }) + .where({ id }) .update(data) .returning([ + 'id', + 'email', 'account_id', 'meeting_id', 'quorum', @@ -35,15 +39,17 @@ function updateParticipant(data, account_id, meeting_id) { ]); } -function deleteParticipant(account_id, meeting_id) { - return db('participant').where({ account_id, meeting_id }).del(); +function deleteParticipant(id) { + return db('participant').where({id }).del(); } -function getParticipantById(account_id, meeting_id) { +function getParticipantById(id) { return db('participant') - .where({ account_id, meeting_id }) + .where({ id }) .first() .select( + 'id', + 'email', 'account_id', 'meeting_id', 'quorum', diff --git a/api/routes/participantRoute.js b/api/routes/participantRoute.js index ad84250..1ea1112 100644 --- a/api/routes/participantRoute.js +++ b/api/routes/participantRoute.js @@ -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, }); } diff --git a/middlewares/validateParticipantID.js b/middlewares/validateParticipantID.js index de9e25b..1ace3f8 100644 --- a/middlewares/validateParticipantID.js +++ b/middlewares/validateParticipantID.js @@ -1,13 +1,10 @@ const Participant = require('../api/models/participantModel'); async function validateParticipantID(req, res, next) { - const { account_id, meeting_id } = req.params; + 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.`, @@ -17,10 +14,10 @@ async function validateParticipantID(req, res, next) { } } 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, }); } } -exports.validateParticipantID = validateParticipantID; \ No newline at end of file +exports.validateParticipantID = validateParticipantID;