Bug 24474: (QA follow-up) Fix failing test
[koha.git] / Koha / REST / V1 / Biblios.pm
blobaf13f283bdf0e6583d165c61ec69a2fe90913b8b
1 package Koha::REST::V1::Biblios;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Biblios;
23 use C4::Biblio qw(DelBiblio);
25 use MARC::Record::MiJ;
27 use Try::Tiny;
29 =head1 API
31 =head2 Methods
33 =head3 get
35 Controller function that handles retrieving a single biblio object
37 =cut
39 sub get {
40 my $c = shift->openapi->valid_input or return;
42 my $attributes;
43 $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
44 unless $c->req->headers->accept =~ m/application\/json/;
46 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
48 unless ( $biblio ) {
49 return $c->render(
50 status => 404,
51 openapi => {
52 error => "Object not found."
57 return try {
59 if ( $c->req->headers->accept =~ m/application\/json/ ) {
60 return $c->render(
61 status => 200,
62 json => $biblio->to_api
65 else {
66 my $record = $biblio->metadata->record;
68 $c->respond_to(
69 marcxml => {
70 status => 200,
71 format => 'marcxml',
72 text => $record->as_xml_record
74 mij => {
75 status => 200,
76 format => 'mij',
77 text => $record->to_mij
79 marc => {
80 status => 200,
81 format => 'marc',
82 text => $record->as_usmarc
84 any => {
85 status => 406,
86 openapi => [
87 "application/json",
88 "application/marcxml+xml",
89 "application/marc-in-json",
90 "application/marc"
96 catch {
97 return $c->render(
98 status => 500,
99 openapi => { error => "Something went wrong, check the logs ($_)" }
104 =head3 delete
106 Controller function that handles deleting a biblio object
108 =cut
110 sub delete {
111 my $c = shift->openapi->valid_input or return;
113 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
115 if ( not defined $biblio ) {
116 return $c->render(
117 status => 404,
118 openapi => { error => "Object not found" }
122 return try {
123 my $error = DelBiblio( $biblio->id );
125 if ($error) {
126 return $c->render(
127 status => 409,
128 openapi => { error => $error }
131 else {
132 return $c->render( status => 204, openapi => "" );
135 catch {
136 if ( $_->isa('DBIx::Class::Exception') ) {
137 return $c->render(
138 status => 500,
139 openapi => { error => $_->{msg} }
142 else {
143 return $c->render(
144 status => 500,
145 openapi => { error => "Something went wrong, check the logs." }