Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::DeletedBiblios; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
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. |
9 |
# |
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. |
14 |
# |
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>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
|
22 |
use Koha::Old::Biblios; |
23 |
use Koha::DateUtils; |
24 |
use Koha::RecordProcessor; |
25 |
|
26 |
use C4::Context; |
27 |
|
28 |
use Koha::Items; |
29 |
|
30 |
use List::MoreUtils qw( any ); |
31 |
use MARC::Record::MiJ; |
32 |
|
33 |
use Try::Tiny qw( catch try ); |
34 |
|
35 |
=head1 API |
36 |
|
37 |
=head2 Methods |
38 |
|
39 |
=head3 get |
40 |
|
41 |
Controller function that handles retrieving a single biblio object |
42 |
|
43 |
=cut |
44 |
|
45 |
sub get { |
46 |
my $c = shift->openapi->valid_input or return; |
47 |
|
48 |
my $attributes; |
49 |
$attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed |
50 |
unless $c->req->headers->accept =~ m/application\/json/; |
51 |
|
52 |
my $biblio = Koha::Old::Biblios->find( { biblionumber => $c->param('biblio_id') }, $attributes ); |
53 |
|
54 |
unless ( $biblio ) { |
55 |
return $c->render( |
56 |
status => 404, |
57 |
openapi => { |
58 |
error => "Object not found." |
59 |
} |
60 |
); |
61 |
} |
62 |
|
63 |
return try { |
64 |
|
65 |
if ( $c->req->headers->accept =~ m/application\/json/ ) { |
66 |
return $c->render( |
67 |
status => 200, |
68 |
json => $biblio->to_api |
69 |
); |
70 |
} |
71 |
else { |
72 |
my $metadata = $biblio->metadata; |
73 |
my $record = $metadata->record; |
74 |
my $schema = $metadata->schema // C4::Context->preference("marcflavour"); |
75 |
|
76 |
$c->respond_to( |
77 |
marcxml => { |
78 |
status => 200, |
79 |
format => 'marcxml', |
80 |
text => $record->as_xml_record($schema), |
81 |
}, |
82 |
mij => { |
83 |
status => 200, |
84 |
format => 'mij', |
85 |
data => $record->to_mij |
86 |
}, |
87 |
marc => { |
88 |
status => 200, |
89 |
format => 'marc', |
90 |
text => $record->as_usmarc |
91 |
}, |
92 |
txt => { |
93 |
status => 200, |
94 |
format => 'text/plain', |
95 |
text => $record->as_formatted |
96 |
}, |
97 |
any => { |
98 |
status => 406, |
99 |
openapi => [ |
100 |
"application/json", |
101 |
"application/marcxml+xml", |
102 |
"application/marc-in-json", |
103 |
"application/marc", |
104 |
"text/plain" |
105 |
] |
106 |
} |
107 |
); |
108 |
} |
109 |
} |
110 |
catch { |
111 |
$c->unhandled_exception($_); |
112 |
}; |
113 |
} |
114 |
|
115 |
=head3 list |
116 |
|
117 |
Controller function that handles listing a deleted biblio objects |
118 |
|
119 |
=cut |
120 |
|
121 |
sub list { |
122 |
my $c = shift->openapi->valid_input or return; |
123 |
|
124 |
my @prefetch = qw(biblioitem); |
125 |
push @prefetch, 'metadata' # don't prefetch metadata if not needed |
126 |
unless $c->req->headers->accept =~ m/application\/json/; |
127 |
|
128 |
my $rs = Koha::Old::Biblios->search( undef, { prefetch => \@prefetch }); |
129 |
my $biblios = $c->objects->search_rs( $rs, [(sub{ $rs->api_query_fixer( $_[0], '', $_[1] ) })] ); |
130 |
|
131 |
return try { |
132 |
|
133 |
if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) { |
134 |
return $c->render( |
135 |
status => 200, |
136 |
json => $c->objects->to_api( $biblios ), |
137 |
); |
138 |
} |
139 |
elsif ( |
140 |
$c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ ) |
141 |
{ |
142 |
$c->res->headers->add( 'Content-Type', 'application/marcxml+xml' ); |
143 |
return $c->render( |
144 |
status => 200, |
145 |
text => $biblios->print_collection('marcxml') |
146 |
); |
147 |
} |
148 |
elsif ( |
149 |
$c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ ) |
150 |
{ |
151 |
$c->res->headers->add( 'Content-Type', 'application/marc-in-json' ); |
152 |
return $c->render( |
153 |
status => 200, |
154 |
data => $biblios->print_collection('mij') |
155 |
); |
156 |
} |
157 |
elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) { |
158 |
$c->res->headers->add( 'Content-Type', 'application/marc' ); |
159 |
return $c->render( |
160 |
status => 200, |
161 |
text => $biblios->print_collection('marc') |
162 |
); |
163 |
} |
164 |
elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) { |
165 |
return $c->render( |
166 |
status => 200, |
167 |
text => $biblios->print_collection('txt') |
168 |
); |
169 |
} |
170 |
else { |
171 |
return $c->render( |
172 |
status => 406, |
173 |
openapi => [ |
174 |
"application/json", "application/marcxml+xml", |
175 |
"application/marc-in-json", "application/marc", |
176 |
"text/plain" |
177 |
] |
178 |
); |
179 |
} |
180 |
} |
181 |
catch { |
182 |
$c->unhandled_exception($_); |
183 |
}; |
184 |
} |
185 |
|
186 |
1; |