'use strict';

/**
 * @ngdoc service
 * @name ExpenseManagerApp.ExpenseService
 * @description
 * # ExpenseService
 * Factory in the ExpenseManagerApp.
 */
angular.module('ExpenseManagerApp')

  .factory('ExpenseService', function () {
    var prefix = 'expense-manager';
    var expenseService = {};

    expenseService.saveExpense = function (data) {
      var timeStamp = Math.round(new Date().getTime());
      var key = prefix + timeStamp;

      data = JSON.stringify(data);
      localStorage[key] = data;
    };

    expenseService.getExpense = function () {
      var expenses = [];
      Object.keys(localStorage).forEach(function (key) {
        if (key.substring(0, prefix.length) === prefix) {
          var item = localStorage[key];
          item = JSON.parse(item);
          expenses.push(item);
        }
      });
      return expenses;
    };

    return expenseService;
  });

@@ -853,24 +853,469 @@
 es;%0A    %7D;%0A%0A
+    expenseService.getCategoryTotal = function (category) %7B%0A      var categoryTotal = 0;%0A      Object.keys(localStorage).forEach(function (key) %7B%0A        if (key.substring(0, prefix.length) === prefix) %7B%0A          var item = localStorage%5Bkey%5D;%0A          item = JSON.parse(item);%0A          if (item.category === category) %7B%0A            categoryTotal += parseFloat(item.amount);%0A          %7D%0A        %7D%0A      %7D);%0A      return categoryTotal;%0A    %7D;%0A%0A
     return e

get totals for given category