added deleting and updating transactions
This commit is contained in:
parent
2cf0678126
commit
73d44feace
2 changed files with 94 additions and 2 deletions
|
@ -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');
|
||||
|
|
|
@ -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',
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue