poconsole.factory('Note',function($q, $http, $log){
	var service = {};
	service.getNotesPage = function(page_number) {
        var deferred = $q.defer();
        $http.get('/api/notes?q={"order_by":[{"field":"created_on","direction":"desc"}]}&page=' + page_number).success(function(data) {
        	deferred.resolve(data);
        });
        return deferred.promise;
    };

    service.getNote = function(id){
        var deferred = $q.defer();
        $http.get('/api/notes/' + id).success(function(data) {
        	deferred.resolve(data);
        });
        return deferred.promise;
    };

    service.createNote = function(n){
        var deferred = $q.defer();
        var note_obj = {
            'note_text': n.note_text,
            'author': n.author,
            'created_on': n.created_on
        };
        $http.post('/api/notes', note_obj).success(function(data){
            deferred.resolve(data);
        });
        return deferred.promise;
    };

    service.updateNote = function(n){
        var deferred = $q.defer();
        var note_obj = {
            'note_text': n.note_text,
            'author': n.author,
            'created_on': n.created_on
        };
        $http.put('/api/notes/'+ n._id, data=note_obj).success(function(data){
            deferred.resolve(data);
        });
        return deferred.promise;
    };

    service.deleteNote = function(id){
        var deferred = $q.defer();
        var url = '/api/notes/' + id;
        $http.delete(url).success(function(data) {
            deferred.resolve({
                data: data
            });
        });
        return deferred.promise;
    };



    return service;
});
