{"size": 6127, "ext": "js", "max_stars_repo_path": "flapper/flapperNews/public/javascripts/angularApp.js", "max_stars_repo_name": "codebhendi/hackBegins", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "flapper/flapperNews/public/javascripts/angularApp.js", "max_issues_repo_name": "codebhendi/hackBegins", "max_issues_repo_issues_event_min_datetime": "2016-03-01T18:48:05.000Z", "max_issues_repo_issues_event_max_datetime": "2016-03-01T18:48:05.000Z", "max_forks_repo_path": "flapper/flapperNews/public/javascripts/angularApp.js", "max_forks_repo_name": "codebhendi/hackBegins", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 22.3613138686, "max_line_length": 92, "alphanum_fraction": 0.5714052554}
var app = angular.module( 'flapperNews', ['ui.router'] );

app.config( [
	'$stateProvider',
	'$urlRouterProvider',
	function ( $stateProvider, $urlRouterProvider ) {
		
		$stateProvider
			.state( 'posts', {
				url: '/posts/{id}',
				templateUrl: '/posts.html',
				controller: 'PostsCtrl',
				resolve: {
					post: ['$stateParams', 'posts', function ( $stateParams, posts ) {
						return posts.get( $stateParams.id );
					}]
				}
			} )
			.state( 'home', {
				url: '/home',
				templateUrl: '/home.html',
				controller: 'MainCtrl',
				resolve: {
					postPromise: ['posts', function ( posts ) {
						return posts.getAll();
					}]
				}
			} )
			.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( 'posts', ['$http', 'auth', function ( $http, auth ) {
	var o = {
		posts: []
	};

	o.getAll = function () {
		return $http.get( '/posts' ).success( function ( data ) {
			angular.copy( data, o.posts );
		} );
	};

	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;
		} );
	};

	o.downvote = function ( post ) {
		return $http.put('/posts/' + post._id + '/downvote', null, {
			headers: {Authorization: 'Bearer ' + auth.getToken()}
		} ).success( function ( data ) {
				post.upvotes -= 1;
		} );
	};

	o.get = function ( id ) {
		return $http.get( '/posts/' + id ).then( function ( res ) {
			return res.data;
		} );
	};

	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;
		} );
	}

	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;
		} ).error( function( err ) {
			console.log( err );
		} );
	}

	return o;
}] )

.factory( 'auth', ['$http', '$window', function( $http, $window ) {
	var auth = {};

	auth.saveToken = function ( token ) {
		$window.localStorage['flapper-news-token'] = token;
	};

	auth.getToken = function () {
		//console.log($window.localStorage['flapper-news-token'] )
		return $window.localStorage['flapper-news-token'];
	};

	auth.isLoggedIn = function () {
		var token = auth.getToken();

		if ( token ) {
			var payload = JSON.parse( $window.atob( token.split( '.' )[1] ) );
			//console.log(payload);
			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) {
			//console.log(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.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.incrementUpvotes = function ( comment ) {
			posts.upvoteComment( $scope.post, comment );
		}

		$scope.decrementUpvotes = function ( comment ) {
			posts.downvoteComment( $scope.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;
				console.log(error);
			} ).then( function() {
				$state.go( 'home' );
			} )
		};

		$scope.logIn = function () {
			auth.logIn( $scope.user ).error( function ( error ) {
				$scope.error = error;
				console.log(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;
	}
] );

app.controller( 'MainCtrl', [
	'$scope',
	'posts',
	'auth',
	function ( $scope, posts, auth ) {
		$scope.posts = posts.posts;
		console.log($scope.posts[0]);
		$scope.isLoggedIn = auth.isLoggedIn;

		$scope.addPost = function () {
			posts.create( {
				title: $scope.title,
				link: $scope.link
			} );
			$scope.title = '';
			$scope.link = '';
		}

		$scope.incrementUpvotes = function ( post ) {
			posts.upvote( post );
		}

		$scope.decrementUpvotes = function ( post ) {
			posts.downvote( post );
		}
	}
] );