This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
73 lines (73 loc) · 2.05 KB
/
db.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Created by shawnmccarthy on 1/22/17.
*/
'use strict;';
//Include crypto to generate the movie id
var crypto = require('crypto');
module.exports = function () {
return {
userList: [],
/*
* Save the user inside the "db".
*/
save: function (user) {
user.id = crypto.randomBytes(20).toString('hex'); // fast enough for our purpose
this.userList.push(user);
return 1;
},
/*
* Retrieve a movie with a given id or return all the movies if the id is undefined.
*/
find: function (id) {
if (id) {
return this.userList.find(function (element) {
return element.id === id;
});
}
else {
return this.userList;
}
},
findOne: function (name) {
if (name) {
return this.userList.find(function (element) {
return element.username === name;
});
}
else {
return this.userList;
}
},
/*
* Delete a movie with the given id.
*/
remove: function (id) {
var found = 0;
this.userList = this.userList.filter(function (element) {
if (element.id === id) {
found = 1;
}
else {
return element.id !== id;
}
});
return found;
},
/*
* Update a movie with the given id
*/
update: function (id, user) {
var userIndex = this.userList.findIndex(function (element) {
return element.id === id;
});
if (userIndex !== -1) {
this.userList[userIndex].username = user.username;
this.userList[userIndex].password = user.password;
return 1;
}
else {
return 0;
}
}
};
};