Bug 32997: Add REST API endpoint to list authorised values for multiple given categories
authorPedro Amorim <pedro.amorim@ptfs-europe.com>
Mon, 20 Feb 2023 16:17:41 +0000 (16:17 +0000)
committerTomas Cohen Arazi <tomascohen@theke.io>
Thu, 2 Mar 2023 15:00:15 +0000 (12:00 -0300)
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 <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/AuthorisedValueCategory.pm
Koha/REST/V1/AuthorisedValueCategories.pm [new file with mode: 0644]
api/v1/swagger/definitions/authorised_value_category.yaml [new file with mode: 0644]
api/v1/swagger/paths/authorised_value_categories.yaml [new file with mode: 0644]
api/v1/swagger/swagger.yaml

index e14129f..4e715c5 100644 (file)
@@ -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 (file)
index 0000000..3a7872f
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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 (file)
index 0000000..1d5dd02
--- /dev/null
@@ -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 (file)
index 0000000..f5f8387
--- /dev/null
@@ -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
index d27ffcd..cc3e187 100644 (file)
@@ -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}":