From e5a6cb30cc10776386d30128ccc959d97b53d7a4 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 --- Koha/REST/V1/Lists.pm | 71 ++++++++++++++++++++++++++++ Koha/Schema/Result/Virtualshelve.pm | 4 +- Koha/Virtualshelf.pm | 32 +++++++++++++ api/v1/swagger/definitions/list.yaml | 38 +++++++++++++++ api/v1/swagger/paths/lists.yaml | 52 ++++++++++++++++++++ api/v1/swagger/swagger.yaml | 5 ++ 6 files changed, 201 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..ffe3977a215 --- /dev/null +++ b/Koha/REST/V1/Lists.pm @@ -0,0 +1,71 @@ +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'); + + return $c->render( + status => 400, + openapi => { error => "only_mine and only_public are mutually exclussive" } + ) if $only_mine and $only_public; + + if ( !$user and $only_mine ) { + $c->render( + status => 200, + openapi => [], + ); + } + + return try { + my $lists_set = Koha::Virtualshelves->new; + if ($only_mine) { + $lists_set = $lists_set->search( { owner => $user->id } ); + } elsif ($only_public) { + $lists_set = $lists_set->filter_by_public; + } else { + $lists_set = $lists_set->filter_by_readable( { patron_id => $user->id } ); + } + + my $lists = $c->objects->search($lists_set); + return $c->render( status => 200, openapi => $lists ); + } 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 437ef58bd13..dbae8ce93ef 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -353,6 +353,38 @@ sub transfer_ownership { return $self->set({ owner => $patron_id })->store; } +=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..c17f9da4856 --- /dev/null +++ b/api/v1/swagger/definitions/list.yaml @@ -0,0 +1,38 @@ +--- +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 +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..f6fa88f501b --- /dev/null +++ b/api/v1/swagger/paths/lists.yaml @@ -0,0 +1,52 @@ +--- +"/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 transfer limits + schema: + type: array + "400": + description: Bad parameter + 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" + x-koha-authorization: + permissions: + catalogue: 1 diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 75517313c25..5253096d2aa 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -493,6 +493,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}": @@ -1189,6 +1191,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.44.0