From c4681ffc3ac819d6cba936e599c1cfbfae1b4b26 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 10 Nov 2017 16:45:58 +0000 Subject: [PATCH 1/3] web app api leaderboard added --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index d909fad..b2f1a71 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -109,4 +109,69 @@ sub post_leaderboards { }); } +sub post_leaderboards_paged { + my $c = shift; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + + my $leaderboard_rs = $c->schema->resultset('Leaderboard'); + + $validation->required('type')->in_resultset( 'type', $leaderboard_rs ); + $validation->optional('page')->number; + + return $c->api_validation_error if $validation->has_error; + + my $page = 1; + + my $today_board = $leaderboard_rs->get_latest( $validation->param('type') ); + + if ( !defined $validation->param('page') || $validation->param('page') < 1 ) { + my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id }); + if { $user_position > 10 ) { + my $math1; + int($user_position / 10) + 1 = $page; + } + } + + my $today_values = $today_board->values->search( + {}, + { + page => $validation->param('page') || $page, + rows => 10, + order_by => { -asc => 'me.position' }, + columns => [ + qw/ + me.value + me.trend + me.position + /, + { display_name => 'customer.display_name' }, + ], + join => { entity => 'customer' }, + }, + ); + $today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' ); + + my @leaderboard_array = $today_values->all; + + if ( $validation->param('type') =~ /total$/ ) { + @leaderboard_array = (map { + { + %$_, + value => $_->{value} / 100000, + } + } @leaderboard_array); + } + + my $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id }); + + return $c->render( json => { + success => Mojo::JSON->true, + leaderboard => [ @leaderboard_array ], + user_position => defined $current_user_position ? $current_user_position->{position} : 0, + page => $validation->param('page') || $page, + }); +} + 1; From 049b4836c5b8aee55c32af57c215e8d7109f1fb0 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 10 Nov 2017 17:07:41 +0000 Subject: [PATCH 2/3] Added code to leaderboard web app API --- lib/Pear/LocalLoop.pm | 1 + lib/Pear/LocalLoop/Controller/Api/Stats.pm | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 9890811..d37e5b9 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -148,6 +148,7 @@ sub startup { $api->post('/user-history')->to('api-user#post_user_history'); $api->post('/stats')->to('api-stats#post_index'); $api->post('/stats/leaderboard')->to('api-stats#post_leaderboards'); + $api->post('/stats/leaderboard/paged')->to('api-stats#post_leaderboards_paged'); $api->post('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index b2f1a71..6081f3e 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -129,7 +129,6 @@ sub post_leaderboards_paged { if ( !defined $validation->param('page') || $validation->param('page') < 1 ) { my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id }); if { $user_position > 10 ) { - my $math1; int($user_position / 10) + 1 = $page; } } @@ -171,6 +170,7 @@ sub post_leaderboards_paged { leaderboard => [ @leaderboard_array ], user_position => defined $current_user_position ? $current_user_position->{position} : 0, page => $validation->param('page') || $page, + page_no => $today_values->pager->total_entries, }); } From 105c9093b836e4a363961de79bf1ddd6b705e5ab Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 10 Nov 2017 18:39:00 +0000 Subject: [PATCH 3/3] fixes --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 6081f3e..ad984e2 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -128,15 +128,15 @@ sub post_leaderboards_paged { if ( !defined $validation->param('page') || $validation->param('page') < 1 ) { my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id }); - if { $user_position > 10 ) { - int($user_position / 10) + 1 = $page; - } + $page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1; + } else { + $page = $validation->param('page'); } my $today_values = $today_board->values->search( {}, { - page => $validation->param('page') || $page, + page => $page, rows => 10, order_by => { -asc => 'me.position' }, columns => [ @@ -169,8 +169,8 @@ sub post_leaderboards_paged { success => Mojo::JSON->true, leaderboard => [ @leaderboard_array ], user_position => defined $current_user_position ? $current_user_position->{position} : 0, - page => $validation->param('page') || $page, - page_no => $today_values->pager->total_entries, + page => $page, + count => $today_values->pager->total_entries, }); }