forked from redhat-appstudio-qe/simple-nodejs-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
44 lines (36 loc) · 1.13 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
let users = [
{id: 1, firstName: "first1", lastName: "last1", email: "[email protected]"},
{id: 2, firstName: "first2", lastName: "last2", email: "[email protected]"},
{id: 3, firstName: "first3", lastName: "last3", email: "[email protected]"},
{id: 4, firstName: "first4", lastName: "last4", email: "[email protected]"}
]
function getUsers() {
return users;
}
function saveUser(user) {
const numberOfUsers = users.length
user['id'] = numberOfUsers + 1
users.push(user);
}
function deleteUser(id) {
const numberOfUsers = users.length
users = users.filter(user => user.id != id);
return users.length !== numberOfUsers
}
function replaceUser(id, user) {
const foundUser = users.filter(usr => usr.id == id);
if (foundUser.length === 0) return false
users = users.map(usr => {
if (id == usr.id) {
usr = {id: usr.id, ...user};
}
return usr
})
return true
}
const Users = function() {}
Users.prototype.getUsers = getUsers
Users.prototype.saveUser = saveUser
Users.prototype.deleteUser = deleteUser
Users.prototype.replaceUser = replaceUser
module.exports = new Users()