diff --git a/api/models/accountModel.js b/api/models/accountModel.js index 5981bb9..a8b6f50 100644 --- a/api/models/accountModel.js +++ b/api/models/accountModel.js @@ -1,14 +1,69 @@ const db = require('../../data/db'); module.exports = { - addUser, + addAccount, + getAccountById, + updateAccount, + deleteAccount, + // getMeetingsByAccountId, }; -function addUser(userData) { - // TODO Complete query without providing id - // right now if ID is not provided, pg-promise send an erro fo mising column - return db.one( - 'INSERT INTO account VALUES(emptyUpdate, ${username}, ${email}, ${password}, ${timezone}, ${earliest_time}, ${latest_time}) RETURNING *', - userData, - ); +function addAccount(data) { + return db('account') + .insert(data) + .returning([ + 'id', + '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', + ); } diff --git a/api/models/meetingModel.js b/api/models/meetingModel.js new file mode 100644 index 0000000..a5e4cd2 --- /dev/null +++ b/api/models/meetingModel.js @@ -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', + ); +} diff --git a/api/routes/accountRoute.js b/api/routes/accountRoute.js index 48e1a55..a2385fd 100644 --- a/api/routes/accountRoute.js +++ b/api/routes/accountRoute.js @@ -3,15 +3,61 @@ let router = express.Router(); let Account = require('../models/accountModel'); -// Add a user router.post('/', async (req, res) => { - const userData = { ...req.body }; + const data = { ...req.body }; try { - const user = await Account.addUser(userData); - res.status(201).json(user); + const [account] = await Account.addAccount(data); + res.status(201).json(account); } 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 }); } }); diff --git a/api/routes/meetingRoute.js b/api/routes/meetingRoute.js new file mode 100644 index 0000000..f13a6e0 --- /dev/null +++ b/api/routes/meetingRoute.js @@ -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; diff --git a/api/server.js b/api/server.js index d44db72..24abc18 100644 --- a/api/server.js +++ b/api/server.js @@ -3,15 +3,17 @@ const cors = require('cors'); const helmet = require('helmet'); require('dotenv').config(); -const accountsRoute = require('../api/routes/accountRoute'); +const accountsRoute = require('./routes/accountRoute'); +const meetingsRoute = require('./routes/meetingRoute'); const server = express(); -server.use(express.json()); server.use(cors()); +server.use(express.json()); server.use(helmet()); server.use('/api/accounts', accountsRoute); +server.use('/api/meetings', meetingsRoute); server.get('/', (req, res) => res diff --git a/knexfile.js b/knexfile.js index 8ad8755..b6968a4 100644 --- a/knexfile.js +++ b/knexfile.js @@ -1,30 +1,33 @@ require('dotenv').config(); module.exports = { - development: { - client: 'pg', - connection: process.env.DATABASE_URL, - migrations: { - directory: './data/migrations', - }, - seeds: { directory: './data/seeds' }, - }, + development: { + client: 'pg', + connection: process.env.DATABASE_URL, + migrations: { + directory: './data/migrations', + }, + seeds: { directory: './data/seeds' }, + useNullAsDefault: true, + }, - testing: { - client: 'pg', - connection: process.env.DATABASE_URL, - migrations: { - directory: './data/migrations', - }, - seeds: { directory: './data/seeds' }, - }, + testing: { + client: 'pg', + connection: process.env.DATABASE_URL, + migrations: { + directory: './data/migrations', + }, + seeds: { directory: './data/seeds' }, + useNullAsDefault: true, + }, - production: { - client: 'pg', - connection: process.env.DATABASE_URL, - migrations: { - directory: './data/migrations', - }, - seeds: { directory: './data/seeds' }, - }, -}; \ No newline at end of file + production: { + client: 'pg', + connection: process.env.DATABASE_URL, + migrations: { + directory: './data/migrations', + }, + seeds: { directory: './data/seeds' }, + useNullAsDefault: true, + }, +}; diff --git a/package-lock.json b/package-lock.json index e90941f..0a2e6d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1134,6 +1134,11 @@ "version": "2.2.0", "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.2.0.tgz", "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=" }, "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==" + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.0.0.tgz", + "integrity": "sha512-jOXGuXZAWdsTH7eZLtyXMqUb9EcWMGZNbL9YcGBJl4MH4nrxHmZJhEHvyLFrkxo+28uLb/NYRcStH48fnD0Vzw==" }, "v8flags": { "version": "3.1.3", diff --git a/package.json b/package.json index 8337515..1ca0ff2 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "helmet": "^3.22.0", "knex": "^0.21.1", "pg": "^8.0.3", - "pgtools": "^0.3.0" + "pgtools": "^0.3.0", + "uuid": "^8.0.0" } }