From 7cb2dedf7e9ba57387d45ca7a264c90f50aaaf87 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 7 Jul 2022 17:59:29 +0100 Subject: [PATCH] Bug 31028: Add catalog concerns endpoints This patch adds the staff accessible REST API endpoints for catalog concerns. New endpoints available: * POST /catalog/concerns * GET /catalog/concerns * GET /catalog/concerns/{concern_id} * PUT /catalog/concerns/{concern_id} * DELETE /catalog/concenrs/{concern_id} --- Koha/Catalog/Concern.pm | 52 +++++ Koha/Catalog/Concerns.pm | 49 +++++ Koha/REST/V1/Catalog/Concerns.pm | 141 ++++++++++++ .../swagger/definitions/catalog_concern.yaml | 38 ++++ api/v1/swagger/paths/catalog_concerns.yaml | 200 ++++++++++++++++++ api/v1/swagger/swagger.yaml | 12 ++ 6 files changed, 492 insertions(+) create mode 100644 Koha/Catalog/Concern.pm create mode 100644 Koha/Catalog/Concerns.pm create mode 100644 Koha/REST/V1/Catalog/Concerns.pm create mode 100644 api/v1/swagger/definitions/catalog_concern.yaml create mode 100644 api/v1/swagger/paths/catalog_concerns.yaml diff --git a/Koha/Catalog/Concern.pm b/Koha/Catalog/Concern.pm new file mode 100644 index 0000000000..d608da7512 --- /dev/null +++ b/Koha/Catalog/Concern.pm @@ -0,0 +1,52 @@ +package Koha::Catalog::Concern; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::Catalog::Concern - Koha Catalog::Concern Object class + +=head1 API + +=head2 Internal methods + +=cut + +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Catalog::Concern object +on the API. + +=cut + +sub to_api_mapping { + return { id => 'concern_id', }; +} + +=head3 _type + +=cut + +sub _type { + return 'CatalogConcern'; +} + +1; diff --git a/Koha/Catalog/Concerns.pm b/Koha/Catalog/Concerns.pm new file mode 100644 index 0000000000..ec4652f31c --- /dev/null +++ b/Koha/Catalog/Concerns.pm @@ -0,0 +1,49 @@ +package Koha::Catalog::Concerns; + +# 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 base qw(Koha::Objects); + +=head1 NAME + +Koha::Tag - Koha Tag Object class + +=head1 API + +=head2 Internal methods + +=cut + +=head3 _type + +=cut + +sub _type { + return 'CatalogConcern'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Catalog::Concern'; +} + +1; diff --git a/Koha/REST/V1/Catalog/Concerns.pm b/Koha/REST/V1/Catalog/Concerns.pm new file mode 100644 index 0000000000..2ce15bfd79 --- /dev/null +++ b/Koha/REST/V1/Catalog/Concerns.pm @@ -0,0 +1,141 @@ +package Koha::REST::V1::Catalog::Concerns; + +# 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::Catalog::Concern; +use Koha::Catalog::Concerns; + +use Try::Tiny qw( catch try ); + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $concerns_set = Koha::Catalog::Concerns->new; + my $concerns = $c->objects->search( $concerns_set ); + return $c->render( status => 200, openapi => $concerns ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +=head3 get + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + return try { + my $concern = Koha::Catalog::Concerns->find( $c->validation->param('concern_id') ); + unless ($concern) { + return $c->render( status => 404, + openapi => { error => "Catalog::Concern not found" } ); + } + + return $c->render( status => 200, openapi => $concern->to_api ); + } + catch { + $c->unhandled_exception($_); + } +} + +=head3 add + +=cut + +sub add { + my $c = shift->openapi->valid_input or return; + + return try { + my $body = $c->validation->param('body'); + my $concern = Koha::Catalog::Concern->new_from_api($body)->store; + $concern->discard_changes; + $c->res->headers->location( $c->req->url->to_string . '/' . $concern->id ); + return $c->render( + status => 201, + openapi => $concern->to_api + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 update + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $concern = Koha::Catalog::Concerns->find( $c->validation->param('concern_id') ); + + if ( not defined $concern ) { + return $c->render( status => 404, + openapi => { error => "Object not found" } ); + } + + return try { + $concern->set_from_api( $c->validation->param('body') ); + $concern->store(); + return $c->render( status => 200, openapi => $concern->to_api ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 delete + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $concern = Koha::Catalog::Concerns->find( $c->validation->param('concern_id') ); + if ( not defined $concern ) { + return $c->render( status => 404, + openapi => { error => "Object not found" } ); + } + + return try { + $concern->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/api/v1/swagger/definitions/catalog_concern.yaml b/api/v1/swagger/definitions/catalog_concern.yaml new file mode 100644 index 0000000000..01e7b87b92 --- /dev/null +++ b/api/v1/swagger/definitions/catalog_concern.yaml @@ -0,0 +1,38 @@ +--- +type: object +properties: + concern_id: + type: integer + description: Internal concern identifier + added_date: + type: + - string + - "null" + format: date-time + description: Date the concern was reported + biblio_id: + type: integer + description: Internal identifier for the biblio the concern is related to + message: + type: string + description: Concern details + patron_id: + type: integer + description: Internal identifier for the patron the concern was submitted by + resolved_by: + type: + - integer + - "null" + description: Internal identifier for the patron the concern was resolved by + resolved_date: + type: + - string + - "null" + format: date-time + description: Date the concern was resolved_date + resolution_message: + type: + - string + - "null" + description: Resolution details +additionalProperties: false diff --git a/api/v1/swagger/paths/catalog_concerns.yaml b/api/v1/swagger/paths/catalog_concerns.yaml new file mode 100644 index 0000000000..138d51986b --- /dev/null +++ b/api/v1/swagger/paths/catalog_concerns.yaml @@ -0,0 +1,200 @@ +--- +/catalog/concerns: + get: + x-mojo-to: Catalog::Concerns#list + operationId: listConcerns + tags: + - concerns + summary: List concerns + produces: + - application/json + parameters: + - $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" + - $ref: "../swagger.yaml#/parameters/request_id_header" + responses: + "200": + description: A list of concerns + schema: + type: array + items: + $ref: "../swagger.yaml#/definitions/catalog_concern" + "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: + catalogue: "1" + post: + x-mojo-to: Catalog::Concerns#add + operationId: addConcern + tags: + - concerns + summary: Add catalog concern + parameters: + - name: body + in: body + description: A JSON object containing informations about the new hold + required: true + schema: + $ref: "../swagger.yaml#/definitions/catalog_concern" + produces: + - application/json + responses: + "201": + description: Concern added + schema: + $ref: "../swagger.yaml#/definitions/catalog_concern" + "401": + description: Authentication required + 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: + catalogue: "1" +"/catalog/concerns/{concern_id}": + get: + x-mojo-to: Catalog::Concerns#get + operationId: getConcern + tags: + - concerns + summary: Get catalog concern + parameters: + - $ref: "../swagger.yaml#/parameters/concern_id_pp" + produces: + - application/json + responses: + "200": + description: A catalog concern + schema: + $ref: "../swagger.yaml#/definitions/catalog_concern" + "404": + description: Concern 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" + put: + x-mojo-to: Catalog::Concerns#update + operationId: updateConcern + tags: + - concerns + summary: Update catalog concern + parameters: + - $ref: "../swagger.yaml#/parameters/concern_id_pp" + - name: body + in: body + description: A catalog concern object + required: true + schema: + $ref: "../swagger.yaml#/definitions/catalog_concern" + produces: + - application/json + responses: + "200": + description: A catalog concern + schema: + $ref: "../swagger.yaml#/definitions/catalog_concern" + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Concern not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "500": + description: Internal error + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + editcatalogue: edit_catalogue + delete: + x-mojo-to: Catalog::Concerns#delete + operationId: deleteConcern + tags: + - concerns + summary: Delete catalog concern + parameters: + - $ref: "../swagger.yaml#/parameters/concern_id_pp" + produces: + - application/json + responses: + "204": + description: Concern deleted + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Concern not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "500": + description: Internal error + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + editcatalogue: edit_catalogue diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 5bdbab2fa8..655194b5b3 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -14,6 +14,8 @@ definitions: $ref: ./definitions/bundle_link.yaml cashup: $ref: ./definitions/cashup.yaml + catalog_concern: + $ref: ./definitions/catalog_concern.yaml checkout: $ref: ./definitions/checkout.yaml checkouts: @@ -125,6 +127,10 @@ paths: $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups" "/cashups/{cashup_id}": $ref: "./paths/cash_registers.yaml#/~1cashups~1{cashup_id}" + "/catalog/concerns": + $ref: "./paths/catalog_concerns.yaml#/~1catalog~1concerns" + "/catalog/concerns/{concern_id}": + $ref: "./paths/catalog_concerns.yaml#/~1catalog~1concerns~1{concern_id}" /checkouts: $ref: ./paths/checkouts.yaml#/~1checkouts "/checkouts/{checkout_id}": @@ -290,6 +296,12 @@ parameters: name: checkout_id required: true type: integer + concern_id_pp: + description: Internal concern identifier + in: path + name: concern_id + required: true + type: integer city_id_pp: description: City internal identifier in: path -- 2.20.1