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 => { error => "Object not found." } |
58 |
); |
59 |
} |
60 |
|
61 |
return try { |
62 |
|
63 |
if ( $c->req->headers->accept =~ m/application\/json/ ) { |
64 |
return $c->render( |
65 |
status => 200, |
66 |
json => $biblio->to_api |
67 |
); |
68 |
} else { |
69 |
my $metadata = $biblio->metadata; |
70 |
my $record = $metadata->record; |
71 |
my $schema = $metadata->schema // C4::Context->preference("marcflavour"); |
72 |
|
73 |
$c->respond_to( |
74 |
marcxml => { |
75 |
status => 200, |
76 |
format => 'marcxml', |
77 |
text => $record->as_xml_record($schema), |
78 |
}, |
79 |
mij => { |
80 |
status => 200, |
81 |
format => 'mij', |
82 |
data => $record->to_mij |
83 |
}, |
84 |
marc => { |
85 |
status => 200, |
86 |
format => 'marc', |
87 |
text => $record->as_usmarc |
88 |
}, |
89 |
txt => { |
90 |
status => 200, |
91 |
format => 'text/plain', |
92 |
text => $record->as_formatted |
93 |
}, |
94 |
any => { |
95 |
status => 406, |
96 |
openapi => [ |
97 |
"application/json", |
98 |
"application/marcxml+xml", |
99 |
"application/marc-in-json", |
100 |
"application/marc", |
101 |
"text/plain" |
102 |
] |
103 |
} |
104 |
); |
105 |
} |
106 |
} catch { |
107 |
$c->unhandled_exception($_); |
108 |
}; |
109 |
} |
110 |
|
111 |
=head3 list |
112 |
|
113 |
Controller function that handles listing a deleted biblio objects |
114 |
|
115 |
=cut |
116 |
|
117 |
sub list { |
118 |
my $c = shift->openapi->valid_input or return; |
119 |
|
120 |
my @prefetch = qw(biblioitem); |
121 |
push @prefetch, 'metadata' # don't prefetch metadata if not needed |
122 |
unless $c->req->headers->accept =~ m/application\/json/; |
123 |
|
124 |
my $rs = Koha::Old::Biblios->search( undef, { prefetch => \@prefetch } ); |
125 |
my $biblios = $c->objects->search_rs( $rs, [ ( sub { $rs->api_query_fixer( $_[0], '', $_[1] ) } ) ] ); |
126 |
|
127 |
return try { |
128 |
|
129 |
if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) { |
130 |
return $c->render( |
131 |
status => 200, |
132 |
json => $c->objects->to_api($biblios), |
133 |
); |
134 |
} elsif ( $c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ ) { |
135 |
$c->res->headers->add( 'Content-Type', 'application/marcxml+xml' ); |
136 |
return $c->render( |
137 |
status => 200, |
138 |
text => $biblios->print_collection('marcxml') |
139 |
); |
140 |
} elsif ( $c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ ) { |
141 |
$c->res->headers->add( 'Content-Type', 'application/marc-in-json' ); |
142 |
return $c->render( |
143 |
status => 200, |
144 |
data => $biblios->print_collection('mij') |
145 |
); |
146 |
} elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) { |
147 |
$c->res->headers->add( 'Content-Type', 'application/marc' ); |
148 |
return $c->render( |
149 |
status => 200, |
150 |
text => $biblios->print_collection('marc') |
151 |
); |
152 |
} elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) { |
153 |
return $c->render( |
154 |
status => 200, |
155 |
text => $biblios->print_collection('txt') |
156 |
); |
157 |
} else { |
158 |
return $c->render( |
159 |
status => 406, |
160 |
openapi => [ |
161 |
"application/json", "application/marcxml+xml", |
162 |
"application/marc-in-json", "application/marc", |
163 |
"text/plain" |
164 |
] |
165 |
); |
166 |
} |
167 |
} catch { |
168 |
$c->unhandled_exception($_); |
169 |
}; |
170 |
} |
171 |
|
172 |
1; |