1 package Koha::REST::V1::Authorities;
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>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Authorities;
23 use C4::AuthoritiesMarc qw( DelAuthority AddAuthority FindDuplicateAuthority ModAuthority);
25 use List::MoreUtils qw( any );
26 use MARC::Record::MiJ;
28 use Try::Tiny qw( catch try );
36 Controller function that handles retrieving a single authority object
41 my $c = shift->openapi->valid_input or return;
43 my $authority = Koha::Authorities->find( { authid => $c->param('authority_id') } );
44 unless ( $authority ) {
48 error => "Object not found."
55 if ( $c->req->headers->accept =~ m/application\/json/ ) {
58 json => $authority->to_api
62 my $record = $authority->record;
68 text => $record->as_xml_record
73 data => $record->to_mij
78 text => $record->as_usmarc
82 format => 'text/plain',
83 text => $record->as_formatted
89 "application/marcxml+xml",
90 "application/marc-in-json",
99 $c->unhandled_exception($_);
105 Controller function that handles deleting an authority object
110 my $c = shift->openapi->valid_input or return;
112 my $authority = Koha::Authorities->find( { authid => $c->param('authority_id') } );
114 if ( not defined $authority ) {
117 openapi => { error => "Object not found" }
122 DelAuthority( { authid => $authority->authid } );
124 return $c->render( status => 204, openapi => q{} );
127 $c->unhandled_exception($_);
133 Controller function that handles creating an authority object
138 my $c = shift->openapi->valid_input or return;
141 my $headers = $c->req->headers;
142 my $overrides = $c->stash('koha.overrides');
145 C4::Context->preference('marcflavour') eq 'UNIMARC'
151 my $authtypecode = $headers->header('x-authority-type');
152 my $content_type = $headers->content_type;
154 if ( $content_type =~ m/application\/marcxml\+xml/ ) {
155 $record = MARC::Record->new_from_xml( $c->req->body, 'UTF-8', $flavour );
156 } elsif ( $content_type =~ m/application\/marc-in-json/ ) {
157 $record = MARC::Record->new_from_mij_structure( $c->req->json );
158 } elsif ( $content_type =~ m/application\/marc/ ) {
159 $record = MARC::Record->new_from_usmarc( $c->req->body );
164 "application/marcxml+xml",
165 "application/marc-in-json",
171 unless ( $overrides->{any} || $overrides->{duplicate} ) {
172 my ( $duplicateauthid, $duplicateauthvalue ) = C4::AuthoritiesMarc::FindDuplicateAuthority( $record, $authtypecode );
177 error => "Duplicate record ($duplicateauthid)",
178 error_code => 'duplicate',
180 ) unless !$duplicateauthid;
183 my $authid = AddAuthority( $record, undef, $authtypecode );
185 $c->res->headers->location($c->req->url->to_string . '/' . $authid);
192 $c->unhandled_exception($_);
199 Controller function that handles modifying an authority object
204 my $c = shift->openapi->valid_input or return;
206 my $authid = $c->param('authority_id');
207 my $authority = Koha::Authorities->find( { authid => $authid } );
209 if ( not defined $authority ) {
212 openapi => { error => "Object not found" }
217 my $headers = $c->req->headers;
220 C4::Context->preference('marcflavour') eq 'UNIMARC'
225 my $authtypecode = $headers->header('x-authority-type') || $authority->authtypecode;
226 if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
227 $record = MARC::Record->new_from_xml( $c->req->body, 'UTF-8', $flavour );
228 } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
229 $record = MARC::Record->new_from_mij_structure( $c->req->json );
230 } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
231 $record = MARC::Record->new_from_usmarc( $c->req->body );
237 "application/marcxml+xml",
238 "application/marc-in-json",
244 my $authid = ModAuthority( $authid, $record, $authtypecode );
248 openapi => { id => $authid }
252 $c->unhandled_exception($_);
258 Controller function that handles retrieving a list of authorities
263 my $c = shift->openapi->valid_input or return;
265 my $authorities = $c->objects->search_rs( Koha::Authorities->new );
269 if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) {
272 json => $authorities->to_api
276 $c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ )
278 $c->res->headers->add( 'Content-Type', 'application/marcxml+xml' );
281 text => $authorities->print_collection('marcxml')
285 $c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ )
287 $c->res->headers->add( 'Content-Type', 'application/marc-in-json' );
290 data => $authorities->print_collection('mij')
293 elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) {
294 $c->res->headers->add( 'Content-Type', 'application/marc' );
297 text => $authorities->print_collection('marc')
300 elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) {
303 text => $authorities->print_collection('txt')
310 "application/json", "application/marcxml+xml",
311 "application/marc-in-json", "application/marc",
318 $c->unhandled_exception($_);