From: Damyan Ivanov Date: Sun, 6 Mar 2022 08:56:29 +0000 (+0000) Subject: working modification of shopping lists X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=a720b382c5ce6629148bdf6a0523555b9f921b77;p=lsl.git working modification of shopping lists --- 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'); diff --git a/public/javascripts/lsl.js b/public/javascripts/lsl.js index c0125de..97c0dfe 100644 --- a/public/javascripts/lsl.js +++ b/public/javascripts/lsl.js @@ -152,6 +152,24 @@ function delete_list(dlg) { dlg.dialog('destroy'); got_lists_version(d.lists_version); }); +} +function save_list(dlg) { + var lists_ver = lists_version; + var new_name = dlg.find('input[type="text"]').val(); + + $.ajax( selected_list.data('lsl-uri'), + { type: 'PUT', + data: JSON.stringify({ name: new_name, + version: selected_list.data('lsl-version')}) + } + ) + .done((d)=>{ + lists_version = lists_ver + 1; + + selected_list.find('.list-name').text(new_name); + selected_list.data('lsl-name', new_name); + $('#selected-list-name').text(new_name); + dlg.dialog('destroy'); got_lists_version(d.lists_version); });