Add users endpoints with queries
This commit is contained in:
parent
d97221e522
commit
ccc7a0bfb0
17
.eslintrc.js
Normal file
17
.eslintrc.js
Normal file
@ -0,0 +1,17 @@
|
||||
module.exports = {
|
||||
"env": {
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"rules": {
|
||||
}
|
||||
};
|
@ -53,6 +53,7 @@ Sleep Tracker is intended for anyone interested in improving their sleep and hea
|
||||
|
||||
- `GET /api/users/:id` - fetch a user
|
||||
- `PUT /api/users/:id` - update a user
|
||||
- `DELETE /api/users/:id` - delete a user
|
||||
|
||||
- `GET /api/users/:id/sessions` - fetch list of all sessions by user id
|
||||
- `POST /api/users/:id/sessions` - add a session by user id
|
||||
|
@ -3,6 +3,7 @@ const helmet = require('helmet');
|
||||
const cors = require('cors');
|
||||
|
||||
const authRouter = require('../services/auth/authRouter');
|
||||
const usersRouter = require('../services/users/usersRouter');
|
||||
|
||||
const server = express();
|
||||
|
||||
@ -11,5 +12,6 @@ server.use(express.json());
|
||||
server.use(cors());
|
||||
|
||||
server.use('/api/auth', authRouter);
|
||||
server.use('/api/users', usersRouter);
|
||||
|
||||
module.exports = server;
|
||||
|
@ -4,6 +4,8 @@ module.exports = {
|
||||
add,
|
||||
findBy,
|
||||
findById,
|
||||
update,
|
||||
remove
|
||||
};
|
||||
|
||||
function findBy(filter) {
|
||||
@ -23,3 +25,16 @@ function findById(id) {
|
||||
.first()
|
||||
.select('id', 'email', 'username');
|
||||
}
|
||||
|
||||
async function update(id, changes) {
|
||||
await db('users')
|
||||
.where({ id })
|
||||
.update(changes, '*');
|
||||
return findById(id);
|
||||
}
|
||||
|
||||
function remove(id) {
|
||||
return db('users')
|
||||
.where({ id })
|
||||
.del();
|
||||
}
|
32
services/users/usersRouter.js
Normal file
32
services/users/usersRouter.js
Normal file
@ -0,0 +1,32 @@
|
||||
const router = require('express').Router();
|
||||
|
||||
const { authenticate } = require('../auth/authenticate');
|
||||
const Users = require('../users/usersModel');
|
||||
|
||||
router.get('/:id', authenticate, (req, res) => {
|
||||
Users.findById(req.params.id)
|
||||
.then(users => {
|
||||
res.status(200).json(users);
|
||||
})
|
||||
.catch(err => res.send(err));
|
||||
});
|
||||
|
||||
router.put('/:id', authenticate, async (req, res) => {
|
||||
Users.update(req.params.id, req.body)
|
||||
.then( user => {
|
||||
if (user) {
|
||||
res.status(200).json(user);
|
||||
} else {
|
||||
res.status(404).json({ message: 'The user could not be found' });
|
||||
}
|
||||
})
|
||||
.catch(err => res.status(500).json({ message: 'Error updating the user' }));
|
||||
});
|
||||
|
||||
router.delete("/:id", (req, res) => {
|
||||
Users.remove(req.params.id)
|
||||
.then(data => res.status(200).json(data))
|
||||
.catch(err => res.json(err));
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Reference in New Issue
Block a user