{"size": 1838, "ext": "js", "max_stars_repo_path": "src/main/webapp/static/js/controller/user_controller.js", "max_stars_repo_name": "Rahul-Karma/myapp", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/main/webapp/static/js/controller/user_controller.js", "max_issues_repo_name": "Rahul-Karma/myapp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/main/webapp/static/js/controller/user_controller.js", "max_forks_repo_name": "Rahul-Karma/myapp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.8873239437, "max_line_length": 81, "alphanum_fraction": 0.4853101197}
'use strict';
App.controller('UserController', ['$scope', 'User', function($scope, User) {
    var self = this;
    self.user= new User();
     
    self.users=[];
         
    self.fetchAllUsers = function(){
        self.users = User.query();
    };
      
    self.createUser = function(){
        self.user.$save(function(){
            self.fetchAllUsers();
        });
    };
     
    self.updateUser = function(){
        self.user.$update(function(){
            self.fetchAllUsers();
        });
    };

   self.deleteUser = function(identity){
       var user = User.get({id:identity}, function() {
            user.$delete(function(){
                console.log('Deleting user with id ', identity);
                self.fetchAllUsers();
            });
       });
    };

    self.fetchAllUsers();

    self.submit = function() {
        if(self.user.id==null){
            console.log('Saving New User', self.user);    
            self.createUser();
        }else{
            console.log('Upddating user with id ', self.user.id);
            self.updateUser();
            console.log('User updated with id ', self.user.id);
        }
        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) {//If it is the one shown on screen, reset screen
           self.reset();
        }
        self.deleteUser(id);
    };

     
    self.reset = function(){
        self.user= new User();
        $scope.myForm.$setPristine(); //reset Form
    };

}]);