From a097db2017a83d39257c577bcdb56d7509ff402f Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 21 Apr 2017 23:38:12 +0100 Subject: [PATCH] Fix home page upload to use json api instead of seperate upload code --- lib/Pear/LocalLoop.pm | 4 +- lib/Pear/LocalLoop/Controller/Portal.pm | 111 +----------------------- public/static/user/js/home.js | 19 +++- templates/admin/users/index.html.ep | 2 +- templates/admin/users/read.html.ep | 4 +- templates/root/home.html.ep | 2 +- 6 files changed, 27 insertions(+), 115 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 3366cf7..0449d52 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -142,7 +142,9 @@ sub startup { $user_routes->get('/home')->to('root#home'); - $user_routes->post('/portal/upload')->to('portal#post_upload'); + my $portal_api = $r->under('/portal')->to('api-auth#check_json')->under('/')->to('portal#under'); + + $portal_api->post('/upload')->to('api-upload#post_upload'); $self->hook( before_dispatch => sub { my $self = shift; diff --git a/lib/Pear/LocalLoop/Controller/Portal.pm b/lib/Pear/LocalLoop/Controller/Portal.pm index 176ee18..5e4b4ef 100644 --- a/lib/Pear/LocalLoop/Controller/Portal.pm +++ b/lib/Pear/LocalLoop/Controller/Portal.pm @@ -1,116 +1,11 @@ package Pear::LocalLoop::Controller::Portal; use Mojo::Base 'Mojolicious::Controller'; -has error_messages => sub { - return { - transaction_type => { - required => { message => 'transaction_type is missing.', status => 400 }, - in => { message => 'transaction_type is not a valid value.', status => 400 }, - }, - transaction_value => { - required => { message => 'transaction_value is missing', status => 400 }, - number => { message => 'transaction_value does not look like a number', status => 400 }, - gt_num => { message => 'transaction_value cannot be equal to or less than zero', status => 400 }, - }, - file => { - required => { message => 'No file uploaded', status => 400 }, - upload => { message => 'file key does not contain a file', status => 400 }, - filetype => { message => 'File must be of type image/jpeg', status => 400 }, - }, - organisation_id => { - required => { message => 'organisation_id is missing', status => 400 }, - number => { message => 'organisation_id is not a number', status => 400 }, - in_resultset => { message => 'organisation_id does not exist in the database', status => 400 }, - }, - organisation_name => { - required => { message => 'organisation_name is missing', status => 400 }, - }, - }; -}; - -sub post_upload { +sub under { my $c = shift; - my $user = $c->current_user; - my $validation = $c->validation; - - $validation->required('file')->upload->filetype('image/jpeg'); - $validation->required('transaction_value')->number->gt_num(0); - $validation->required('transaction_type')->in( 1, 2, 3 ); - - # First pass of required items - return $c->api_validation_error if $validation->has_error; - - my $type = $validation->param('transaction_type'); - - if ( $type == 1 ) { - # Validated Organisation - my $valid_org_rs = $c->schema->resultset('Organisation'); - $validation->required('organisation_id')->number->in_resultset( 'organisationalid', $valid_org_rs ); - } elsif ( $type == 2 ) { - # Unvalidated Organisation - my $valid_org_rs = $c->schema->resultset('PendingOrganisation')->search({ usersubmitted_fk => $user->id }); - $validation->required('organisation_id')->number->in_resultset( 'pendingorganisationid', $valid_org_rs ); - } elsif ( $type == 3 ) { - # Unknown Organisation - $validation->required('organisation_name'); - $validation->optional('street_name'); - $validation->optional('town'); - $validation->optional('postcode')->postcode; - } - - return $c->api_validation_error if $validation->has_error; - - my $transaction_value = $validation->param('transaction_value'); - my $upload = $validation->param('file'); - - my $file = $c->store_file_from_upload( $upload ); - - if ( $type == 1 ) { - # Validated organisation - $c->schema->resultset('Transaction')->create({ - buyeruserid_fk => $user->id, - sellerorganisationid_fk => $validation->param('organisation_id'), - valuemicrocurrency => $transaction_value, - proof_image => $file, - timedatesubmitted => DateTime->now, - }); - } elsif ( $type == 2 ) { - # Unvalidated Organisation - $c->schema->resultset('PendingTransaction')->create({ - buyeruserid_fk => $user->id, - pendingsellerorganisationid_fk => $validation->param('organisation_id'), - valuemicrocurrency => $transaction_value, - proof_image => $file, - timedatesubmitted => DateTime->now, - }); - } elsif ( $type == 3 ) { - my $organisation_name = $validation->param('organisation_name'); - my $street_name = $validation->param('street_name'); - my $town = $validation->param('town'); - my $postcode = $validation->param('postcode'); - - my $pending_org = $c->schema->resultset('PendingOrganisation')->create({ - submitted_by => $user, - submitted_at => DateTime->now, - name => $organisation_name, - street_name => $street_name, - town => $town, - postcode => $postcode, - }); - - $c->schema->resultset('PendingTransaction')->create({ - buyeruserid_fk => $user->id, - pendingsellerorganisationid_fk => $pending_org->id, - valuemicrocurrency => $transaction_value, - proof_image => $file, - timedatesubmitted => DateTime->now, - }); - } - return $c->render( json => { - success => Mojo::JSON->true, - message => 'Upload Successful', - }); + $c->stash( api_user => $c->current_user ); + return 1; } 1; diff --git a/public/static/user/js/home.js b/public/static/user/js/home.js index f42fd0d..5f618da 100644 --- a/public/static/user/js/home.js +++ b/public/static/user/js/home.js @@ -4,14 +4,29 @@ $(function() { event.stopPropagation(); // Create new form data object with the contents of this form - var formData = new FormData(this); + var formData = new FormData(); + formData.append('file', $('#tran-file')[0].files[0]); + formData.append('json', JSON.stringify({ + transaction_type: $('#tran-type').val(), + organisation_name: $('#org-name').val(), + street_name: $('#org-street').val(), + town: $('#org-town').val(), + postcode: $('#org-postcode').val(), + transaction_value: $('#tran-value').val() + })); $.ajax({ url: $(this).attr("action"), type: 'POST', data: formData, success: function(data) { - alert(data); + console.log(data); + alert(data.message); + $('form#receipt-form')[0].reset(); + }, + error: function(data) { + console.log(data); + alert(data.responseJSON.message); }, cache: false, contentType: false, diff --git a/templates/admin/users/index.html.ep b/templates/admin/users/index.html.ep index 2fc74bc..b31330a 100644 --- a/templates/admin/users/index.html.ep +++ b/templates/admin/users/index.html.ep @@ -13,7 +13,7 @@ % }
% for my $user (@$users) { - +
%= $user->{email}
diff --git a/templates/admin/users/read.html.ep b/templates/admin/users/read.html.ep index 8002e1b..eaca969 100644 --- a/templates/admin/users/read.html.ep +++ b/templates/admin/users/read.html.ep @@ -18,11 +18,11 @@
- +
- +
diff --git a/templates/root/home.html.ep b/templates/root/home.html.ep index 747f1c8..54a8251 100644 --- a/templates/root/home.html.ep +++ b/templates/root/home.html.ep @@ -7,7 +7,7 @@

Submit Receipt

- +