Bug 33960: Add routes and controller

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Nick Clemens 2023-12-15 16:56:11 +00:00 committed by Katrin Fischer
parent 61e01ec8a1
commit a3481d8104
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
3 changed files with 285 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses>.
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;

View File

@ -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"

View File

@ -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: