Update participant model and routes

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

View File

@ -10,7 +10,7 @@ function addAvailability(data) {
.insert(data) .insert(data)
.returning([ .returning([
'id', 'id',
'account_id', 'participant_id',
'meeting_id', 'meeting_id',
'possible_date_id', 'possible_date_id',
'preference', 'preference',

View File

@ -12,6 +12,8 @@ function addParticipant(data) {
return db('participant') return db('participant')
.insert(data) .insert(data)
.returning([ .returning([
'id',
'email',
'account_id', 'account_id',
'meeting_id', 'meeting_id',
'quorum', 'quorum',
@ -21,11 +23,13 @@ function addParticipant(data) {
]); ]);
} }
function updateParticipant(data, account_id, meeting_id) { function updateParticipant(data, id) {
return db('participant') return db('participant')
.where({ account_id, meeting_id }) .where({ id })
.update(data) .update(data)
.returning([ .returning([
'id',
'email',
'account_id', 'account_id',
'meeting_id', 'meeting_id',
'quorum', 'quorum',
@ -35,15 +39,17 @@ function updateParticipant(data, account_id, meeting_id) {
]); ]);
} }
function deleteParticipant(account_id, meeting_id) { function deleteParticipant(id) {
return db('participant').where({ account_id, meeting_id }).del(); return db('participant').where({id }).del();
} }
function getParticipantById(account_id, meeting_id) { function getParticipantById(id) {
return db('participant') return db('participant')
.where({ account_id, meeting_id }) .where({ id })
.first() .first()
.select( .select(
'id',
'email',
'account_id', 'account_id',
'meeting_id', 'meeting_id',
'quorum', 'quorum',

View File

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

View File

@ -1,13 +1,10 @@
const Participant = require('../api/models/participantModel'); const Participant = require('../api/models/participantModel');
async function validateParticipantID(req, res, next) { async function validateParticipantID(req, res, next) {
const { account_id, meeting_id } = req.params; const { id } = req.params;
try { try {
const participant = await Participant.getParticipantById( const participant = await Participant.getParticipantById(id);
account_id,
meeting_id,
);
if (typeof participant == 'undefined') { if (typeof participant == 'undefined') {
res.status(404).json({ res.status(404).json({
message: `Participant with id ${id} doesn't exist.`, message: `Participant with id ${id} doesn't exist.`,
@ -17,7 +14,7 @@ async function validateParticipantID(req, res, next) {
} }
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
message: `Failed to get participant with id ${account_id}-${meeting_id}.`, message: `Failed to get participant with id ${id}`,
error, error,
}); });
} }