From 6ce64b5e9560a5da67a2106008a28e9fee73d1ba Mon Sep 17 00:00:00 2001 From: rui hildt Date: Mon, 4 May 2020 18:47:05 +0200 Subject: [PATCH] Add CRUD operations for participant --- api/models/participantModel.js | 63 ++++++++++++++++ api/routes/participantRoute.js | 72 +++++++++++++++++++ api/server.js | 2 + data/migrations/20200502193341_participant.js | 20 +++--- .../migrations/20200502193443_availibility.js | 9 +-- 5 files changed, 151 insertions(+), 15 deletions(-) create mode 100644 api/models/participantModel.js create mode 100644 api/routes/participantRoute.js diff --git a/api/models/participantModel.js b/api/models/participantModel.js new file mode 100644 index 0000000..435ee9a --- /dev/null +++ b/api/models/participantModel.js @@ -0,0 +1,63 @@ +const db = require('../../data/db'); + +module.exports = { + addParticipant, + getParticipantById, + updateParticipant, + deleteParticipant, + // getMeetingsByParticipantId, +}; + +function addParticipant(data) { + return db('participant') + .insert(data) + .returning([ + 'account_id', + 'meeting_id', + 'earliest_time', + 'latest_time', + 'quorum', + 'mandatory', + 'host', + 'answered', + 'timezone', + ]); +} + +function updateParticipant(data, account_id, meeting_id) { + return db('participant') + .where({ account_id, meeting_id }) + .update(data) + .returning([ + 'account_id', + 'meeting_id', + 'earliest_time', + 'latest_time', + 'quorum', + 'mandatory', + 'host', + 'answered', + 'timezone', + ]); +} + +function deleteParticipant(id) { + return db('participant').where({ id }).del(); +} + +function getParticipantById(account_id, meeting_id) { + return db('participant') + .where({ account_id, meeting_id }) + .first() + .select( + 'account_id', + 'meeting_id', + 'earliest_time', + 'latest_time', + 'quorum', + 'mandatory', + 'host', + 'answered', + 'timezone', + ); +} diff --git a/api/routes/participantRoute.js b/api/routes/participantRoute.js new file mode 100644 index 0000000..69d5b50 --- /dev/null +++ b/api/routes/participantRoute.js @@ -0,0 +1,72 @@ +let express = require('express'); +let router = express.Router(); + +let Participant = require('../models/participantModel'); + +router.post('/', 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', 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', 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.', + error, + }); + } +}); + +router.get('/:account_id-:meeting_id', async (req, res) => { + const { account_id, meeting_id } = req.params; + + try { + const participant = await Participant.getParticipantById( + account_id, + meeting_id, + ); + res.status(200).json(participant); + } catch (error) { + res.status(500).json({ message: "Participant doesn't exist.", error }); + } +}); + +module.exports = router; diff --git a/api/server.js b/api/server.js index 24abc18..4d850b3 100644 --- a/api/server.js +++ b/api/server.js @@ -5,6 +5,7 @@ require('dotenv').config(); const accountsRoute = require('./routes/accountRoute'); const meetingsRoute = require('./routes/meetingRoute'); +const participantsRoute = require('./routes/participantRoute'); const server = express(); @@ -14,6 +15,7 @@ server.use(helmet()); server.use('/api/accounts', accountsRoute); server.use('/api/meetings', meetingsRoute); +server.use('/api/participants', participantsRoute); server.get('/', (req, res) => res diff --git a/data/migrations/20200502193341_participant.js b/data/migrations/20200502193341_participant.js index a06bf82..13c58d0 100644 --- a/data/migrations/20200502193341_participant.js +++ b/data/migrations/20200502193341_participant.js @@ -1,18 +1,16 @@ exports.up = (knex) => { return knex.schema.createTable('participant', (table) => { - table.increments('id').primary(); + table.integer('account_id').unsigned(); + table.uuid('meeting_id').unsigned(); table - .integer('account_id') - .unsigned() - .notNullable() - .references('account.id') - .onDelete('cascade'); + .foreign('meeting_id') + .references('meeting.id') + .onDelete('cascade'); table - .uuid('meeting_id') - .unsigned() - .notNullable() - .references('meeting.id') - .onDelete('cascade'); + .foreign('account_id') + .references('account.id') + .onDelete('cascade'); + table.primary(['meeting_id','account_id']); table.time('earliest_time'); table.time('latest_time'); table.boolean('quorum'); diff --git a/data/migrations/20200502193443_availibility.js b/data/migrations/20200502193443_availibility.js index a3e628e..8963283 100644 --- a/data/migrations/20200502193443_availibility.js +++ b/data/migrations/20200502193443_availibility.js @@ -1,11 +1,12 @@ exports.up = (knex) => { return knex.schema.createTable('availibility', (table) => { table.increments('id').primary(); + table.uuid('meeting_id').unsigned(); + table.integer('account_id').unsigned(); table - .integer('participant_id') - .unsigned() - .notNullable() - .references('participant.id') + .foreign(['account_id', 'meeting_id']) + .references(['account_id', 'meeting_id']) + .on('participant') .onDelete('cascade'); table .integer('possible_date_id')