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