forked from modernduck/ng1-day2-router-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
66 lines (63 loc) · 2.34 KB
/
factory.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
angular.module("myFactory", [])
.factory('userProvider', function(){
var users = [{id:1, name:"sompop", lastname:"kulapalanont", salary:50000, picture:"http://placehold.it/100x100"},
{id:2, name:"pichanok", lastname:"noobparn", salary:30000, picture:"http://placehold.it/100x100"},
{id:3, name:"unnandunn", lastname:"gucheng", salary:50000, picture:"http://placehold.it/100x100"},
{id:4, name:"lermisme", lastname:"marketting", salary:35000, picture:"http://placehold.it/100x100"}];
return {
load : () => {
if(localStorage['user-info-data'])
users = JSON.parse(localStorage['user-info-data']);
return users;
},
save : () => {
localStorage['user-info-data'] = JSON.stringify(users)
},
getUsers : () => {
return users;
},
createUser : (name, lastname, salary, picture) => {
var now = new Date();
users.push({
id:now.getTime(),
name:name,
lastname:lastname,
salary:salary,
picture:picture
})
},
getUserById : id => {
return users.find( item => {
return item.id == id
})
},
updateUserById : (id, name, lastname, salary, picture )=> {
//find INdex
var index = users.findIndex( item => {
return item.id == id;
})//if no match return -1
//check if user@index is valid
if(users[index]){
users[index].name = name;
users[index].lastname = lastname;
users[index].salary = salary;
users.picture = picture;
}
},
deleteUserById : id => {
var index = users.findIndex( item => {
return item.id == id;
})
users.splice(index, 1);
}
}
})
/*
Create a service that can
1.Save / load data to local storage
2. getUsers()
3. createUser()
4. getUserById(id)
5. updateUserById(id, ….. )
6. deleteUserById(id)
*/