From 06858fc005b5859ee095e42a34c505a0e8387cb1 Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 5 Sep 2017 16:37:31 +0100 Subject: [PATCH] Added outgoing transaction list API --- lib/Pear/LocalLoop.pm | 1 + .../LocalLoop/Controller/Api/Transactions.pm | 45 +++++++++++++++++++ lib/Pear/LocalLoop/Controller/Api/User.pm | 1 - 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/Pear/LocalLoop/Controller/Api/Transactions.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index c4ee95e..d874fa6 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -154,6 +154,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('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases'); my $api_v1 = $api->under('/v1'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Transactions.pm b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm new file mode 100644 index 0000000..5932116 --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm @@ -0,0 +1,45 @@ +package Pear::LocalLoop::Controller::Api::Transactions; +use Mojo::Base 'Mojolicious::Controller'; +use Mojo::JSON; + +has error_messages => sub { + return { + email => { + required => { message => 'No email sent.', status => 400 }, + email => { message => 'Email is invalid.', status => 400 }, + }, + }; +}; + +sub post_transaction_list_purchases { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->optional('page')->number; + + return $c->api_validation_error if $validation->has_error; + + my $transactions = $user->entity->purchases->search( + undef, { + page => $validation->param('page') || 1, + rows => 10, + order_by => { -desc => 'purchase_time' }, + }, + ); + my @transaction_list = ( + map {{ + seller => $_->entity->name, + value => $_->value, + purchase_time => $_->purchase_time, + }} $transactions->all + ) + return $c->render( json => { + success => Mojo::JSON->true, + transactions => \@transaction_list, + }); +} + +1; diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index b83f04b..dfe9bb3 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -64,7 +64,6 @@ sub post_account { if ( defined $user_result ) { my $email = $user_result->email; - #Needs elsif added for trader page for this similar relevant entry if ( $user_result->type eq 'customer' ) { my $full_name = $user_result->entity->customer->full_name; my $display_name = $user_result->entity->customer->display_name;