diff --git a/app.js b/app.js index 18fd6c7..641de4c 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,9 @@ const cors = require('cors'); require('dotenv').config(); +// routes +const users = require('./routes/users'); + const app = express(); const PORT = process.env.PORT || 3001; @@ -15,6 +18,7 @@ app.use( // add all routes under here app.use(express.json()); // for req.body +app.use('/users', users); app.listen(PORT, () => { console.log(`Server listening on ${PORT}`); diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..eba4bbb --- /dev/null +++ b/routes/users.js @@ -0,0 +1,68 @@ +const express = require('express'); +const { keysToCamel } = require('../common/utils'); +const { db } = require('../server/db'); + +const userRouter = express.Router(); + +userRouter.get('/', async (req, res) => { + try { + const allUsers = await db.query(`SELECT * FROM users;`); + res.status(200).json(keysToCamel(allUsers)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +userRouter.get('/pending-accounts', async (req, res) => { + try { + const pendingAccounts = await db.query(`SELECT * FROM users WHERE approved = FALSE;`); + res.status(200).json(keysToCamel(pendingAccounts)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +userRouter.post('/', async (req, res) => { + try { + const { id, email, type, approved } = req.body; + await db.query(`INSERT INTO users (id, email, "type", approved) VALUES ($1, $2, $3, $4);`, [ + id, + email, + type, + approved, + ]); + res.status(201).json({ + id, + }); + } catch (err) { + res.status(500).json({ + status: 'Failed', + msg: err.message, + }); + } +}); + +userRouter.put('/:uid', async (req, res) => { + try { + const { uid } = req.params; + const updatedApproval = await db.query( + `UPDATE users SET approved = TRUE WHERE id = $1 RETURNING *;`, + [uid], + ); + return res.status(200).send(keysToCamel(updatedApproval)); + } catch (err) { + return res.status(500).send(err.message); + } +}); + +userRouter.delete('/:uid', async (req, res) => { + try { + const { uid } = req.params; + const deletedUser = await db.query(`DELETE FROM users WHERE id = $1 RETURNING *;`, [uid]); + res.status(200).send(keysToCamel(deletedUser)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +module.exports = userRouter; diff --git a/server/queries/users.sql b/server/queries/users.sql deleted file mode 100644 index 1700cdc..0000000 --- a/server/queries/users.sql +++ /dev/null @@ -1,29 +0,0 @@ --- PUT/:uid - Updates an existing user as approved -UPDATE - users -SET - approved = TRUE -WHERE - id = ?; - --- DELETE/:uid - Deletes an existing user given their id -DELETE FROM - users -WHERE - id = ?; - --- GET - Returns all data from the Users table -SELECT * FROM - users; - --- GET/pending-accounts - Returns all data from Users table who are currently pending approval -SELECT * FROM - users -WHERE - approved = FALSE; - --- POST - Adds a new row into Users table -INSERT INTO - users (id, email, "type", approved) -VALUES - (?,?,?,?); \ No newline at end of file