From e6357018133d4bcb1b674f475d3d29339cbe5fdd Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Sun, 12 Oct 2014 08:23:36 +0200 Subject: [PATCH] API service --- app.rb | 6 ++--- public/app/index.html | 1 + public/app/js/controllers/BalanceCtrl.js | 29 ++++++++++++------------ public/app/js/services/API.js | 29 ++++++++++++++++++++++++ public/app/js/templates/balance.tpl.html | 25 +++++++++++--------- 5 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 public/app/js/services/API.js diff --git a/app.rb b/app.rb index 4cb0d83d..c5a93798 100644 --- a/app.rb +++ b/app.rb @@ -35,10 +35,10 @@ class LedgerRbApp < Sinatra::Base get '/api/ledger/register/?' do param :period, String, default: nil - param :category, String, required: true + param :categories, String, required: true - { key: params[ :category ], - values: Ledger.register( params[ :period ], params[ :category ] ) } + { key: params[ :categories ], + values: Ledger.register( params[ :period ], params[ :categories ] ) } .to_json end diff --git a/public/app/index.html b/public/app/index.html index 8ec9a08b..b0c9d60c 100644 --- a/public/app/index.html +++ b/public/app/index.html @@ -51,6 +51,7 @@ + diff --git a/public/app/js/controllers/BalanceCtrl.js b/public/app/js/controllers/BalanceCtrl.js index 258e37bf..8b14d320 100644 --- a/public/app/js/controllers/BalanceCtrl.js +++ b/public/app/js/controllers/BalanceCtrl.js @@ -1,6 +1,7 @@ app.controller( 'BalanceCtrl', - [ '$scope', '$http', '$filter', 'ngTableParams', - function ( $scope, $http, $filter, ngTableParams ) { + [ '$scope', '$filter', 'ngTableParams', 'API', + function ( $scope, $filter, ngTableParams, API ) { + console.log(API) $scope.xFunction = function () { return function ( d ) { return d.account; @@ -101,12 +102,8 @@ app.controller( 'BalanceCtrl', }; _($scope.balance.buckets).each( function( bucket ) { - $http.get( '/api/ledger/balance', { - params: { - period: period, - categories: bucket.categories - } - } ) + API.balance( { period: period, + categories: bucket.categories } ) .then( function ( response ) { bucket.data = _.chain( response.data ) .map( function( account ) { @@ -119,12 +116,8 @@ app.controller( 'BalanceCtrl', .value(); _( bucket.data ).each( function ( account ) { - $http.get( '/api/ledger/register', { - params: { - period: period, - category: account.account - } - } ) + API.register( { period: period, + categories: account.account } ) .then( function ( response ) { $scope.balance.details[ account.account ] = response.data; } ); @@ -152,7 +145,7 @@ app.controller( 'BalanceCtrl', $scope.period_offset = $scope.dates_salaries.length - 1; }; - $http.get( '/api/ledger/dates_salaries' ) + API.dates_salaries() .then( function ( response ) { $scope.dates_salaries = response.data; @@ -165,5 +158,11 @@ app.controller( 'BalanceCtrl', } ); } ); + API.accounts() + .then( function ( response ) { + $scope.accounts = response.data.map( function( account_ary ) { + return account_ary.join( ':' ); + } ); + } ); } ] ); diff --git a/public/app/js/services/API.js b/public/app/js/services/API.js new file mode 100644 index 00000000..0142ad6e --- /dev/null +++ b/public/app/js/services/API.js @@ -0,0 +1,29 @@ +app.service( 'API', + [ '$http', + function( $http ) { + this.balance = function( params ) { + return $http.get( '/api/ledger/balance', { + params: { + period: params.period, + categories: params.categories + } + } ); + }; + + this.register = function( params ) { + return $http.get( '/api/ledger/register', { + params: { + period: params.period, + categories: params.categories + } + } ); + }; + + this.dates_salaries = function( ) { + return $http.get( '/api/ledger/dates_salaries' ); + }; + + this.accounts = function( ) { + return $http.get( '/api/ledger/accounts' ); + }; + } ] ); diff --git a/public/app/js/templates/balance.tpl.html b/public/app/js/templates/balance.tpl.html index 7793e19e..4d9af575 100644 --- a/public/app/js/templates/balance.tpl.html +++ b/public/app/js/templates/balance.tpl.html @@ -5,18 +5,21 @@

From {{from_date | date:'longDate'}} to {{to_date | date:'longDate'}}

+
+ prev + Now + next + + +
-
- prev - Now - next -

Balance: {{( balance.buckets[1].total - balance.buckets[0].total ) | number:2}} €