add simple web services for adding or subtracting records from a temp list
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 18 May 2018 18:38:47 +0000 (14:38 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 18 May 2018 21:32:07 +0000 (17:32 -0400)
GET /eg/opac/api/mylist/add?record=45
GET /eg/opac/api/mylist/delete?record=45

The JSON response is a hash containing a mylist key pointing to the list
of bib IDs of contents of the temporary list.

The record parameter can be repeated to allow adding or removing
records as an atomic operation. Note that this change also now available
to /eg/opac/mylist/{add,delete}

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm

index d3aecaa..b92fa47 100644 (file)
@@ -129,7 +129,11 @@ sub load {
     }
 
     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
-        $path =~ /opac\/my(opac\/lists|list)/;
+        $path =~ /opac\/my(opac\/lists|list)/ ||
+        $path =~ m!opac/api/mylist!;
+
+    return $self->load_api_mylist_add if $path =~ m|opac/api/mylist/add|;
+    return $self->load_api_mylist_delete if $path =~ m|opac/api/mylist/delete|;
 
     return $self->load_simple("home") if $path =~ m|opac/home|;
     return $self->load_simple("css") if $path =~ m|opac/css|;
index df7b79f..7309b61 100644 (file)
@@ -10,7 +10,7 @@ my $U = 'OpenILS::Application::AppUtils';
 # Retrieve the users cached records AKA 'My List'
 # Returns an empty list if there are no cached records
 sub fetch_mylist {
-    my ($self, $with_marc_xml) = @_;
+    my ($self, $with_marc_xml, $skip_sort) = @_;
 
     my $list = [];
     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
@@ -59,7 +59,7 @@ sub fetch_mylist {
 
     # Leverage QueryParser to sort the items by values of config.metabib_fields
     # from the items' marc records.
-    if (@$list) {
+    if (@$list && !$skip_sort) {
         my ($sorter, $modifier) = $self->_get_bookbag_sort_params("anonsort");
         my $query = $self->_prepare_anonlist_sorting_query($list, $sorter, $modifier);
         my $args = {
@@ -77,15 +77,8 @@ sub fetch_mylist {
 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
 sub load_mylist_add {
     my $self = shift;
-    my $rec_id = $self->cgi->param('record');
 
-    my ($cache_key, $list) = $self->fetch_mylist;
-    push(@$list, $rec_id);
-
-    $cache_key = $U->simplereq(
-        'open-ils.actor',
-        'open-ils.actor.anon_cache.set_value', 
-        $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
+    my ($cache_key, $list) = $self->_do_mylist_add();
 
     # Check if we need to warn patron about adding to a "temporary"
     # list:
@@ -96,21 +89,84 @@ sub load_mylist_add {
     return $self->mylist_action_redirect($cache_key);
 }
 
-sub load_mylist_delete {
+sub load_api_mylist_add {
     my $self = shift;
-    my $rec_id = $self->cgi->param('record');
 
-    my ($cache_key, $list) = $self->fetch_mylist;
-    $list = [ grep { $_ ne $rec_id } @$list ];
+    my ($cache_key, $list) = $self->_do_mylist_add();
+
+    $self->ctx->{json_response} = {
+        mylist => [ map { 0 + $_ } @$list ], # force integers
+    };
+    $self->ctx->{json_reponse_cookie} =
+        $self->cgi->cookie(
+            -name => (ref $self)->COOKIE_ANON_CACHE,
+            -path => '/',
+            -value => ($cache_key) ? $cache_key : '',
+            -expires => ($cache_key) ? undef : '-1h'
+        );
+
+    return Apache2::Const::OK;
+}
+
+sub _do_mylist_add {
+    my $self = shift;
+    my @rec_ids = $self->cgi->param('record');
+
+    my ($cache_key, $list) = $self->fetch_mylist(0, 1);
+    push(@$list, @rec_ids);
 
     $cache_key = $U->simplereq(
         'open-ils.actor',
         'open-ils.actor.anon_cache.set_value', 
         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
 
+    return ($cache_key, $list);
+}
+
+sub load_mylist_delete {
+    my $self = shift;
+
+    my ($cache_key, $list) = $self->_do_mylist_delete;
+
     return $self->mylist_action_redirect($cache_key);
 }
 
+sub load_api_mylist_delete {
+    my $self = shift;
+
+    my ($cache_key, $list) = $self->_do_mylist_delete();
+
+    $self->ctx->{json_response} = {
+        mylist => [ map { 0 + $_ } @$list ], # force integers
+    };
+    $self->ctx->{json_reponse_cookie} =
+        $self->cgi->cookie(
+            -name => (ref $self)->COOKIE_ANON_CACHE,
+            -path => '/',
+            -value => ($cache_key) ? $cache_key : '',
+            -expires => ($cache_key) ? undef : '-1h'
+        );
+
+    return Apache2::Const::OK;
+}
+
+sub _do_mylist_delete {
+    my $self = shift;
+    my @rec_ids = $self->cgi->param('record');
+
+    my ($cache_key, $list) = $self->fetch_mylist(0, 1);
+    foreach my $rec_id (@rec_ids) {
+        $list = [ grep { $_ ne $rec_id } @$list ];
+    }
+
+    $cache_key = $U->simplereq(
+        'open-ils.actor',
+        'open-ils.actor.anon_cache.set_value', 
+        $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
+
+    return ($cache_key, $list);
+}
+
 sub load_mylist_move {
     my $self = shift;
     my @rec_ids = $self->cgi->param('record');