From 6ef15e84e77bb06afb4e6b95847f90f49d53ab2f Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 6 Mar 2022 09:38:26 +0000 Subject: [PATCH] start item management, delete works --- doc/protocol.md | 11 ++++++ lib/App/LazyShoppingList/API/v1.pm | 32 +++++++++++++++++ public/javascripts/lsl.js | 57 ++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/doc/protocol.md b/doc/protocol.md index 25ab284..7d5de4e 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -132,3 +132,14 @@ Possible error responses: the differences to the user and either cancel the request and keep the newly fetched data as current, or re-submit the change using the fresh version number. + +DELETE /list/$list_id/$item_id +------------------------------ + +Deletes a list item. No request body. Always succeeds, even if the list item +doesn't exist server-side, in which case the `list_version` isn't changed. + +Returns JSON object with the following keys: + + - `list_version`: the new version of the shopping list whose item was deleted. + - `lists_version`: the current version of the list of shopping lists diff --git a/lib/App/LazyShoppingList/API/v1.pm b/lib/App/LazyShoppingList/API/v1.pm index e481464..c13137a 100644 --- a/lib/App/LazyShoppingList/API/v1.pm +++ b/lib/App/LazyShoppingList/API/v1.pm @@ -274,4 +274,36 @@ del '/list/:list_id' => sub { 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; +}; + true; diff --git a/public/javascripts/lsl.js b/public/javascripts/lsl.js index 78a53b0..09f7144 100644 --- a/public/javascripts/lsl.js +++ b/public/javascripts/lsl.js @@ -22,6 +22,7 @@ function add_list_item(data) { var item = $('
  • ').addClass('list-item-row').data({ 'lsl-uri': data.uri, 'lsl-version': data.version, + 'lsl-description': data.description, 'lsl-done': data.done}); var cb = $(''); if (data.done) cb.prop('checked', true); @@ -269,6 +270,61 @@ function new_list_item_submission_done(data) { $('#new-list-item input').val(''); } +function delete_list_item(dlg, li) { + $.ajax(li.data('lsl-uri'), + { type: 'DELETE' }) + .done((resp) => { + li.remove(); + dlg.dialog('destroy'); + selected_list.data('lsl-version', selected_list.data('lsl-version')+1); + got_list_version(resp.list_version); + got_lists_version(resp.lists_version); + }); +} +function edit_list_item(li) { + var d = $('
    ') + .append( + $('
    ') + .append( + $('').text('Item name'), + $('') + .val(li.data('lsl-description')) + ) + ); + + d.dialog({ + dialogClass: 'edit-item-dialog', + autoOpen: true, + modal: true, + title: 'Edit item', + width: 'max-content', + buttons: [ + { + class: 'btn-delete', + icon: 'ui-icon-trash', + click: () => { + delete_list_item(d, li); + }, + }, + { + text: 'Cancel', + click: ()=>{ d.dialog('destroy'); }, + }, + { + icon: 'ui-icon-disk', + text: 'OK', + click: () => { + save_list_item(d, li); + }, + }, + ], + }); +} +function handle_list_item_edit(ev) { + var li = $(ev.target).closest('li'); + edit_list_item(li); + return false; +} function handle_new_list_item_submission(){ var description = $('#new-list-item input[type="text"]').val().trim(); if (description.length == 0) return; @@ -344,6 +400,7 @@ $(function(){ edit_list(); return false; }); + $('#list-items').on('click', '>li .edit-trigger', handle_list_item_edit); $('#new-list-item button').on('click', handle_new_list_item_submission); $('#new-list-item input').on('keypress', ev => { if (13 == ev.keyCode) { -- 2.39.2