angular.module('tasksApp', [])
  .controller('TaskController', function($http) {
    var controller = this;
    controller.tasks = [];
    controller.updateMessage = '';
    
    var getTasks = function() {
      $http.get('tasks')
           .success(function(data) {
             controller.tasks = data;
           });      
    }
    
    var success = function(data) {
      controller.updateMessage = data;
      controller.getTasks();
    }
    
    var addATask = function(task) {
      $http.post('tasks/add', controller.newTask)
           .success(success);
    }

    var deleteTask = function(task) {
      $http.post('tasks/delete', task)
           .success(success);
    }

    controller.getTasks = getTasks;
    controller.getTasks();
    controller.addATask = addATask;
    controller.deleteTask = deleteTask;
  });