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') };
# 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 (
{ uri => uri_for( "/list/" . $list->id ), name => $list->name };
}
- $dbh->txn_commit;
return \%r;
};
}
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,
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";
}
};
}
- $dbh->txn_commit;
-
return \%r;
};
my $done = JSON->boolean( $req->{done} // 0 );
my $dbh = get_database;
- $dbh->txn_begin;
my %r = (
lists_version => get_lists_version($dbh),
my $list = $dbh->resultset('ShoppingList')->find($list_id);
unless ($list) {
- $dbh->txn_commit;
exception 404, "No such list";
}
$r{version} = $item->version;
$r{list_version} = $list->version;
- $dbh->txn_commit;
-
return \%r;
};
or return invalid_input('bad version');
my $dbh = get_database;
- $dbh->txn_begin;
my %r = (
lists_version => get_lists_version($dbh),
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";
}
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 );
}
$r{version} = $item->version;
$r{list_version} = $list->version;
- $dbh->txn_commit;
return \%r;
};