-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
65 lines (61 loc) · 1.76 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//users
//Here we are holding all the users, the property will be username and it is unique.
var users = [];
var cookies = {};
module.exports = {
get: function(username){
return users[username];
},
getTodos: function(username){
return users[username].todos;
},
insertUser:function(details){
users[details.username] = {fullName: details.fullName, password: details.password, todos: []};
},
getUserByCookie: function(cookie){
return cookies[cookie];
},
setUserByCookie: function(cookie,username){
cookies[cookie] = username;
},
isIdExists: function(id,todos){
for(var i = 0; i < todos.length; i++){
if(todos[i].id === id){
return i;
}
}
return -1;
},
insertTodo: function(username,todoId,todoVal){
if(this.isIdExists(todoId,users[username].todos) < 0){
users[username].todos.push({id: todoId, completed: false, title: todoVal});
return {status: 0, msg: ""};
}
return {status: 1, msg: "id of todo item already exists."};
},
deleteTodo: function(username,todoId){
for(var todo in users[username].todos){
if(users[username].todos[todo].id === todoId){
users[username].todos.splice(todo,1);
return {status: 0, msg: ""};
}
}
return {status:1, msg: "item does not exist"};
},
deleteCompleted: function(username){
var noCompleted = users[username].todos.filter(function(todo){
return !todo.completed;
});
users[username].todos = noCompleted;
return {status: 0, msg: ""};
},
updateTodo: function(username, todoId, value, completed){
var todoIndex = this.isIdExists(todoId,users[username].todos);
if(todoIndex < 0){
return {status: 1, msg: "item does not exist"};
}
users[username].todos[todoIndex].title = value;
users[username].todos[todoIndex].completed = completed;
return {status: 0, msg: ""};
}
};