From 6cd7df12592a21704b8965cfa15ed6aea637dad5 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 22 Jan 2018 16:26:45 +0000 Subject: [PATCH] working API with arrays --- .../LocalLoop/Controller/Api/Categories.pm | 32 +++++++++++-------- .../ViewQuantisedTransactionCategorySQLite.pm | 20 ++++++------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Categories.pm b/lib/Pear/LocalLoop/Controller/Api/Categories.pm index 79751eb..6412dfc 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Categories.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Categories.pm @@ -8,11 +8,9 @@ sub post_category_list { my $entity = $c->stash->{api_user}->entity; - my $duration = DateTime::Duration->new( days => 30 ); - my $start = DateTime->today; - my $end = $start->clone->subtract_duration( $duration ); - - my $data = { days => [], category => [], value => [] }; + my $duration = DateTime::Duration->new( days => 28 ); + my $end = DateTime->today; + my $start = $end->clone->subtract_duration( $duration ); my $dtf = $c->schema->storage->datetime_parser; my $driver = $c->schema->storage->dbh->{Driver}->{Name}; @@ -29,26 +27,34 @@ sub post_category_list { { columns => [ { - quantised => 'quantised_days', - count => \"COUNT(*)", + quantised => 'quantised_weeks', + value => 'value', + category_id => 'category_id', } ], - group_by => 'quantised_days', - order_by => { '-asc' => 'quantised_days' }, + order_by => { '-desc' => 'quantised_weeks' }, } ); + my $data = {}; + for ( $month_transaction_rs->all ) { my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised')); - push @{ $data->{ days } }, ($c->format_iso_datetime( $quantised ) || 0); - push @{ $data->{ category } }, ($_->get_column('category_id') || 0); - push @{ $data->{ value } }, ($_->get_column('value') || 0) / 100000; + my $days = $c->format_iso_date( $quantised ) || 0; + my $category = $_->get_column('category_id') || 0; + my $value = ($_->get_column('value') || 0) / 100000; + $data->{$days} = [] unless exists $data->{$days}; + push @{ $data->{$days} }, { + days => $days, + value => $value, + category => $category, + }; } return $c->render( json => { success => Mojo::JSON->true, - graph => $data, + data => $data, } ); } diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm index 27e4ab7..fcef5a2 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm @@ -10,17 +10,17 @@ __PACKAGE__->table('view_quantised_transactions'); __PACKAGE__->result_source_instance->is_virtual(1); __PACKAGE__->result_source_instance->view_definition( qq/ -SELECT "transactions.value", - "transactions.distance", - "transactions.purchase_time", - "transactions.buyer_id", - "transactions.seller_id", - "transaction_category.category_id", - DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions.purchase_time")) AS "quantised_hours", - DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions.purchase_time")) AS "quantised_days", - DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions.purchase_time", 'weekday 1')) AS "quantised_weeks" +SELECT "transactions"."value", + "transactions"."distance", + "transactions"."purchase_time", + "transactions"."buyer_id", + "transactions"."seller_id", + "transaction_category"."category_id", + DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions"."purchase_time")) AS "quantised_hours", + DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time")) AS "quantised_days", + DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time", 'weekday 1')) AS "quantised_weeks" FROM "transactions" -LEFT JOIN "transaction_category" ON "transactions.id" = "transaction_category.transaction_id" +LEFT JOIN "transaction_category" ON "transactions"."id" = "transaction_category"."transaction_id" /); 1;