From 2743622a0599cbde4c77624d6b52ede9f33bf0d2 Mon Sep 17 00:00:00 2001 From: ctc-devops <90984711+ctc-devops@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:40:54 -0800 Subject: [PATCH 1/4] Create a pull trequest for branch 23-make-backend-routes-for-users From 188e789268f0d600b0fc73b283fdfcc2014c2772 Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Mon, 27 Nov 2023 16:59:36 -0800 Subject: [PATCH 2/4] Co-authored-by: cherhchen --- routes/users.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 routes/users.js diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..96434be --- /dev/null +++ b/routes/users.js @@ -0,0 +1,51 @@ +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) { + console.log(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) { + console.log(err); + res.status(500).send(err.message); + } +}); + + +userRouter.post('/', async (req, res) => { + try { + const { id, email, type, approved } = req.params; + } catch (err) { + + } +}); + +userRouter.put('/:uid', async (req, res) => { + try { + + } catch (err) { + + } +}); + +userRouter.delete('/:uid', async (req, res) => { + try { + + } catch (err) { + + } +}); From 49e1bcaa5c87b41fc47834bd06e35c0871eee294 Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Fri, 1 Dec 2023 15:42:35 -0800 Subject: [PATCH 3/4] Finished backend routes Co-authored-by: cherhchen --- app.js | 4 ++++ routes/users.js | 37 +++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) 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 index 96434be..6826366 100644 --- a/routes/users.js +++ b/routes/users.js @@ -9,43 +9,60 @@ userRouter.get('/', async (req, res) => { const allUsers = await db.query(`SELECT * FROM users;`); res.status(200).json(keysToCamel(allUsers)); } catch (err) { - console.log(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) { - console.log(err); res.status(500).send(err.message); } }); - userRouter.post('/', async (req, res) => { try { - const { id, email, type, approved } = req.params; + 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({ + status: 'Success', + }); } 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(updatedApproval[0]); } catch (err) { - + return res.status(500).send(err.message); } }); userRouter.delete('/:uid', async (req, res) => { try { - + const { uid } = req.params; + await db.query(`DELETE FROM users WHERE id = $1;`, [uid]); + res.status(200).send('Deleted user'); } catch (err) { - + res.status(500).send(err.message); } }); + +module.exports = userRouter; From cabe25874088033736573ea17f916d6f8b6aa7c2 Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Wed, 13 Dec 2023 10:15:39 -0800 Subject: [PATCH 4/4] Refactor users.js for minor updates and delete users.sql Co-authored-by: cherhchen --- routes/users.js | 8 ++++---- server/queries/users.sql | 29 ----------------------------- 2 files changed, 4 insertions(+), 33 deletions(-) delete mode 100644 server/queries/users.sql diff --git a/routes/users.js b/routes/users.js index 6826366..eba4bbb 100644 --- a/routes/users.js +++ b/routes/users.js @@ -32,7 +32,7 @@ userRouter.post('/', async (req, res) => { approved, ]); res.status(201).json({ - status: 'Success', + id, }); } catch (err) { res.status(500).json({ @@ -49,7 +49,7 @@ userRouter.put('/:uid', async (req, res) => { `UPDATE users SET approved = TRUE WHERE id = $1 RETURNING *;`, [uid], ); - return res.status(200).send(updatedApproval[0]); + return res.status(200).send(keysToCamel(updatedApproval)); } catch (err) { return res.status(500).send(err.message); } @@ -58,8 +58,8 @@ userRouter.put('/:uid', async (req, res) => { userRouter.delete('/:uid', async (req, res) => { try { const { uid } = req.params; - await db.query(`DELETE FROM users WHERE id = $1;`, [uid]); - res.status(200).send('Deleted user'); + 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); } 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