X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=lib%2FApp%2FLazyShoppingList%2FAPI%2Fv1.pm;h=d09bc647182cb1265de68d08b1a3904bc08f6552;hb=a720b382c5ce6629148bdf6a0523555b9f921b77;hp=cd7be18e24dd5ab4371df4017822a967c0dd1d66;hpb=4af316b9339d4cd09a290549ae223affe61b0d90;p=lsl.git diff --git a/lib/App/LazyShoppingList/API/v1.pm b/lib/App/LazyShoppingList/API/v1.pm index cd7be18..d09bc64 100644 --- a/lib/App/LazyShoppingList/API/v1.pm +++ b/lib/App/LazyShoppingList/API/v1.pm @@ -213,6 +213,46 @@ put '/list/:list_id/:item_id' => sub { return \%r; }; +# modify shipping list +put '/list/:list_id' => sub { + my $list_id = route_parameters->get('list_id'); + length($list_id) and $list_id =~ /^\d{1,18}$/ + or return invalid_input('bad list ID'); + + my $req = request_data; + + my $name = $req->{name} // ''; + my $version = $req->{version} // -1; + + length($version) and $version =~ /^\d{1,18}$/ + or return invalid_input('bad version'); + + my $dbh = get_database; + + my $list = $dbh->resultset('ShoppingList')->find($list_id); + unless ($list) { + return exception 404, "No such list"; + } + + # in case no real changes are needed will return the current state + if ( $name ne ( $list->name // '') ) { + unless ($version == $list->version) { + return exception 409, + sprintf( 'Outdated version (current is %d)', $list->version ); + } + + $list->update({ + name => $name // '', + version => $version + 1, + }); + } + + return { + version => $list->version, + lists_version => increment_lists_version($dbh), + }; +}; + # delete shopping list del '/list/:list_id' => sub { my $list_id = route_parameters->get('list_id');