]> git.ktnx.net Git - lsl.git/blobdiff - lib/App/LazyShoppingList/API/v1.pm
working modification of shopping lists
[lsl.git] / lib / App / LazyShoppingList / API / v1.pm
index cd7be18e24dd5ab4371df4017822a967c0dd1d66..d09bc647182cb1265de68d08b1a3904bc08f6552 100644 (file)
@@ -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');