From a3481d81048bcc37147553e54ff6c886a4b3acd9 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 15 Dec 2023 16:56:11 +0000 Subject: [PATCH] Bug 33960: Add routes and controller Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/REST/V1/DeletedBiblios.pm | 172 ++++++++++++++++++++++ api/v1/swagger/paths/deleted_biblios.yaml | 109 ++++++++++++++ api/v1/swagger/swagger.yaml | 4 + 3 files changed, 285 insertions(+) create mode 100644 Koha/REST/V1/DeletedBiblios.pm create mode 100644 api/v1/swagger/paths/deleted_biblios.yaml diff --git a/Koha/REST/V1/DeletedBiblios.pm b/Koha/REST/V1/DeletedBiblios.pm new file mode 100644 index 0000000000..c165b6d84b --- /dev/null +++ b/Koha/REST/V1/DeletedBiblios.pm @@ -0,0 +1,172 @@ +package Koha::REST::V1::DeletedBiblios; + +# 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::Old::Biblios; +use Koha::DateUtils; +use Koha::RecordProcessor; + +use C4::Context; + +use Koha::Items; + +use List::MoreUtils qw( any ); +use MARC::Record::MiJ; + +use Try::Tiny qw( catch try ); + +=head1 API + +=head2 Methods + +=head3 get + +Controller function that handles retrieving a single biblio object + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + my $attributes; + $attributes = { prefetch => ['metadata'] } # don't prefetch metadata if not needed + unless $c->req->headers->accept =~ m/application\/json/; + + my $biblio = Koha::Old::Biblios->find( { biblionumber => $c->param('biblio_id') }, $attributes ); + + unless ($biblio) { + return $c->render( + status => 404, + openapi => { error => "Object not found." } + ); + } + + return try { + + if ( $c->req->headers->accept =~ m/application\/json/ ) { + return $c->render( + status => 200, + json => $biblio->to_api + ); + } else { + my $metadata = $biblio->metadata; + my $record = $metadata->record; + my $schema = $metadata->schema // C4::Context->preference("marcflavour"); + + $c->respond_to( + marcxml => { + status => 200, + format => 'marcxml', + text => $record->as_xml_record($schema), + }, + mij => { + status => 200, + format => 'mij', + data => $record->to_mij + }, + marc => { + status => 200, + format => 'marc', + text => $record->as_usmarc + }, + txt => { + status => 200, + format => 'text/plain', + text => $record->as_formatted + }, + any => { + status => 406, + openapi => [ + "application/json", + "application/marcxml+xml", + "application/marc-in-json", + "application/marc", + "text/plain" + ] + } + ); + } + } catch { + $c->unhandled_exception($_); + }; +} + +=head3 list + +Controller function that handles listing a deleted biblio objects + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + my @prefetch = qw(biblioitem); + push @prefetch, 'metadata' # don't prefetch metadata if not needed + unless $c->req->headers->accept =~ m/application\/json/; + + my $rs = Koha::Old::Biblios->search( undef, { prefetch => \@prefetch } ); + my $biblios = $c->objects->search_rs( $rs, [ ( sub { $rs->api_query_fixer( $_[0], '', $_[1] ) } ) ] ); + + return try { + + if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) { + return $c->render( + status => 200, + json => $c->objects->to_api($biblios), + ); + } elsif ( $c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ ) { + $c->res->headers->add( 'Content-Type', 'application/marcxml+xml' ); + return $c->render( + status => 200, + text => $biblios->print_collection('marcxml') + ); + } elsif ( $c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ ) { + $c->res->headers->add( 'Content-Type', 'application/marc-in-json' ); + return $c->render( + status => 200, + data => $biblios->print_collection('mij') + ); + } elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) { + $c->res->headers->add( 'Content-Type', 'application/marc' ); + return $c->render( + status => 200, + text => $biblios->print_collection('marc') + ); + } elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) { + return $c->render( + status => 200, + text => $biblios->print_collection('txt') + ); + } else { + return $c->render( + status => 406, + openapi => [ + "application/json", "application/marcxml+xml", + "application/marc-in-json", "application/marc", + "text/plain" + ] + ); + } + } catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/api/v1/swagger/paths/deleted_biblios.yaml b/api/v1/swagger/paths/deleted_biblios.yaml new file mode 100644 index 0000000000..5678a2a22a --- /dev/null +++ b/api/v1/swagger/paths/deleted_biblios.yaml @@ -0,0 +1,109 @@ +--- +"/deleted/biblios": + get: + x-mojo-to: DeletedBiblios#list + operationId: listDeletedBiblios + tags: + - biblios + summary: List deleted biblios + parameters: + - $ref: "../swagger.yaml#/parameters/page" + - $ref: "../swagger.yaml#/parameters/per_page" + - $ref: "../swagger.yaml#/parameters/match" + - $ref: "../swagger.yaml#/parameters/order_by" + - $ref: "../swagger.yaml#/parameters/q_param" + - $ref: "../swagger.yaml#/parameters/q_body" + - $ref: "../swagger.yaml#/parameters/request_id_header" + produces: + - application/json + - application/marcxml+xml + - application/marc-in-json + - application/marc + - text/plain + responses: + "200": + description: A list of deleted biblios + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Biblio not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "406": + description: Not acceptable + schema: + type: array + description: Accepted content-types + items: + type: string + "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" +"/deleted/biblios/{biblio_id}": + get: + x-mojo-to: DeletedBiblios#get + operationId: getDeletedBiblio + tags: + - biblios + summary: Get deleted biblio + parameters: + - $ref: "../swagger.yaml#/parameters/biblio_id_pp" + produces: + - application/json + - application/marcxml+xml + - application/marc-in-json + - application/marc + - text/plain + responses: + "200": + description: A deleted biblio + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Biblio not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "406": + description: Not acceptable + schema: + type: array + description: Accepted content-types + items: + type: string + "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 aa11ade837..9ea76782a1 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -283,6 +283,10 @@ paths: $ref: ./paths/config_smtp_servers.yaml#/~1config~1smtp_servers "/config/smtp_servers/{smtp_server_id}": $ref: "./paths/config_smtp_servers.yaml#/~1config~1smtp_servers~1{smtp_server_id}" + "/deleted/biblios": + $ref: "./paths/deleted_biblios.yaml#/~1deleted~1biblios" + "/deleted/biblios/{biblio_id}": + $ref: "./paths/deleted_biblios.yaml#/~1deleted~1biblios~1{biblio_id}" /erm/config: $ref: ./paths/erm_config.yaml#/~1erm~1config /erm/agreements: -- 2.39.5