]> git.ktnx.net Git - lsl.git/blobdiff - lib/App/LazyShoppingList/API/v1.pm
fix updating only done state or only item descriptiom
[lsl.git] / lib / App / LazyShoppingList / API / v1.pm
index 6a34491a00f4535202c834adf93c236c75957c92..185c0947ba85960a13db52b2de72e60ede8a7d21 100644 (file)
@@ -166,7 +166,8 @@ put '/list/:list_id/:item_id' => sub {
     my $req = request_data;
 
     my $descr = $req->{description};
-    my $done  = JSON->boolean( $req->{done} // 0 );
+    my $done =
+        exists $req->{done} ? JSON->boolean( $req->{done} // 0 ) : undef;
     my $version = $req->{version};
 
     length($version) and $version =~ /^\d{1,18}$/
@@ -210,6 +211,98 @@ put '/list/:list_id/:item_id' => sub {
     $r{version} = $item->version;
     $r{list_version} = $list->version;
 
+    return \%r;
+};
+
+# modify shopping 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');
+    length($list_id) and $list_id =~ /^\d{1,18}$/
+        or return invalid_input('bad list ID');
+
+    my $dbh = get_database;
+
+    my %r = (
+        lists_version => get_lists_version($dbh),
+    );
+
+    my $list = $dbh->resultset('ShoppingList')->find($list_id);
+    if ($list) {
+        $list->delete;
+        $r{lists_version} = increment_lists_version($dbh)
+    }
+
+    return \%r;
+};
+
+# delete shopping list item
+del '/list/:list_id/:item_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 $item_id = route_parameters->get('item_id');
+    length($item_id) and $item_id =~ /^\d{1,18}$/
+        or return invalid_input('bad item ID');
+
+    my $dbh = get_database;
+
+    my %r = (
+        lists_version => get_lists_version($dbh),
+    );
+
+    my $list = $dbh->resultset('ShoppingList')->find($list_id)
+        or return exception 404, 'No such list';
+
+    my $item = $dbh->resultset('ShoppingListItem')
+        ->find( { shopping_list => $list->id, id => $item_id } );
+
+    if ($item) {
+        $item->delete;
+        $list->update({version => $list->version + 1});
+    }
+
+    $r{list_version} = $list->version;
 
     return \%r;
 };