Add CRUD operations for participant

This commit is contained in:
rui hildt 2020-05-04 18:47:05 +02:00
parent f48244292c
commit 6ce64b5e95
5 changed files with 151 additions and 15 deletions

View File

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

View File

@ -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;

View File

@ -5,6 +5,7 @@ require('dotenv').config();
const accountsRoute = require('./routes/accountRoute'); const accountsRoute = require('./routes/accountRoute');
const meetingsRoute = require('./routes/meetingRoute'); const meetingsRoute = require('./routes/meetingRoute');
const participantsRoute = require('./routes/participantRoute');
const server = express(); const server = express();
@ -14,6 +15,7 @@ server.use(helmet());
server.use('/api/accounts', accountsRoute); server.use('/api/accounts', accountsRoute);
server.use('/api/meetings', meetingsRoute); server.use('/api/meetings', meetingsRoute);
server.use('/api/participants', participantsRoute);
server.get('/', (req, res) => server.get('/', (req, res) =>
res res

View File

@ -1,18 +1,16 @@
exports.up = (knex) => { exports.up = (knex) => {
return knex.schema.createTable('participant', (table) => { return knex.schema.createTable('participant', (table) => {
table.increments('id').primary(); table.integer('account_id').unsigned();
table.uuid('meeting_id').unsigned();
table table
.integer('account_id') .foreign('meeting_id')
.unsigned()
.notNullable()
.references('account.id')
.onDelete('cascade');
table
.uuid('meeting_id')
.unsigned()
.notNullable()
.references('meeting.id') .references('meeting.id')
.onDelete('cascade'); .onDelete('cascade');
table
.foreign('account_id')
.references('account.id')
.onDelete('cascade');
table.primary(['meeting_id','account_id']);
table.time('earliest_time'); table.time('earliest_time');
table.time('latest_time'); table.time('latest_time');
table.boolean('quorum'); table.boolean('quorum');

View File

@ -1,11 +1,12 @@
exports.up = (knex) => { exports.up = (knex) => {
return knex.schema.createTable('availibility', (table) => { return knex.schema.createTable('availibility', (table) => {
table.increments('id').primary(); table.increments('id').primary();
table.uuid('meeting_id').unsigned();
table.integer('account_id').unsigned();
table table
.integer('participant_id') .foreign(['account_id', 'meeting_id'])
.unsigned() .references(['account_id', 'meeting_id'])
.notNullable() .on('participant')
.references('participant.id')
.onDelete('cascade'); .onDelete('cascade');
table table
.integer('possible_date_id') .integer('possible_date_id')