-
Notifications
You must be signed in to change notification settings - Fork 756
Expand file tree
/
Copy pathusers.js
More file actions
60 lines (53 loc) · 1.24 KB
/
users.js
File metadata and controls
60 lines (53 loc) · 1.24 KB
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
52
53
54
55
56
57
58
59
60
const { models } = require('../../sequelize');
const { getIdParam } = require('../helpers');
async function getAll(req, res) {
const users = await models.user.findAll();
res.status(200).json(users);
};
async function getById(req, res) {
const id = getIdParam(req);
const user = await models.user.findByPk(id);
if (user) {
res.status(200).json(user);
} else {
res.status(404).send('404 - Not found');
}
};
async function create(req, res) {
if (req.body.id) {
res.status(400).send(`Bad request: ID should not be provided, since it is determined automatically by the database.`)
} else {
await models.user.create(req.body);
res.status(201).end();
}
};
async function update(req, res) {
const id = getIdParam(req);
// We only accept an UPDATE request if the `:id` param matches the body `id`
if (req.body.id === id) {
await models.user.update(req.body, {
where: {
id: id
}
});
res.status(200).end();
} else {
res.status(400).send(`Bad request: param ID (${id}) does not match body ID (${req.body.id}).`);
}
};
async function remove(req, res) {
const id = getIdParam(req);
await models.user.destroy({
where: {
id: id
}
});
res.status(200).end();
};
module.exports = {
getAll,
getById,
create,
update,
remove,
};