-
Notifications
You must be signed in to change notification settings - Fork 4
/
UserController.js
95 lines (90 loc) · 2.19 KB
/
UserController.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
*
*/
'use strict';
app.controller('UserController', [ '$scope', 'UserService', '$location',
'$rootScope', function($scope, UserService, $location, $rootScope) {
console.log("UserController...")
var self = this;
self.user = {
id : '',
name : '',
password : '',
mobile : '',
address : '',
email : '',
role : '',
errorMessage : ''
};
self.users=[];
self.fetchAllUsers=function(){
UserService.fetchAllUsers().then(function(d){
self.users=d;
},function(errResponse){
console.error('Error while fetching Users');
});
};
self.createUser=function(user){
UserService.createUser(user).then(self.fetchAllUsers,
function(errResponse){
console.error('Error while creating User...');
});
};
self.updateUser=function(user,id){
UserService.updateUser(user,id).then(self.fetchAllUsers,
function(errResponse){
console.error('Error while updating User...');
});
};
self.authenticate=function(user){
UserService.authenticate(user).then(
function(d){
self.user=d;
if($rootScope.currentUser)
{
$location.path('/');
}
},
function(errResponse){
console.error('Error while authenticate User...');
});
};
self.deleteUser=function(id){
UserService.deleteUser(id).then(self.fetchAllUsers,
function(errResponse){
console.error('Error while deleting User');
});
};
self.fetchAllUsers();
self.login=function(){
{
console.log('login validation????',self.user);
self.authenticate(self.user);
}
};
self.submit=function(){
{
console.log('Saving New user',self.user);
self.createUser(self.user);
}
self.reset();
};
self.edit=function(id){
console.log('id to be edited',id);
for(var i=0;i<self.users.length;i++){
if(self.users[i].id==id){
self.user=angular.copy(self.users[i]);
break;
}
}
};
self.remove=function(id){
console.log('id to be deleted',id);
if(self.user.id==id){
self.reset();
}
self.deleteUser(id);
};
}
]
);