Skip to content

Commit

Permalink
Finished backend routes
Browse files Browse the repository at this point in the history
Co-authored-by: cherhchen <[email protected]>
  • Loading branch information
h0ethan04 and cherhchen committed Dec 1, 2023
1 parent 188e789 commit 49e1bca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 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
37 changes: 27 additions & 10 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 49e1bca

Please sign in to comment.