From 5a3a2e34bd2f7286ed73cded06c3cc2000d0a5f3 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 4 Mar 2022 20:24:27 +0000 Subject: [PATCH] manage transactions via hooks begin before request, commit after, unless there was a 5xx error, in which case rollback --- lib/App/LazyShoppingList/API/v1.pm | 33 ++++++++++++++---------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/App/LazyShoppingList/API/v1.pm b/lib/App/LazyShoppingList/API/v1.pm index 3e3565b..6a34491 100644 --- a/lib/App/LazyShoppingList/API/v1.pm +++ b/lib/App/LazyShoppingList/API/v1.pm @@ -15,6 +15,21 @@ use experimental 'signatures'; set charset => 'UTF-8'; set serializer => 'JSON'; +hook before_request => sub { + get_database->txn_begin; +}; + +hook after_request => sub { + my $dbh = get_database; + + if (response->status =~ /^5/) { + $dbh->txn_rollback; + } + else { + $dbh->txn_commit; + } +}; + # get the URI for the list of shopping lists get '/' => sub { return { lists => uri_for('/list') }; @@ -23,7 +38,6 @@ get '/' => sub { # get the list of shopping lists get '/list' => sub { my $dbh = get_database; - $dbh->txn_begin; my %r = ( lists_version => get_lists_version($dbh), lists => [] ); for my $list ( @@ -36,7 +50,6 @@ get '/list' => sub { { uri => uri_for( "/list/" . $list->id ), name => $list->name }; } - $dbh->txn_commit; return \%r; }; @@ -50,15 +63,12 @@ post '/list' => sub { } my $dbh = get_database; - $dbh->txn_begin; my $list = $dbh->resultset('ShoppingList')->create({ name => $name}); $list->discard_changes; my $lists_ver = increment_lists_version($dbh); - $dbh->txn_commit; - return { uri => uri_for( '/list/' . $list->id ), version => $list->version, @@ -75,14 +85,12 @@ get '/list/:list_id' => sub { warn $dbh; my %r = ( items => [] ); - $dbh->txn_begin; $r{lists_version} = get_lists_version($dbh); my $list = $dbh->resultset('ShoppingList')->find($list_id); unless ($list) { - $dbh->txn_commit; return exception 404, "No list with that ID found"; } @@ -102,8 +110,6 @@ get '/list/:list_id' => sub { }; } - $dbh->txn_commit; - return \%r; }; @@ -119,7 +125,6 @@ post '/list/:id' => sub { my $done = JSON->boolean( $req->{done} // 0 ); my $dbh = get_database; - $dbh->txn_begin; my %r = ( lists_version => get_lists_version($dbh), @@ -128,7 +133,6 @@ post '/list/:id' => sub { my $list = $dbh->resultset('ShoppingList')->find($list_id); unless ($list) { - $dbh->txn_commit; exception 404, "No such list"; } @@ -146,8 +150,6 @@ post '/list/:id' => sub { $r{version} = $item->version; $r{list_version} = $list->version; - $dbh->txn_commit; - return \%r; }; @@ -171,7 +173,6 @@ put '/list/:list_id/:item_id' => sub { or return invalid_input('bad version'); my $dbh = get_database; - $dbh->txn_begin; my %r = ( lists_version => get_lists_version($dbh), @@ -179,13 +180,11 @@ put '/list/:list_id/:item_id' => sub { my $list = $dbh->resultset('ShoppingList')->find($list_id); unless ($list) { - $dbh->txn_commit; return exception 404, "No such list"; } my $item = $dbh->resultset('ShoppingListItem')->find({shopping_list => $list->id, id => $item_id}); unless ($item) { - $dbh->txn_commit; return exception 404, "No such item"; } @@ -194,7 +193,6 @@ put '/list/:list_id/:item_id' => sub { or defined($done) and $done != $item->done ) { unless ($version == $item->version) { - $dbh->txn_commit; return exception 409, sprintf( 'Outdated version (current is %d)', $item->version ); } @@ -212,7 +210,6 @@ put '/list/:list_id/:item_id' => sub { $r{version} = $item->version; $r{list_version} = $list->version; - $dbh->txn_commit; return \%r; }; -- 2.39.2