Complete crud for accounts and meetings endpoints

This commit is contained in:
rui hildt 2020-05-04 16:02:31 +02:00
parent dc30471806
commit f48244292c
8 changed files with 267 additions and 44 deletions

View File

@ -1,14 +1,69 @@
const db = require('../../data/db'); const db = require('../../data/db');
module.exports = { module.exports = {
addUser, addAccount,
getAccountById,
updateAccount,
deleteAccount,
// getMeetingsByAccountId,
}; };
function addUser(userData) { function addAccount(data) {
// TODO Complete query without providing id return db('account')
// right now if ID is not provided, pg-promise send an erro fo mising column .insert(data)
return db.one( .returning([
'INSERT INTO account VALUES(emptyUpdate, ${username}, ${email}, ${password}, ${timezone}, ${earliest_time}, ${latest_time}) RETURNING *', 'id',
userData, 'username',
); 'email',
'timezone',
'earliest_time',
'latest_time',
]);
}
function updateAccount(data, id) {
return db('account')
.where({ id })
.update(data)
.returning([
'id',
'username',
'email',
'timezone',
'earliest_time',
'latest_time',
]);
}
function deleteAccount(id) {
return db('account').where({ id }).del();
}
// function getMeetingsByAccountId(id) {
// // Get all meetings to which an account is participating
// // select id, title, description, start_time, timezone, duration, status where
// return db('participant')
// .where({ id })
// .select(
// 'id',
// 'username',
// 'email',
// 'timezone',
// 'earliest_time',
// 'latest_time',
// );
// }
function getAccountById(id) {
return db('account')
.where({ id })
.first()
.select(
'id',
'username',
'email',
'timezone',
'earliest_time',
'latest_time',
);
} }

View File

@ -0,0 +1,57 @@
const db = require('../../data/db');
module.exports = {
addMeeting,
getMeetingById,
updateMeeting,
deleteMeeting,
// getMeetingsByAccountId,
};
function addMeeting(data) {
return db('meeting')
.insert(data)
.returning([
'id',
'title',
'description',
'start_time',
'timezone',
'duration',
'status',
]);
}
function updateMeeting(data, id) {
return db('meeting')
.where({ id })
.update(data)
.returning([
'id',
'title',
'description',
'start_time',
'timezone',
'duration',
'status',
]);
}
function deleteMeeting(id) {
return db('meeting').where({ id }).del();
}
function getMeetingById(id) {
return db('meeting')
.where({ id })
.first()
.select(
'id',
'title',
'description',
'start_time',
'timezone',
'duration',
'status',
);
}

View File

@ -3,15 +3,61 @@ let router = express.Router();
let Account = require('../models/accountModel'); let Account = require('../models/accountModel');
// Add a user
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
const userData = { ...req.body }; const data = { ...req.body };
try { try {
const user = await Account.addUser(userData); const [account] = await Account.addAccount(data);
res.status(201).json(user); res.status(201).json(account);
} catch (error) { } catch (error) {
res.status(500).json({ message: 'Failed to add new user', error }); res.status(500).json({ message: 'Failed to add new account.', error });
}
});
router.put('/:id', async (req, res) => {
const data = { ...req.body };
const id = req.params.id;
try {
const account = await Account.updateAccount(data, id);
res.status(200).json(...account);
} catch (error) {
res.status(500).json({ message: 'Failed to update account.', error });
}
});
router.delete('/:id', async (req, res) => {
const id = req.params.id;
try {
const account = await Account.deleteAccount(id);
res.status(200).json({message: `Account with id ${id} successfully deleted.`});
} catch (error) {
res.status(500).json({ message: 'Failed to delete account.', error });
}
});
// // Get list of meetings by id
// router.get('/', async (req, res) => {
// const { id } = req.body;
// try {
// const meetings = await Account.getMeetingsByAccountId(id);
// res.status(200).json(meetings);
// } catch (error) {
// res.status(500).json({ message: "Couldn't get meetings for account with id ${id}.", error });
// }
// });
router.get('/:id', async (req, res) => {
const id = req.params.id;
try {
const account = await Account.getAccountById(id);
res.status(200).json(account);
} catch (error) {
res.status(500).json({ message: "Account doesn't exist.", error });
} }
}); });

View File

@ -0,0 +1,54 @@
let express = require('express');
let router = express.Router();
const { v4: uuidv4 } = require('uuid');
let Meeting = require('../models/meetingModel');
router.post('/', async (req, res) => {
id = uuidv4();
data = {id, ...req.body}
console.log(data);
try {
const [meeting] = await Meeting.addMeeting(data);
res.status(201).json(meeting);
} catch (error) {
res.status(500).json({ message: 'Failed to add meeting.', error });
}
});
router.put('/:id', async (req, res) => {
const data = { ...req.body };
const id = req.params.id;
try {
const meeting = await Meeting.updateMeeting(data, id);
res.status(200).json(meeting);
} catch (error) {
res.status(500).json({ message: 'Failed to update meeting.', error });
}
});
router.delete('/:id', async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.deleteMeeting(id);
res.status(200).json({message: `Meeting with id ${id} successfully deleted.`});
} catch (error) {
res.status(500).json({ message: 'Failed to delete meeting.', error });
}
});
router.get('/', async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getMeetingById(id);
res.status(200).json(meeting);
} catch (error) {
res.status(500).json({ message: "Meeting doesn't exist.", error });
}
});
module.exports = router;

View File

@ -3,15 +3,17 @@ const cors = require('cors');
const helmet = require('helmet'); const helmet = require('helmet');
require('dotenv').config(); require('dotenv').config();
const accountsRoute = require('../api/routes/accountRoute'); const accountsRoute = require('./routes/accountRoute');
const meetingsRoute = require('./routes/meetingRoute');
const server = express(); const server = express();
server.use(express.json());
server.use(cors()); server.use(cors());
server.use(express.json());
server.use(helmet()); server.use(helmet());
server.use('/api/accounts', accountsRoute); server.use('/api/accounts', accountsRoute);
server.use('/api/meetings', meetingsRoute);
server.get('/', (req, res) => server.get('/', (req, res) =>
res res

View File

@ -1,30 +1,33 @@
require('dotenv').config(); require('dotenv').config();
module.exports = { module.exports = {
development: { development: {
client: 'pg', client: 'pg',
connection: process.env.DATABASE_URL, connection: process.env.DATABASE_URL,
migrations: { migrations: {
directory: './data/migrations', directory: './data/migrations',
}, },
seeds: { directory: './data/seeds' }, seeds: { directory: './data/seeds' },
}, useNullAsDefault: true,
},
testing: { testing: {
client: 'pg', client: 'pg',
connection: process.env.DATABASE_URL, connection: process.env.DATABASE_URL,
migrations: { migrations: {
directory: './data/migrations', directory: './data/migrations',
}, },
seeds: { directory: './data/seeds' }, seeds: { directory: './data/seeds' },
}, useNullAsDefault: true,
},
production: { production: {
client: 'pg', client: 'pg',
connection: process.env.DATABASE_URL, connection: process.env.DATABASE_URL,
migrations: { migrations: {
directory: './data/migrations', directory: './data/migrations',
}, },
seeds: { directory: './data/seeds' }, seeds: { directory: './data/seeds' },
}, useNullAsDefault: true,
},
}; };

11
package-lock.json generated
View File

@ -1134,6 +1134,11 @@
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.2.0.tgz", "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.2.0.tgz",
"integrity": "sha512-xB/+wxcpFipUZOQcSzcgkjcNOosGhEoPSjz06jC89lv1dj7mc9bZv6wLVy8M2fVjP0a/xN0N988YDq1L0FhK3A==" "integrity": "sha512-xB/+wxcpFipUZOQcSzcgkjcNOosGhEoPSjz06jC89lv1dj7mc9bZv6wLVy8M2fVjP0a/xN0N988YDq1L0FhK3A=="
},
"uuid": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-7.0.3.tgz",
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg=="
} }
} }
}, },
@ -2226,9 +2231,9 @@
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
}, },
"uuid": { "uuid": {
"version": "7.0.3", "version": "8.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-7.0.3.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.0.0.tgz",
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==" "integrity": "sha512-jOXGuXZAWdsTH7eZLtyXMqUb9EcWMGZNbL9YcGBJl4MH4nrxHmZJhEHvyLFrkxo+28uLb/NYRcStH48fnD0Vzw=="
}, },
"v8flags": { "v8flags": {
"version": "3.1.3", "version": "3.1.3",

View File

@ -20,6 +20,7 @@
"helmet": "^3.22.0", "helmet": "^3.22.0",
"knex": "^0.21.1", "knex": "^0.21.1",
"pg": "^8.0.3", "pg": "^8.0.3",
"pgtools": "^0.3.0" "pgtools": "^0.3.0",
"uuid": "^8.0.0"
} }
} }