-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (139 loc) · 4.34 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var app = angular.module('snapp',['ngRoute','ngCookies']);
var URL = "http://betsol.org:9000/";
app.config(function($routeProvider){
$routeProvider
.when('/',{
redirectTo: '/login'
})
.when('/login',{
templateUrl: 'views/login.html',
controller: 'loginCtrl'
})
.when('/signup',{
templateUrl: 'views/signup.html',
controller: 'signupCtrl'
})
.when('/dashboard',{
templateUrl: 'views/dashboard.html',
controller: 'dashboardCtrl'
})
.otherwise({
redirectTo: '/'
});
});
app.controller('loginCtrl',['$scope','$location','$http',function($scope,$location,$http){
$scope.login=function(){
var request= $http({
method: "POST",
url: URL + "login",
data:{email:$scope.email,password:$scope.password}
});
request.success(function(data){
var response= angular.fromJson(data);//converts from Json to angular format
if(response["error"]){
$scope.validationMessage=response["message"][0];// response["message"][0] gives the backend error message
}
else{
sessionStorage.email=response["email"];//sessions are active until the browser shuts the process
sessionStorage.password=response["password"];
sessionStorage.userId=response["id"];
$location.url('/dashboard');
}
});
request.error(function(data){
console.log(data);
});
}
}]);
app.controller('signupCtrl',['$scope','$location','$http',
function($scope,$location,$http){
$scope.signup=function(){
var request= $http({
method: "POST",
url: URL + "signup",
data:{email:$scope.email,password:$scope.password,firstName:$scope.firstName,lastName:$scope.lastName}
});
request.success(function(data){
var response= angular.fromJson(data);//converts from Json to angular format
if(response["error"]){
$scope.validationMessage=response["message"][0];// response["message"][0] gives the backend error message
}
else{
sessionStorage.email=response["email"];
sessionStorage.password=response["password"];
sessionStorage.userId=response["id"];
$location.url('/dashboard');
}
});
request.error(function(data){
console.log(data);
});
}
}]);
app.controller('dashboardCtrl',['$scope','$location','$http',function($scope,$location,$http){
$scope.update=function(){
delete $scope.profileData["connectionRequests"];
delete $scope.profileData["connections"];
delete $scope.profileData["suggestions"];
var request= $http({
method: "PUT",
url: URL + "profile/" + sessionStorage.userId,
data:$scope.profileData
});
request.success(function(data){
//var response= angular.fromJson(data);//converts from Json to angular format
$scope.responseMessage="Update sucessful";
$("#dashboardMsgModal").modal('show');
$scope.getProfileData();
});
request.error(function(data){
console.log(data);
});
}
$scope.getProfileData=function(){
var request=$http({
method: "GET",
url: URL + "profile/" + sessionStorage.userId
});
request.success(function(data){
$scope.profileData=angular.fromJson(data);
console.log(data);
});
request.error(function(data){
console.log(data);
});
}
$scope.getProfileData();
$scope.sendRequest=function(receiverId){
var request=$http({
method:"POST",
url: URL + "request/send/" + sessionStorage.userId + "/" + receiverId
});
request.success(function(data) {
$scope.responseMessage= "Your request has been sent";
$("#dashboardMsgModal").modal('show');
$scope.getProfileData();
});
request.error(function(data){
console.log(data);
});
}
$scope.acceptRequest=function(requestId){
var request=$http({
method: "POST",
url: URL + "request/accept/" + requestId
});
request.success(function(data){
$scope.responseMessage="Friend request accepted";
$("#dashboardMsgModal").modal('show');
$scope.getProfileData();
});
request.console.error(function(data){
console.log(data);
});
}
$scope.logout=function(){
sessionStorage.clear();
$location.path("/login");
}
}]);