'use strict';

// Posts controller
angular.module('posts').controller('PostsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Posts','$http',
	function($scope, $stateParams, $location, Authentication, Posts,$http) {
		$scope.authentication = Authentication;

		// Create new Post
		$scope.create = function() {
            $http.post('/posts', $scope.fields).success(function(response) {
            // And redirect to the project page
                $location.path('/posts/'+response._id);
            }).error(function(response) {
                $scope.error = response.message;
        });
        };

		// Remove existing Post
		$scope.remove = function() {
            var post = $scope.post;
			$http.put('/posts/'+post._id+'/remove').success(function (response) {
                $location.path('/posts');
            }).error(function(response){
                $scope.error=response.message;
            });
		};

		// Update existing Post
		$scope.update = function() {
            var post = $scope.post;
            $http.put('/posts/'+post._id,$scope.edit).success(function (response) {
                $location.path('posts/' + response._id);
            }).error(function (response) {
                $scope.error = response.message;
            });
        };



		// Find a list of Posts
		$scope.find = function() {
			$scope.posts = Posts.query();
		};

        $scope.interest= function(){
            var post = $scope.post;

            $http.post('/posts/'+post._id+'/interested').success(function(response){

                $scope.interestedUsers = response.interestedUsers;
                //$location.path('/posts/'+response._id);
                }).error(function(response){
                $scope.error = response.message;
            });

        };

        $scope.findOne = function(){
            //var post = $scope.post;
            $http.get('/posts/'+$stateParams.postId).success(function(post){

                post.isAlreadyInterested = false;
                post.isPoster = false;
                for (var key in post.interestedUsers) {
                    if (post.interestedUsers.hasOwnProperty(key)) {
                        if(post.interestedUsers[key]._id===$scope.authentication.user._id){
                            post.isAlreadyInterested=true;
                        }
                    }
                }
                if(post.user._id===$scope.authentication.user._id){
                    post.isPoster=true;
                }
                $scope.post = post;
            }).error(function(response){
                alert(response.message);
                $scope.error = response.message;
                $location.path('/posts');
            });
        };

	}
]);

@@ -156,16 +156,25 @@
 '$http',
+'$state',
 %0A%09functi
@@ -236,16 +236,23 @@
 ts,$http
+,$state
 ) %7B%0A%09%09$s
@@ -1405,17 +1405,16 @@
 );%0A%09%09%7D;%0A
-%0A
         
@@ -1564,17 +1564,16 @@
 ponse)%7B%0A
-%0A
         
@@ -1651,47 +1651,22 @@
     
-//$location.path('/posts/'+response._id
+$state.reload(
 );%0A 

refresh interest list works