Bug 24545: Fix license statements
[koha.git] / Koha / REST / V1 / Biblios.pm
1 package Koha::REST::V1::Biblios;
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::Biblios;
23 use C4::Biblio qw(DelBiblio);
24
25 use MARC::Record::MiJ;
26
27 use Try::Tiny;
28
29 =head1 API
30
31 =head2 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   => $biblio->to_api
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 }
103
104 =head3 delete
105
106 Controller function that handles deleting a biblio object
107
108 =cut
109
110 sub delete {
111     my $c = shift->openapi->valid_input or return;
112
113     my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
114
115     if ( not defined $biblio ) {
116         return $c->render(
117             status  => 404,
118             openapi => { error => "Object not found" }
119         );
120     }
121
122     return try {
123         my $error = DelBiblio( $biblio->id );
124
125         if ($error) {
126             return $c->render(
127                 status  => 409,
128                 openapi => { error => $error }
129             );
130         }
131         else {
132             return $c->render( status => 204, openapi => "" );
133         }
134     }
135     catch {
136         if ( $_->isa('DBIx::Class::Exception') ) {
137             return $c->render(
138                 status  => 500,
139                 openapi => { error => $_->{msg} }
140             );
141         }
142         else {
143             return $c->render(
144                 status  => 500,
145                 openapi => { error => "Something went wrong, check the logs." }
146             );
147         }
148     };
149 }
150
151 1;