From 416ac6343a5698e7627b85c8c5ed1933b1b6ee1d Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 7 Sep 2017 12:53:01 +0100 Subject: [PATCH 1/3] Added ability to delete transactions successfully --- lib/Pear/LocalLoop.pm | 1 + .../Controller/Admin/Transactions.pm | 15 +++++ templates/admin/transactions/read.html.ep | 56 +++++++++++-------- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index d874fa6..7a5697c 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -189,6 +189,7 @@ sub startup { $admin_routes->get('/transactions')->to('admin-transactions#index'); $admin_routes->get('/transactions/:id')->to('admin-transactions#read'); $admin_routes->get('/transactions/:id/image')->to('admin-transactions#image'); + $admin_routes->get('/transactions/:id/delete')->to('admin-transactions#delete'); # my $user_routes = $r->under('/')->to('root#under'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Transactions.pm b/lib/Pear/LocalLoop/Controller/Admin/Transactions.pm index ecb18a0..428fdf9 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Transactions.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Transactions.pm @@ -48,4 +48,19 @@ sub image { } } +sub delete { + my $c = shift; + + my $id = $c->param('id'); + +if ( my $transaction = $c->result_set->find($id) ) { + $transaction->delete; + $c->flash( success => 'Successfully deleted transaction' ); + $c->redirect_to( '/admin/transactions' ); + } else { + $c->flash( error => 'No transaction found' ); + $c->redirect_to( '/admin/transactions' ); + } +} + 1; diff --git a/templates/admin/transactions/read.html.ep b/templates/admin/transactions/read.html.ep index a40f384..5cbf658 100644 --- a/templates/admin/transactions/read.html.ep +++ b/templates/admin/transactions/read.html.ep @@ -11,28 +11,36 @@ Success! <%= $success %> % } -
-
- - +
+

+ Transaction Details + Delete Transaction +

+
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
-
- - -
-
- - -
-
- - -
-
- - -
-
- -
- +
From edac93bef57d7f698f90b2709e3a0a9770ba9d3c Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 7 Sep 2017 13:20:53 +0100 Subject: [PATCH 2/3] Changed deletion method to POST and working test --- lib/Pear/LocalLoop.pm | 2 +- t/admin/transaction.t | 74 +++++++++++++++++++++++ templates/admin/transactions/read.html.ep | 8 ++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 t/admin/transaction.t diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 7a5697c..ef7106d 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -189,7 +189,7 @@ sub startup { $admin_routes->get('/transactions')->to('admin-transactions#index'); $admin_routes->get('/transactions/:id')->to('admin-transactions#read'); $admin_routes->get('/transactions/:id/image')->to('admin-transactions#image'); - $admin_routes->get('/transactions/:id/delete')->to('admin-transactions#delete'); + $admin_routes->post('/transactions/:id/delete')->to('admin-transactions#delete'); # my $user_routes = $r->under('/')->to('root#under'); diff --git a/t/admin/transaction.t b/t/admin/transaction.t new file mode 100644 index 0000000..4419213 --- /dev/null +++ b/t/admin/transaction.t @@ -0,0 +1,74 @@ +use Mojo::Base -strict; + +use FindBin qw/ $Bin /; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../etc", +); +$framework->install_fixtures('users'); + +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $start = DateTime->today->subtract( hours => 12 ); + +# create 30 days worth of data +for my $count ( 0 .. 29 ) { + my $trans_day = $start->clone->subtract( days => $count ); + + create_random_transaction( 'test1@example.com', $trans_day ); + if ( $count % 2 ) { + create_random_transaction( 'test2@example.com', $trans_day ); + } + if ( $count % 3 ) { + create_random_transaction( 'test3@example.com', $trans_day ); + } + if ( $count % 4 ) { + create_random_transaction( 'test4@example.com', $trans_day ); + } +} + +#login to admin +$t->ua->max_redirects(10); +$t->post_ok('/admin', form => { + email => 'admin@example.com', + password => 'abc123', +})->status_is(200); + +#Read valid transaction +$t->get_ok("/admin/transactions/1") + ->status_is(200)->or($framework->dump_error); + +#get stock image for valid transaction +$t->get_ok("/admin/transactions/1/image") + ->status_is(200)->or($framework->dump_error); + +#Delete valid transaction +$t->post_ok("/admin/transactions/1/delete") + ->status_is(200)->or($framework->dump_error) + ->content_like(qr/Successfully deleted transaction/); + +#Read deleted transaction +$t->get_ok("/admin/transactions/1") + ->content_like(qr/No transaction found/); + +sub create_random_transaction { + my $buyer = shift; + my $time = shift; + + my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity; + my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity; + $schema->resultset('Transaction')->create({ + buyer => $buyer_result, + seller => $seller_result, + value => 10, + purchase_time => $time, + }); +} + +done_testing; diff --git a/templates/admin/transactions/read.html.ep b/templates/admin/transactions/read.html.ep index 5cbf658..9eb9596 100644 --- a/templates/admin/transactions/read.html.ep +++ b/templates/admin/transactions/read.html.ep @@ -14,10 +14,14 @@

Transaction Details - Delete Transaction +
+
+ +
+

-
+
From 54c2b7f64d448e7dead97ea43e27730d5d81dfbb Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 7 Sep 2017 13:26:03 +0100 Subject: [PATCH 3/3] Added count to test --- t/admin/transaction.t | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/admin/transaction.t b/t/admin/transaction.t index 4419213..d3a3980 100644 --- a/t/admin/transaction.t +++ b/t/admin/transaction.t @@ -33,6 +33,8 @@ for my $count ( 0 .. 29 ) { } } +is $schema->resultset('Transaction')->count, 87; + #login to admin $t->ua->max_redirects(10); $t->post_ok('/admin', form => { @@ -53,6 +55,8 @@ $t->post_ok("/admin/transactions/1/delete") ->status_is(200)->or($framework->dump_error) ->content_like(qr/Successfully deleted transaction/); +is $schema->resultset('Transaction')->count, 86; + #Read deleted transaction $t->get_ok("/admin/transactions/1") ->content_like(qr/No transaction found/);