-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
51 lines (37 loc) · 1.31 KB
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const { Router } = require('express');
const { User } = require('../models');
const { buildFilter } = require('../utils/queryHelper');
const { dropUndefined } = require('../utils/dropUndefined');
const router = Router();
router.get('/', async (request, response) => {
const {
query: { displayName, email, createdAt, updatedAt }
} = request;
const filter = buildFilter({ displayName, email, createdAt, updatedAt });
const users = await User.find(filter.length ? { $and: filter } : {});
response.send(users);
});
router.get('/:id', async (request, response) => {
const { id } = request.params;
const user = await User.findById(id);
if (!user) {
response.status(404).send({ error: 'User not found!' });
}
response.send(user);
});
router.put('/:id', async (request, response) => {
const { id } = request.params;
const { displayName, email } = request.body;
const updatedUserInfo = dropUndefined({ displayName, email });
let updatedUser = await User.findByIdAndUpdate(id, updatedUserInfo, { new: true });
response.json(updatedUser);
});
router.delete('/:id', async (request, response) => {
const { id } = request.params;
const deleted = await User.findByIdAndDelete(id);
if (!deleted) {
response.status(404).send({ error: 'User not found!' });
}
response.send({ deleted });
});
module.exports = router;