Bug 24545: Fix license statements
[koha-equinox.git] / Koha / REST / V1 / Libraries.pm
1 package Koha::REST::V1::Libraries;
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 use Koha::Libraries;
22
23 use Scalar::Util qw( blessed );
24
25 use Try::Tiny;
26
27 =head1 NAME
28
29 Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
30
31 =head1 API
32
33 =head2 Methods
34
35 =cut
36
37 =head3 list
38
39 Controller function that handles listing Koha::Library objects
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47         my $libraries_set = Koha::Libraries->new;
48         my $libraries     = $c->objects->search( $libraries_set );
49         return $c->render( status => 200, openapi => $libraries );
50     }
51     catch {
52         unless ( blessed $_ && $_->can('rethrow') ) {
53             return $c->render(
54                 status  => 500,
55                 openapi => { error => "Something went wrong, check Koha logs for details." }
56             );
57         }
58         return $c->render(
59             status  => 500,
60             openapi => { error => "$_" }
61         );
62     };
63 }
64
65 =head3 get
66
67 Controller function that handles retrieving a single Koha::Library
68
69 =cut
70
71 sub get {
72     my $c = shift->openapi->valid_input or return;
73
74     my $library_id = $c->validation->param('library_id');
75     my $library = Koha::Libraries->find( $library_id );
76
77     unless ($library) {
78         return $c->render( status  => 404,
79                            openapi => { error => "Library not found" } );
80     }
81
82     return $c->render(
83         status  => 200,
84         openapi => $library->to_api
85     );
86 }
87
88 =head3 add
89
90 Controller function that handles adding a new Koha::Library object
91
92 =cut
93
94 sub add {
95     my $c = shift->openapi->valid_input or return;
96
97     return try {
98         my $library = Koha::Library->new_from_api( $c->validation->param('body') );
99         $library->store;
100         $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
101
102         return $c->render(
103             status  => 201,
104             openapi => $library->to_api
105         );
106     }
107     catch {
108         unless ( blessed $_ && $_->can('rethrow') ) {
109             return $c->render(
110                 status  => 500,
111                 openapi => { error => "Something went wrong, check Koha logs for details." }
112             );
113         }
114         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
115             return $c->render(
116                 status  => 409,
117                 openapi => { error => $_->error, conflict => $_->duplicate_id }
118             );
119         }
120         else {
121             return $c->render(
122                 status  => 500,
123                 openapi => { error => "$_" }
124             );
125         }
126     };
127 }
128
129 =head3 update
130
131 Controller function that handles updating a Koha::Library object
132
133 =cut
134
135 sub update {
136     my $c = shift->openapi->valid_input or return;
137
138     my $library = Koha::Libraries->find( $c->validation->param('library_id') );
139
140     if ( not defined $library ) {
141         return $c->render(
142             status  => 404,
143             openapi => { error => "Library not found" }
144         );
145     }
146
147     return try {
148         my $params = $c->req->json;
149         $library->set_from_api( $params );
150         $library->store();
151         return $c->render(
152             status  => 200,
153             openapi => $library->to_api
154         );
155     }
156     catch {
157         unless ( blessed $_ && $_->can('rethrow') ) {
158             return $c->render(
159                 status  => 500,
160                 openapi => { error => "Something went wrong, check Koha logs for details." }
161             );
162         }
163
164         return $c->render(
165             status  => 500,
166             openapi => { error => "$_" }
167         );
168     };
169 }
170
171 =head3 delete
172
173 Controller function that handles deleting a Koha::Library object
174
175 =cut
176
177 sub delete {
178
179     my $c = shift->openapi->valid_input or return;
180
181     my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
182
183     if ( not defined $library ) {
184         return $c->render( status => 404, openapi => { error => "Library not found" } );
185     }
186
187     return try {
188         $library->delete;
189         return $c->render( status => 204, openapi => '');
190     }
191     catch {
192         unless ( blessed $_ && $_->can('rethrow') ) {
193             return $c->render(
194                 status  => 500,
195                 openapi => { error => "Something went wrong, check Koha logs for details." }
196             );
197         }
198
199         return $c->render(
200             status  => 500,
201             openapi => { error => "$_" }
202         );
203     };
204 }
205
206 1;