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)
.returning([
'id',
'account_id',
'participant_id',
'meeting_id',
'possible_date_id',
'preference',

View File

@ -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',

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,
});
}

View File

@ -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;
exports.validateParticipantID = validateParticipantID;