Update availibility migration and add basic crud

This commit is contained in:
rui hildt 2020-05-05 11:54:02 +02:00
parent cfedf47568
commit 57b66883da
4 changed files with 59 additions and 3 deletions

View File

@ -0,0 +1,16 @@
const db = require('../../data/db');
module.exports = {
addAvailibility,
deleteAvailibility,
};
function addAvailibility(data) {
return db('possible_date')
.insert(data)
.returning(['id', 'meeting_id', 'possible_date']);
}
function deleteAvailibility(id) {
return db('possible_date').where({ id }).del();
}

View File

@ -0,0 +1,36 @@
let express = require('express');
let router = express.Router();
let Availibility = require('../models/availibilityModel');
router.post('/', async (req, res) => {
const data = { ...req.body };
try {
const [availibility] = await Availibility.addAvailibility(data);
res.status(201).json(availibility);
} catch (error) {
res.status(500).json({
message: 'Failed to add a new availibility.',
error,
});
}
});
router.delete('/:possible_date_id', async (req, res) => {
const id = req.params.id;
try {
const availibility = await Availibility.deleteAvailibility(id);
res.status(200).json({
message: `Availibility with id ${id} successfully deleted.`,
});
} catch (error) {
res.status(500).json({
message: `Failed to delete availibility with id ${id}.`,
error,
});
}
});
module.exports = router;

View File

@ -7,6 +7,7 @@ const accountsRoute = require('./routes/accountRoute');
const meetingsRoute = require('./routes/meetingRoute');
const participantsRoute = require('./routes/participantRoute');
const possibleDatesRoute = require('./routes/possibleDateRoute');
const availibilityRoute = require('./routes/availibilityRoute');
const server = express();
@ -18,6 +19,7 @@ server.use('/api/accounts', accountsRoute);
server.use('/api/meetings', meetingsRoute);
server.use('/api/participants', participantsRoute);
server.use('/api/possible-dates', possibleDatesRoute);
server.use('/api/availibility', availibilityRoute);
server.get('/', (req, res) =>
res

View File

@ -1,6 +1,8 @@
exports.up = (knex) => {
return knex.schema.createTable('availibility', (table) => {
table.increments('id').primary();
// Reference to participant composite primary key
table.uuid('meeting_id').unsigned();
table.integer('account_id').unsigned();
table
@ -8,10 +10,10 @@ exports.up = (knex) => {
.references(['account_id', 'meeting_id'])
.on('participant')
.onDelete('cascade');
table.integer('possible_date_id');
table
.integer('possible_date_id')
.unsigned()
.notNullable()
.foreign('possible_date_id')
.references('possible_date.id')
.onDelete('cascade');
table.boolean('preference').notNullable();