'use strict';

module.exports =

/*@ngInject*/
function ConceptsCmsCtrl (
  $scope, ConceptsFBService, _
) {
  $scope.sortType       = 'concept_level_2.name'; // set the default sort type
  $scope.sortReverse    = false;  // set the default sort order
  $scope.searchConcept  = '';     // set the default search/filter term
  ConceptsFBService.get().then(function (c) {
    $scope.concepts = c;
  });

  $scope.getQuestionLength = function (questions) {
    return _.keys(questions).length;
  };

  $scope.getTotalConcepts = function (concepts) {
    return _.keys(concepts).length;
  };

  $scope.getTotalQuestions = function (concepts) {
    return _.reduce(concepts, function (sum, c) {
      if (_.isNaN(sum)) {
        sum = 0;
      }
      return Number(sum) + Number(_.keys(c.questions).length);
    });
  };
};

'use strict';

module.exports =

/*@ngInject*/
function ConceptsCmsCtrl (
  $scope, ConceptsFBService, _
) {
  $scope.sortType       = 'concept_level_2.name'; // set the default sort type
  $scope.sortReverse    = false;  // set the default sort order
  $scope.searchConcept  = '';     // set the default search/filter term
  ConceptsFBService.get().then(function (c) {
    $scope.concepts = addQuestionCountToConcepts(c);
  });

  $scope.getQuestionLength = function (questions) {
    return _.keys(questions).length;
  };

  $scope.getTotalConcepts = function (concepts) {
    return _.keys(concepts).length;
  };

  $scope.getTotalQuestions = function (concepts) {
    return _.reduce(concepts, function (sum, c) {
      if (_.isNaN(sum)) {
        sum = 0;
      }
      return Number(sum) + Number(_.keys(c.questions).length);
    });
  };

  function addQuestionCountToConcepts (concepts) {
    return _.map(concepts, function (c) {
      c.questionCount = _.keys(c.questions).length;
      return c;
    });
  };
};

Use a map to calculate the question counts for concepts all in once go and store them on the concept object
Use a map to calculate the question counts for concepts all in once go and store them on the concept object

This allows us to sort the table by question count and saves us calculating the question count when trying to render each row of the table.
