Skip to content

Commit

Permalink
23-make-backend-routes-for-users (#26)
Browse files Browse the repository at this point in the history
* 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
3 people authored Dec 18, 2023
1 parent 8c703e9 commit 15d3d25
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 29 deletions.
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
Expand Down
68 changes: 68 additions & 0 deletions routes/users.js
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;
29 changes: 0 additions & 29 deletions server/queries/users.sql

This file was deleted.

0 comments on commit 15d3d25

Please sign in to comment.