Skip to content

Commit

Permalink
Merge pull request #69 from Pear-Trading/finn/appleaderboard
Browse files Browse the repository at this point in the history
Web App Leaderboard endpoint
  • Loading branch information
TBSliver authored Nov 13, 2017
2 parents afac877 + 105c909 commit 6b2b618
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Pear/LocalLoop.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down
65 changes: 65 additions & 0 deletions lib/Pear/LocalLoop/Controller/Api/Stats.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
$page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1;
} else {
$page = $validation->param('page');
}

my $today_values = $today_board->values->search(
{},
{
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 => $page,
count => $today_values->pager->total_entries,
});
}

1;

0 comments on commit 6b2b618

Please sign in to comment.