{"size": 1133, "ext": "js", "max_stars_repo_path": "app/scripts/services/authors.js", "max_stars_repo_name": "Labx/LibraryBox-app", "max_stars_repo_stars_event_min_datetime": "2015-02-18T07:11:44.000Z", "max_stars_repo_stars_event_max_datetime": "2016-11-09T05:20:22.000Z", "max_issues_repo_path": "app/scripts/services/authors.js", "max_issues_repo_name": "Labx/LibraryBox-app", "max_issues_repo_issues_event_min_datetime": "2015-02-17T20:47:07.000Z", "max_issues_repo_issues_event_max_datetime": "2015-06-11T20:56:15.000Z", "max_forks_repo_path": "app/scripts/services/authors.js", "max_forks_repo_name": "Labx/LibraryBox-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 20.2321428571, "max_line_length": 65, "alphanum_fraction": 0.5119152692}
'use strict';

/**
 * @ngdoc service
 * @name libraryboxApp.authors
 * @description
 * # authors
 * Service in the libraryboxApp.
 */

angular.module('libraryboxApp')
.factory('Authors', ['$http', '$q', function($http, $q) {

  var all = function() {
    var deferred = $q.defer();
    $http.get('/data/dump-authors.json').success(function(data) {
        var authors = [];
        for (var i = 0; i < data.length; i ++) {
          authors.push(data[i]);
        }
        deferred.resolve(authors);
    });
    return deferred.promise;
  };

  var find = function(id) {
    var deferred = $q.defer();
    var authors = [];
    var author = {};
    all().then(function(data) {
      authors = data;
      for (var i = authors.length - 1; i >= 0; i--) {
        if (authors[i].id == id) {
          author = authors[i];
        }
      }
      if (typeof(author) == 'undefined') {
        deferred.resolve({
          status: 404,
          message: 'Record not found'
        });
      } else {
        deferred.resolve(author);
      }
    });
    return deferred.promise;
  };


  return {
    all: all,
    find: find
  };

}]);
