From 3c87aa8c7f45f519ca40ed935df796a2829cff7b Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 31 Oct 2023 10:56:31 -0100 Subject: [PATCH] Bug 35197: Add additional_fields REST API endpoint Test plan: 1) Apply patch, restart plack 'koha-plack --restart kohadev' 2) Visit /api/v1/additional_fields?tablename=aqinvoices - Notice its empty 3) Visit /cgi-bin/koha/admin/additional-fields.pl?tablename=aqbasket and add a new additional field 4) Do step 2) again - Notice the newly created additional field is there 5) Visit /cgi-bin/koha/admin/additional-fields.pl?tablename=aqinvoices and add a new additional field for invoices 6) Do step 2) again - Notice both additional fields are there 7) Visit /api/v1/additional_fields?tablename=aqinvoices - Notice only the additional field for aqinvoices is listed Signed-off-by: Lucas Gass Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/REST/V1/AdditionalFields.pm | 52 +++++++++++++++++++ .../swagger/definitions/additional_field.yaml | 35 +++++++++++++ api/v1/swagger/paths/additional_fields.yaml | 49 +++++++++++++++++ api/v1/swagger/swagger.yaml | 4 ++ 4 files changed, 140 insertions(+) create mode 100644 Koha/REST/V1/AdditionalFields.pm create mode 100644 api/v1/swagger/definitions/additional_field.yaml create mode 100644 api/v1/swagger/paths/additional_fields.yaml diff --git a/Koha/REST/V1/AdditionalFields.pm b/Koha/REST/V1/AdditionalFields.pm new file mode 100644 index 0000000000..db9e2ed8d2 --- /dev/null +++ b/Koha/REST/V1/AdditionalFields.pm @@ -0,0 +1,52 @@ +package Koha::REST::V1::AdditionalFields; + +# 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::AdditionalFields; + +use Try::Tiny qw( catch try ); + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + my $tablename = $c->param('tablename'); + + return try { + my $additional_fields_set = Koha::AdditionalFields->new; + if ($tablename) { + $additional_fields_set = $additional_fields_set->search( { tablename => $tablename } ); + } + my $additional_fields = $c->objects->search($additional_fields_set); + return $c->render( status => 200, openapi => $additional_fields ); + } catch { + $c->unhandled_exception($_); + }; + +} + +1; diff --git a/api/v1/swagger/definitions/additional_field.yaml b/api/v1/swagger/definitions/additional_field.yaml new file mode 100644 index 0000000000..5fa3a3e699 --- /dev/null +++ b/api/v1/swagger/definitions/additional_field.yaml @@ -0,0 +1,35 @@ +--- +type: object +properties: + id: + type: integer + description: internally assigned additional field identifier + readOnly: true + tablename: + description: name of the table this additional field corresponds to + type: string + name: + description: name of the additional field + type: string + authorised_value_category: + description: authorised value category of the additional field + type: + - string + - "null" + marcfield: + description: marcfield of the additional field + type: string + marcfield_mode: + description: marcfield mode of the additional field + type: string + enum: + - get + - set + searchable: + description: is the additional field searchable + type: boolean + +additionalProperties: false +required: + - id + - tablename \ No newline at end of file diff --git a/api/v1/swagger/paths/additional_fields.yaml b/api/v1/swagger/paths/additional_fields.yaml new file mode 100644 index 0000000000..3f5b5b651f --- /dev/null +++ b/api/v1/swagger/paths/additional_fields.yaml @@ -0,0 +1,49 @@ +--- +/additional_fields: + get: + x-mojo-to: AdditionalFields#list + operationId: listAdditionalFields + tags: + - additional_fields + summary: List additional fields + produces: + - application/json + parameters: + - description: filter by table name + in: query + name: tablename + 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" + responses: + 200: + description: A list of additional_fields + schema: + items: + $ref: "../swagger.yaml#/definitions/additional_field" + type: array + 400: + description: Bad request + 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" + x-koha-authorization: + permissions: + tools: manage_additional_fields \ No newline at end of file diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 7352b1c240..106d0600f0 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -4,6 +4,8 @@ basePath: /api/v1 definitions: account_line: $ref: ./definitions/account_line.yaml + additional_field: + $ref: ./definitions/additional_field.yaml advancededitormacro: $ref: ./definitions/advancededitormacro.yaml allows_renewal: @@ -197,6 +199,8 @@ paths: $ref: "./paths/acquisitions_vendors.yaml#/~1acquisitions~1vendors~1{vendor_id}" "/acquisitions/vendors/{vendor_id}/issues": $ref: "./paths/acquisitions_vendor_issues.yaml#/~1acquisitions~1vendors~1{vendor_id}~1issues" + /additional_fields: + $ref: ./paths/additional_fields.yaml#/~1additional_fields /advanced_editor/macros: $ref: ./paths/advancededitormacros.yaml#/~1advanced_editor~1macros /advanced_editor/macros/shared: -- 2.39.5