From 1190ce3c9cf0f1292b6d3798445dd77c9faa72b2 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. --- Koha/REST/V1/Lists.pm | 74 ++++++++++++++++++++++++++++ Koha/Schema/Result/Virtualshelve.pm | 4 +- Koha/Virtualshelf.pm | 32 ++++++++++++ api/v1/swagger/definitions/list.yaml | 38 ++++++++++++++ api/v1/swagger/paths.json | 3 ++ api/v1/swagger/paths/lists.yaml | 49 ++++++++++++++++++ api/v1/swagger/swagger.yaml | 4 ++ 7 files changed, 203 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 0000000000..d32a64ef12 --- /dev/null +++ b/Koha/REST/V1/Lists.pm @@ -0,0 +1,74 @@ +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->validation->output->{only_mine}; + my $only_public = delete $c->validation->output->{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 9aaae8d602..fa1e553059 100644 --- a/Koha/Schema/Result/Virtualshelve.pm +++ b/Koha/Schema/Result/Virtualshelve.pm @@ -210,7 +210,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 }, ); 1; diff --git a/Koha/Virtualshelf.pm b/Koha/Virtualshelf.pm index a7273a95d9..0e8c1e1c29 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -255,6 +255,38 @@ sub can_biblios_be_removed { # Same answer since bug 18228 } +=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 0000000000..c17f9da485 --- /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.json b/api/v1/swagger/paths.json index 9d793ae047..a06f4edb91 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -167,6 +167,9 @@ "/public/libraries/{library_id}": { "$ref": "paths/libraries.json#/~1public~1libraries~1{library_id}" }, + "/public/lists": { + "$ref": "paths/lists.yaml#/~1public~1lists" + }, "/public/patrons/{patron_id}/password": { "$ref": "paths/public_patrons.json#/~1public~1patrons~1{patron_id}~1password" }, diff --git a/api/v1/swagger/paths/lists.yaml b/api/v1/swagger/paths/lists.yaml new file mode 100644 index 0000000000..d60ec5d313 --- /dev/null +++ b/api/v1/swagger/paths/lists.yaml @@ -0,0 +1,49 @@ +--- +"/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": "../parameters.json#/match" + - "$ref": "../parameters.json#/order_by" + - "$ref": "../parameters.json#/page" + - "$ref": "../parameters.json#/per_page" + - "$ref": "../parameters.json#/q_param" + - "$ref": "../parameters.json#/q_body" + - "$ref": "../parameters.json#/q_header" + produces: + - application/json + responses: + '200': + description: A list of transfer limits + schema: + type: array + '400': + description: Bad parameter + schema: + "$ref": "../definitions.json#/error" + '500': + description: Internal error + schema: + "$ref": "../definitions.json#/error" + '503': + description: Under maintenance + schema: + "$ref": "../definitions.json#/error" + x-koha-authorization: + permissions: + catalogue: 1 diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 2e4e9bc09f..7bcd0ac82d 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -79,6 +79,10 @@ tags: x-displayName: Libraries description: | Manage libraries + - name: "lists" + x-displayName: Lists + description: | + Manage lists - name: "macros" x-displayName: Macros description: | -- 2.33.0