From bbc0e8fe32cb5e1b5bf752732428ce2f9ebbb840 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 20 Feb 2023 16:17:41 +0000 Subject: [PATCH] Bug 32997: Add REST API endpoint to list authorised values for multiple given categories This patch adds /api/v1/authorised_value_categories endpoint that retrieves authorised value categories for the given names and their authorised values if x-koha-embed: authorised_values is also given. To test: Apply patch curl -u koha:koha --request GET \"http://localhost:8081/api/v1/authorised_value_categories?q=%7B%22me.category_name%22%3A%5B%22LOC%22%2C%22YES_NO%22%5D%7D\" --header \"x-koha-embed:authorised_values\" Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/AuthorisedValueCategory.pm | 13 ++++ Koha/REST/V1/AuthorisedValueCategories.pm | 49 +++++++++++++++ .../authorised_value_category.yaml | 20 +++++++ .../paths/authorised_value_categories.yaml | 60 +++++++++++++++++++ api/v1/swagger/swagger.yaml | 4 ++ 5 files changed, 146 insertions(+) create mode 100644 Koha/REST/V1/AuthorisedValueCategories.pm create mode 100644 api/v1/swagger/definitions/authorised_value_category.yaml create mode 100644 api/v1/swagger/paths/authorised_value_categories.yaml diff --git a/Koha/AuthorisedValueCategory.pm b/Koha/AuthorisedValueCategory.pm index e14129f80d..4e715c5c8e 100644 --- a/Koha/AuthorisedValueCategory.pm +++ b/Koha/AuthorisedValueCategory.pm @@ -33,6 +33,19 @@ Koha::AuthorisedValueCategory - Koha AuthorisedValueCategory Object class =cut +=head3 authorised_values + +Returns the authorised values for this authorised value category + +=cut + +sub authorised_values { + my ( $self ) = @_; + + my $authorised_values_rs = $self->_result->authorised_values; + return Koha::AuthorisedValues->_new_from_dbic($authorised_values_rs); +} + =head3 delete Overridden delete method to prevent system default deletions diff --git a/Koha/REST/V1/AuthorisedValueCategories.pm b/Koha/REST/V1/AuthorisedValueCategories.pm new file mode 100644 index 0000000000..3a7872f6de --- /dev/null +++ b/Koha/REST/V1/AuthorisedValueCategories.pm @@ -0,0 +1,49 @@ +package Koha::REST::V1::AuthorisedValueCategories; + +# 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::AuthorisedValues; +use Koha::AuthorisedValueCategories; + +use Try::Tiny; + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $authorised_value_categories_set = Koha::AuthorisedValueCategories->new; + my $authorised_value_categories = $c->objects->search( $authorised_value_categories_set ); + return $c->render( status => 200, openapi => $authorised_value_categories ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +1; diff --git a/api/v1/swagger/definitions/authorised_value_category.yaml b/api/v1/swagger/definitions/authorised_value_category.yaml new file mode 100644 index 0000000000..1d5dd026bf --- /dev/null +++ b/api/v1/swagger/definitions/authorised_value_category.yaml @@ -0,0 +1,20 @@ +--- +type: object +properties: + category_name: + type: string + description: Unique category name primary key + readOnly: true + is_system: + description: Is this category system or not + type: boolean + readOnly: true + authorised_values: + type: array + description: This category's authorised values + items: + $ref: authorised_value.yaml + +additionalProperties: false +required: + - category_name diff --git a/api/v1/swagger/paths/authorised_value_categories.yaml b/api/v1/swagger/paths/authorised_value_categories.yaml new file mode 100644 index 0000000000..f5f8387e4a --- /dev/null +++ b/api/v1/swagger/paths/authorised_value_categories.yaml @@ -0,0 +1,60 @@ +--- +/authorised_value_categories: + get: + x-mojo-to: AuthorisedValueCategories#list + operationId: listAuthorisedValueCategories + tags: + - authorised_value_categories + summary: List authorised value categories + produces: + - application/json + parameters: + - name: x-koha-embed + in: header + required: false + description: Embed list sent as a request header + type: array + items: + type: string + enum: + - authorised_values + collectionFormat: csv + - $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/q_header" + responses: + 200: + description: A list of authorised value categories + schema: + items: + $ref: "../swagger.yaml#/definitions/authorised_value_category" + type: array + 400: + description: Bad request + schema: + $ref: "../swagger.yaml#/definitions/error" + 403: + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + 404: + description: Resource not found + 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 d27ffcdbad..cc3e1873a3 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -10,6 +10,8 @@ definitions: $ref: ./definitions/allows_renewal.yaml authorised_value: $ref: ./definitions/authorised_value.yaml + authorised_value_category: + $ref: ./definitions/authorised_value_category.yaml identity_provider: "$ref": ./definitions/identity_provider.yaml identity_provider_domain: @@ -141,6 +143,8 @@ paths: $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}": $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id} + /authorised_value_categories: + $ref: paths/authorised_value_categories.yaml#/~1authorised_value_categories "/authorised_value_categories/{authorised_value_category_name}/values": $ref: "./paths/authorised_values.yaml#/~1authorised_value_categories~1{authorised_value_category_name}~1values" "/biblios/{biblio_id}": -- 2.39.5