From 2f8dbe97ee11fd5bfdc2af6617703d2d186c7ab9 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 7 Sep 2021 13:35:13 -0300 Subject: [PATCH] Bug 28965: Add /public/lists This patch adds a route for publicly retrieving lists. Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize --- Koha/REST/V1/Lists.pm | 75 ++++++++++++++++++++++++++++ Koha/Schema/Result/Virtualshelve.pm | 4 +- Koha/Virtualshelf.pm | 32 ++++++++++++ api/v1/swagger/definitions/list.yaml | 41 +++++++++++++++ api/v1/swagger/paths/lists.yaml | 60 ++++++++++++++++++++++ api/v1/swagger/swagger.yaml | 7 +++ 6 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 Koha/REST/V1/Lists.pm create mode 100644 api/v1/swagger/definitions/list.yaml create mode 100644 api/v1/swagger/paths/lists.yaml diff --git a/Koha/REST/V1/Lists.pm b/Koha/REST/V1/Lists.pm new file mode 100644 index 00000000000..c6ea8594c0a --- /dev/null +++ b/Koha/REST/V1/Lists.pm @@ -0,0 +1,75 @@ +package Koha::REST::V1::Lists; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Virtualshelves; + +use Try::Tiny qw( catch try ); + +=head1 API + +=head2 Methods + +=head3 list_public + +=cut + +sub list_public { + my $c = shift->openapi->valid_input or return; + + my $user = $c->stash('koha.user'); + + my $only_mine = delete $c->param('only_mine'); + my $only_public = delete $c->param('only_public'); + + if ( !$user && $only_mine ) { + return $c->render( + status => 400, + openapi => { + error => "Bad request - only_mine can only be passed by logged in users", + error_code => "only_mine_forbidden", + }, + ); + } + + return try { + + my $lists_set = Koha::Virtualshelves->new; + + if ($only_mine) { + $lists_set = $lists_set->search( { owner => $user->id } ); + } + + if ( $only_public || !$user ) { + $lists_set = $lists_set->filter_by_public; + } else { + $lists_set = $lists_set->filter_by_readable( { patron_id => $user->id } ); + } + + return $c->render( + status => 200, + openapi => $c->objects->search($lists_set), + ); + } catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/Koha/Schema/Result/Virtualshelve.pm b/Koha/Schema/Result/Virtualshelve.pm index b2cd76ec4f5..06d3978fd77 100644 --- a/Koha/Schema/Result/Virtualshelve.pm +++ b/Koha/Schema/Result/Virtualshelve.pm @@ -230,7 +230,9 @@ sub koha_objects_class { } __PACKAGE__->add_columns( - '+public' => { is_boolean => 1 }, + '+allow_change_from_others' => { is_boolean => 1 }, + '+allow_change_from_owner' => { is_boolean => 1 }, + '+public' => { is_boolean => 1 }, ); __PACKAGE__->add_columns( diff --git a/Koha/Virtualshelf.pm b/Koha/Virtualshelf.pm index 2cea7e61e88..fea70466f35 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -398,6 +398,38 @@ sub transfer_ownership { return $self; } +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Virtualshelf object +on the API. + +=cut + +sub to_api_mapping { + return { + created_on => 'creation_date', + lastmodified => 'updated_on_date', + owner => 'owner_id', + shelfname => 'name', + shelfnumber => 'list_id', + sortfield => 'default_sort_field', + }; +} + +=head3 public_read_list + +This method returns the list of publicly readable database fields for both API and UI output purposes + +=cut + +sub public_read_list { + return [ + 'created_on', 'lastmodified', 'shelfname', + 'shelfnumber', 'public', 'sortfield', + 'allow_change_from_owner', 'allow_change_from_others' + ]; +} + =head2 Internal methods =head3 _type diff --git a/api/v1/swagger/definitions/list.yaml b/api/v1/swagger/definitions/list.yaml new file mode 100644 index 00000000000..215a8af5dcf --- /dev/null +++ b/api/v1/swagger/definitions/list.yaml @@ -0,0 +1,41 @@ +--- +type: object +properties: + list_id: + description: Interal identifier for the list + type: integer + name: + description: List name + type: string + creation_date: + description: Date the list was created + type: string + format: date-time + updated_on_date: + description: Date the list was last updated + type: string + format: date-time + owner_id: + description: Internal identifier for the owning patron + type: integer + allow_change_from_owner: + description: If the owner can change the contents + type: boolean + allow_change_from_others: + description: If others can change the contents + type: boolean + public: + description: If the list is public + type: boolean + default_sort_field: + description: The field this list is sorted on by default + type: string +additionalProperties: false +required: + - list_id + - name + - creation_date + - updated_on_date + - allow_change_from_owner + - allow_change_from_others + - public diff --git a/api/v1/swagger/paths/lists.yaml b/api/v1/swagger/paths/lists.yaml new file mode 100644 index 00000000000..596c1ef314b --- /dev/null +++ b/api/v1/swagger/paths/lists.yaml @@ -0,0 +1,60 @@ +--- +"/public/lists": + get: + x-mojo-to: Lists#list_public + operationId: listListsPublic + description: "This resource returns a list of existing bibliographic lists." + summary: List bibliographic lists + tags: + - lists + parameters: + - name: only_mine + in: query + description: Only return the users' lists + required: false + type: string + - name: only_public + in: query + description: Only return public lists + required: false + type: string + - $ref: "../swagger.yaml#/parameters/match" + - $ref: "../swagger.yaml#/parameters/order_by" + - $ref: "../swagger.yaml#/parameters/page" + - $ref: "../swagger.yaml#/parameters/per_page" + - $ref: "../swagger.yaml#/parameters/q_param" + - $ref: "../swagger.yaml#/parameters/q_body" + - $ref: "../swagger.yaml#/parameters/request_id_header" + produces: + - application/json + responses: + "200": + description: A list of lists + schema: + type: array + items: + $ref: "../swagger.yaml#/definitions/list" + "400": + description: | + Bad request. Possible `error_code` attribute values: + + * `bad_request` + * `invalid_query` + * `only_mine_forbidden` + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "500": + description: | + Internal server error. Possible `error_code` attribute values: + + * `internal_server_error` + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index d26e7d4fad5..ff7b94a174e 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -132,6 +132,8 @@ definitions: $ref: ./definitions/job.yaml library: $ref: ./definitions/library.yaml + list: + $ref: ./definitions/list.yaml merge_biblios: $ref: ./definitions/merge_biblios.yaml order: @@ -509,6 +511,8 @@ paths: $ref: ./paths/libraries.yaml#/~1public~1libraries "/public/libraries/{library_id}": $ref: "./paths/libraries.yaml#/~1public~1libraries~1{library_id}" + "/public/lists": + $ref: "./paths/lists.yaml#/~1public~1lists" "/public/oauth/login/{provider_code}/{interface}": $ref: ./paths/public_oauth.yaml#/~1public~1oauth~1login~1{provider_code}~1{interface} "/public/patrons/{patron_id}/article_requests/{article_request_id}": @@ -1232,6 +1236,9 @@ tags: - description: "Manage libraries\n" name: libraries x-displayName: Libraries + - description: "Manage lists" + name: "lists" + x-displayName: Lists - description: "Manage macros\n" name: macros x-displayName: Macros -- 2.47.0