From 73d44feaced188add64c128154d1c4d2ac782360 Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 20 Mar 2018 18:43:00 +0000 Subject: [PATCH] added deleting and updating transactions --- lib/Pear/LocalLoop.pm | 2 + .../LocalLoop/Controller/Api/Transactions.pm | 94 ++++++++++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 5bd5e26..c996567 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -154,6 +154,8 @@ sub startup { $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'); + $api->post('/recurring-transactions')->to('api-transactions#update_recurring'); + $api->post('/recurring-transactions/delete')->to('api-transactions#delete_recurring'); my $api_v1 = $api->under('/v1'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Transactions.pm b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm index e5bbc94..e2c4c18 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Transactions.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm @@ -8,6 +8,21 @@ has error_messages => sub { required => { message => 'No email sent.', status => 400 }, email => { message => 'Email is invalid.', status => 400 }, }, + value => { + required => { message => 'transaction amount is missing', status => 400 }, + number => { message => 'transaction amount does not look like a number', status => 400 }, + gt_num => { message => 'transaction amount cannot be equal to or less than zero', status => 400 }, + }, + apply_time => { + required => { message => 'purchase time is missing', status => 400 }, + is_full_iso_datetime => { message => 'time is in incorrect format', status => 400 }, + }, + id => { + required => { message => 'Recurring Transaction not found', status => 400 }, + }, + category => { + in_resultset => { message => 'Category is invalid', status => 400 }, + }, }; }; @@ -70,9 +85,84 @@ sub update_recurring { my $user = $c->stash->{api_user}; my $validation = $c->validation; - $validation->input( $c->stash->{api_json} ); - #TODO check that user matches seller on database before updating for that id + $validation->required('id'); + + return $c->api_validation_error if $validation->has_error; + + my $id = $validation->param('id'); + + my $recur_transaction = $c->schema->resultset('TransactionRecurring')->find($id); + unless ( $recur_transaction ) { + return $c->render( + json => { + success => Mojo::JSON->false, + message => 'Error Finding Recurring Transaction', + error => 'recurring_error', + }, + status => 400, + ); + } + + $validation->required('recurring_period'); + $validation->required('apply_time')->is_full_iso_datetime; + $validation->optional('category')->in_resultset( 'id', $c->schema->resultset('Category')); + $validation->optional('essential'); + $validation->required('value'); + + return $c->api_validation_error if $validation->has_error; + + my $apply_time = $c->parse_iso_datetime($validation->param('apply_time')); + + $c->schema->storage->txn_do( sub { + $recur_transaction->update({ + start_time => $c->format_db_datetime($apply_time), + last_updated => undef, + category_id => $validation->param('category'), + essential => $validation->param('essential'), + value => $validation->param('value') * 100000, + recurring_period => $validation->param('recurring_period'), + }); + }); + + return $c->render( json => { + success => Mojo::JSON->true, + message => 'Recurring Transaction Updated Successfully', + }); + +} + +sub delete_recurring { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->required('id'); + + return $c->api_validation_error if $validation->has_error; + + my $id = $validation->param('id'); + + my $recur_transaction = $c->schema->resultset('TransactionRecurring')->find($id); + unless ( $recur_transaction ) { + return $c->render( + json => { + success => Mojo::JSON->false, + message => 'Error Finding Recurring Transaction', + error => 'recurring_error', + }, + status => 400, + ); + } + + $recur_transaction->delete; + + return $c->render( json => { + success => Mojo::JSON->true, + message => 'Recurring Transaction Delete Successfully', + }); }