generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
23-make-backend-routes-for-users (#26)
* Create a pull trequest for branch 23-make-backend-routes-for-users * Co-authored-by: cherhchen <[email protected]> * Finished backend routes Co-authored-by: cherhchen <[email protected]> * Refactor users.js for minor updates and delete users.sql Co-authored-by: cherhchen <[email protected]> --------- Co-authored-by: Ethan Ho <[email protected]> Co-authored-by: cherhchen <[email protected]>
- Loading branch information
1 parent
8c703e9
commit 15d3d25
Showing
3 changed files
with
72 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file was deleted.
Oops, something went wrong.