-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathuser.js
40 lines (37 loc) · 1.13 KB
/
user.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
const fs = require('fs');
// const index = fs.readFileSync('index.html', 'utf-8');
const data = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
const users = data.users;
exports.createUser = (req, res) => {
console.log(req.body);
users.push(req.body);
res.status(201).json(req.body);
};
exports.getAllUsers = (req, res) => {
res.json(users);
};
exports.getUser = (req, res) => {
const id = +req.params.id;
const user = users.find((p) => p.id === id);
res.json(user);
};
exports.replaceUser = (req, res) => {
const id = +req.params.id;
const userIndex = users.findIndex((p) => p.id === id);
users.splice(userIndex, 1, { ...req.body, id: id });
res.status(201).json();
};
exports.updateUser = (req, res) => {
const id = +req.params.id;
const userIndex = users.findIndex((p) => p.id === id);
const user = users[userIndex];
users.splice(userIndex, 1, { ...user, ...req.body });
res.status(201).json();
};
exports.deleteUser = (req, res) => {
const id = +req.params.id;
const userIndex = users.findIndex((p) => p.id === id);
const user = users[userIndex];
users.splice(userIndex, 1);
res.status(201).json(user);
};