/
angularApp.js
executable file
·269 lines (232 loc) · 6.36 KB
/
angularApp.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
var app = angular.module('flapperNews', ['ui.router']);
app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider.state('home', {
url : '/home',
templateUrl : '/home.html',
controller : 'MainCtrl',
resolve : {
postPromise : ['posts',
function(posts) {
return posts.getAll();
}]
}
}).state('posts', {
url : '/posts/:id',
templateUrl : '/posts.html',
controller : 'PostsCtrl',
resolve : {
post : ['$stateParams', 'posts',
function($stateParams, posts) {
return posts.get($stateParams.id);
}]
}
}).state('login', {
url : '/login',
templateUrl : '/login.html',
controller : 'AuthCtrl',
onEnter : ['$state', 'auth',
function($state, auth) {
if (auth.isLoggedIn()) {
$state.go('home');
}
}]
}).state('register', {
url : '/register',
templateUrl : '/register.html',
controller : 'AuthCtrl',
onEnter : ['$state', 'auth',
function($state, auth) {
if (auth.isLoggedIn()) {
$state.go('home');
}
}]
});
$urlRouterProvider.otherwise('home');
}]);
app.factory('auth', ['$http', '$window',
function($http, $window) {
var auth = {};
auth.saveToken = function(token) {
$window.localStorage['flapper-news-token'] = token;
};
auth.getToken = function() {
return $window.localStorage['flapper-news-token'];
}
auth.isLoggedIn = function() {
var token = auth.getToken();
if (token) {
var payload = JSON.parse($window.atob(token.split('.')[1]));
return payload.exp > Date.now() / 1000;
} else {
return false;
}
};
auth.currentUser = function() {
if (auth.isLoggedIn()) {
var token = auth.getToken();
var payload = JSON.parse($window.atob(token.split('.')[1]));
return payload.username;
}
};
auth.register = function(user) {
return $http.post('/register', user).success(function(data) {
auth.saveToken(data.token);
});
};
auth.logIn = function(user) {
return $http.post('/login', user).success(function(data) {
auth.saveToken(data.token);
});
};
auth.logOut = function() {
$window.localStorage.removeItem('flapper-news-token');
};
return auth;
}]);
app.factory('posts', ['$http', 'auth',
function($http, auth) {
var o = {
posts : []
};
o.getAll = function() {
return $http.get('/posts').success(function(data) {
angular.copy(data, o.posts);
});
};
//now we'll need to create new posts
//uses the router.post in index.js to post a new Post mongoose model to mongodb
//when $http gets a success back, it adds this post to the posts object in
//this local factory, so the mongodb and angular data is the same
//sweet!
o.create = function(post) {
return $http.post('/posts', post, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
o.posts.push(data);
});
};
o.upvote = function(post) {
return $http.put('/posts/' + post._id + '/upvote', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
post.upvotes += 1;
});
};
//downvotes
o.downvote = function(post) {
return $http.put('/posts/' + post._id + '/downvote', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
post.upvotes -= 1;
});
};
//grab a single post from the server
o.get = function(id) {
//use the express route to grab this post and return the response
//from that route, which is a json of the post data
//.then is a promise, a kind of newly native thing in JS that upon cursory research
//looks friggin sweet; TODO Learn to use them like a boss. First, this.
return $http.get('/posts/' + id).then(function(res) {
return res.data;
});
};
//comments, once again using express
o.addComment = function(id, comment) {
return $http.post('/posts/' + id + '/comments', comment, {
headers: {Authorization: 'Bearer '+auth.getToken()}
});
};
o.upvoteComment = function(post, comment) {
return $http.put('/posts/' + post._id + '/comments/'+ comment._id + '/upvote', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
comment.upvotes += 1;
});
};
//downvote comments
//I should really consolidate these into one voteHandler function
o.downvoteComment = function(post, comment) {
return $http.put('/posts/' + post._id + '/comments/'+ comment._id + '/downvote', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
comment.upvotes -= 1;
});
};
return o;
}]);
app.controller('MainCtrl', ['$scope', 'posts', 'auth',
function($scope, posts, auth) {
$scope.posts = posts.posts;
$scope.isLoggedIn = auth.isLoggedIn;
//setting title to blank here to prevent empty posts
$scope.title = '';
$scope.addPost = function() {
if ($scope.title === '') {
return;
}
posts.create({
title : $scope.title,
link : $scope.link,
});
//clear the values
$scope.title = '';
$scope.link = '';
};
$scope.upvote = function(post) {
//our post factory has an upvote() function in it
//we're just calling this using the post we have
console.log('Upvoting:' + post.title + "votes before:" + post.upvotes);
posts.upvote(post);
};
$scope.downvote = function(post) {
posts.downvote(post);
};
}]);
app.controller('PostsCtrl', ['$scope', 'posts', 'post', 'auth',
function($scope, posts, post, auth) {
$scope.post = post;
$scope.isLoggedIn = auth.isLoggedIn;
$scope.addComment = function() {
if ($scope.body === '') {
return;
}
posts.addComment(post._id, {
body : $scope.body,
author : 'user'
}).success(function(comment) {
$scope.post.comments.push(comment);
});
$scope.body = '';
};
$scope.upvote = function(comment) {
posts.upvoteComment(post, comment);
};
$scope.downvote = function(comment) {
posts.downvoteComment(post, comment);
};
}]);
app.controller('AuthCtrl', ['$scope', '$state', 'auth',
function($scope, $state, auth) {
$scope.user = {};
$scope.register = function() {
auth.register($scope.user).error(function(error) {
$scope.error = error;
}).then(function() {
$state.go('home');
});
};
$scope.logIn = function() {
auth.logIn($scope.user).error(function(error) {
$scope.error = error;
}).then(function() {
$state.go('home');
});
};
}]);
app.controller('NavCtrl', ['$scope', 'auth',
function($scope, auth) {
$scope.isLoggedIn = auth.isLoggedIn;
$scope.currentUser = auth.currentUser;
$scope.logOut = auth.logOut;
}]);