Lines 22-35
use Mojo::Base 'Mojolicious::Controller';
Link Here
|
22 |
use Koha::Biblios; |
22 |
use Koha::Biblios; |
23 |
use C4::Biblio qw(DelBiblio); |
23 |
use C4::Biblio qw(DelBiblio); |
24 |
|
24 |
|
|
|
25 |
use MARC::Record::MiJ; |
26 |
|
25 |
use Try::Tiny; |
27 |
use Try::Tiny; |
26 |
|
28 |
|
27 |
=head1 API |
29 |
=head1 API |
28 |
|
30 |
|
29 |
=head2 Class Methods |
31 |
=head2 Class methods |
|
|
32 |
|
33 |
=head3 get |
34 |
|
35 |
Controller function that handles retrieving a single biblio object |
36 |
|
37 |
=cut |
38 |
|
39 |
sub get { |
40 |
my $c = shift->openapi->valid_input or return; |
41 |
|
42 |
my $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed |
43 |
unless $c->req->headers->accept =~ m/application\/json/; |
44 |
|
45 |
my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes ); |
46 |
|
47 |
unless ( $biblio ) { |
48 |
return $c->render( |
49 |
status => 404, |
50 |
openapi => { |
51 |
error => "Object not found." |
52 |
} |
53 |
); |
54 |
} |
55 |
|
56 |
return try { |
57 |
|
58 |
if ( $c->req->headers->accept =~ m/application\/json/ ) { |
59 |
return $c->render( |
60 |
status => 200, |
61 |
json => $c->build_json_biblio( { biblio => $biblio } ) |
62 |
); |
63 |
} |
64 |
else { |
65 |
my $record = $biblio->metadata->record; |
66 |
|
67 |
$c->respond_to( |
68 |
marcxml => { |
69 |
status => 200, |
70 |
format => 'marcxml', |
71 |
text => $record->as_xml_record |
72 |
}, |
73 |
mij => { |
74 |
status => 200, |
75 |
format => 'mij', |
76 |
text => $record->to_mij |
77 |
}, |
78 |
marc => { |
79 |
status => 200, |
80 |
format => 'marc', |
81 |
text => $record->as_usmarc |
82 |
}, |
83 |
any => { |
84 |
status => 406, |
85 |
openapi => [ |
86 |
"application/json", |
87 |
"application/marcxml+xml", |
88 |
"application/marc-in-json", |
89 |
"application/marc" |
90 |
] |
91 |
} |
92 |
); |
93 |
} |
94 |
} |
95 |
catch { |
96 |
return $c->render( |
97 |
status => 500, |
98 |
openapi => { error => "Something went wrong, check the logs ($_)" } |
99 |
); |
100 |
}; |
101 |
} |
30 |
|
102 |
|
31 |
=head3 delete |
103 |
=head3 delete |
32 |
|
104 |
|
|
|
105 |
Controller function that handles deleting a biblio object |
106 |
|
33 |
=cut |
107 |
=cut |
34 |
|
108 |
|
35 |
sub delete { |
109 |
sub delete { |
Lines 73-76
sub delete {
Link Here
|
73 |
}; |
147 |
}; |
74 |
} |
148 |
} |
75 |
|
149 |
|
|
|
150 |
=head2 Internal methods |
151 |
|
152 |
|
153 |
=head3 _to_api |
154 |
|
155 |
Helper function that maps unblessed Koha::Patron objects into REST api |
156 |
attribute names. |
157 |
|
158 |
=cut |
159 |
|
160 |
sub _to_api { |
161 |
my $biblio = shift; |
162 |
|
163 |
# Rename attributes |
164 |
foreach my $column ( keys %{$Koha::REST::V1::Biblios::to_api_mapping} ) { |
165 |
my $mapped_column = $Koha::REST::V1::Biblios::to_api_mapping->{$column}; |
166 |
if ( exists $biblio->{$column} |
167 |
&& defined $mapped_column ) |
168 |
{ |
169 |
# key != undef |
170 |
$biblio->{$mapped_column} = delete $biblio->{$column}; |
171 |
} |
172 |
elsif ( exists $biblio->{$column} |
173 |
&& !defined $mapped_column ) |
174 |
{ |
175 |
# key == undef |
176 |
delete $biblio->{$column}; |
177 |
} |
178 |
} |
179 |
|
180 |
return $biblio; |
181 |
} |
182 |
|
183 |
|
184 |
=head3 build_json_biblio |
185 |
|
186 |
Internal method that returns all the attributes from the biblio and biblioitems tables |
187 |
|
188 |
=cut |
189 |
|
190 |
sub build_json_biblio { |
191 |
my ( $c, $args ) = @_; |
192 |
|
193 |
my $biblio = $args->{biblio}; |
194 |
|
195 |
my $response = $biblio->TO_JSON; |
196 |
my $biblioitem = $biblio->biblioitem->TO_JSON; |
197 |
|
198 |
foreach my $key ( keys %{ $biblioitem } ) { |
199 |
$response->{$key} = $biblioitem->{$key}; |
200 |
} |
201 |
|
202 |
return _to_api($response); |
203 |
} |
204 |
|
205 |
|
206 |
=head2 Global variables |
207 |
|
208 |
=head3 $to_api_mapping |
209 |
|
210 |
=cut |
211 |
|
212 |
our $to_api_mapping = { |
213 |
agerestriction => 'age_restriction', |
214 |
biblioitemnumber => undef, # meaningless |
215 |
biblionumber => 'biblio_id', |
216 |
collectionissn => 'collection_issn', |
217 |
collectiontitle => 'collection_title', |
218 |
collectionvolume => 'collection_volume', |
219 |
copyrightdate => 'copyright_date', |
220 |
datecreated => 'creation_date', |
221 |
editionresponsibility => undef, # obsolete, not mapped |
222 |
editionstatement => 'edition_statement', |
223 |
frameworkcode => 'framework_id', |
224 |
illus => 'illustrations', |
225 |
itemtype => 'item_type', |
226 |
lccn => 'lc_control_number', |
227 |
place => 'publication_place', |
228 |
publicationyear => 'publication_year', |
229 |
publishercode => 'publisher', |
230 |
seriestitle => 'series_title', |
231 |
size => 'material_size', |
232 |
totalissues => 'serial_total_issues', |
233 |
unititle => 'uniform_title', |
234 |
volumedate => 'volume_date', |
235 |
volumedesc => 'volume_description', |
236 |
}; |
237 |
|
76 |
1; |
238 |
1; |
77 |
- |
|
|