Bug 36482: Make embedding work for GET /libraries/:library_id
[koha.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 qw( catch try );
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 = $c->objects->search( Koha::Libraries->new );
48         return $c->render( status => 200, openapi => $libraries );
49     }
50     catch {
51         $c->unhandled_exception($_);
52     };
53 }
54
55 =head3 get
56
57 Controller function that handles retrieving a single Koha::Library
58
59 =cut
60
61 sub get {
62     my $c = shift->openapi->valid_input or return;
63
64     return try {
65         my $library = Koha::Libraries->find( $c->param('library_id') );
66
67         unless ($library) {
68             return $c->render(
69                 status  => 404,
70                 openapi => { error => "Library not found" }
71             );
72         }
73
74         return $c->render(
75             status  => 200,
76             openapi => $c->objects->to_api($library),
77         );
78     } catch {
79         $c->unhandled_exception($_);
80     };
81 }
82
83 =head3 add
84
85 Controller function that handles adding a new Koha::Library object
86
87 =cut
88
89 sub add {
90     my $c = shift->openapi->valid_input or return;
91
92     return try {
93         my $library = Koha::Library->new_from_api( $c->req->json );
94         $library->store;
95         $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
96
97         return $c->render(
98             status  => 201,
99             openapi => $c->objects->to_api($library),
100         );
101     }
102     catch {
103         if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
104             return $c->render(
105                 status  => 409,
106                 openapi => { error => $_->error, conflict => $_->duplicate_id }
107             );
108         }
109
110         $c->unhandled_exception($_);
111     };
112 }
113
114 =head3 update
115
116 Controller function that handles updating a Koha::Library object
117
118 =cut
119
120 sub update {
121     my $c = shift->openapi->valid_input or return;
122
123     my $library = Koha::Libraries->find( $c->param('library_id') );
124
125     if ( not defined $library ) {
126         return $c->render(
127             status  => 404,
128             openapi => { error => "Library not found" }
129         );
130     }
131
132     return try {
133         my $params = $c->req->json;
134         $library->set_from_api( $params );
135         $library->store();
136         return $c->render(
137             status  => 200,
138             openapi => $c->objects->to_api($library),
139         );
140     }
141     catch {
142         $c->unhandled_exception($_);
143     };
144 }
145
146 =head3 delete
147
148 Controller function that handles deleting a Koha::Library object
149
150 =cut
151
152 sub delete {
153
154     my $c = shift->openapi->valid_input or return;
155
156     my $library = Koha::Libraries->find( $c->param( 'library_id' ) );
157
158     if ( not defined $library ) {
159         return $c->render( status => 404, openapi => { error => "Library not found" } );
160     }
161
162     return try {
163         $library->delete;
164         return $c->render( status => 204, openapi => '');
165     }
166     catch {
167         $c->unhandled_exception($_);
168     };
169 }
170
171 =head3 list_desks
172
173 Controller function that handles retrieving the library's desks
174
175 =cut
176
177 sub list_desks {
178     my $c = shift->openapi->valid_input or return;
179
180     return $c->render( status => 404, openapi => { error => "Feature disabled" } )
181         unless C4::Context->preference('UseCirculationDesks');
182
183     return try {
184         my $library = Koha::Libraries->find( $c->param('library_id') );
185
186         unless ($library) {
187             return $c->render(
188                 status  => 404,
189                 openapi => { error => "Library not found" }
190             );
191         }
192
193         return $c->render(
194             status  => 200,
195             openapi => $c->objects->to_api( $library->desks )
196         );
197     } catch {
198         $c->unhandled_exception($_);
199     };
200 }
201
202 =head3 list_cash_registers
203
204 Controller function that handles retrieving the library's cash registers
205
206 =cut
207
208 sub list_cash_registers {
209     my $c = shift->openapi->valid_input or return;
210
211     return $c->render( status => 404, openapi => { error => "Feature disabled" } )
212         unless C4::Context->preference('UseCashRegisters');
213
214     return try {
215         my $library = Koha::Libraries->find( $c->param('library_id') );
216
217         unless ($library) {
218             return $c->render(
219                 status  => 404,
220                 openapi => { error => "Library not found" }
221             );
222         }
223
224         return $c->render(
225             status  => 200,
226             openapi => $c->objects->to_api( $library->cash_registers )
227         );
228     } catch {
229         $c->unhandled_exception($_);
230     };
231 }
232
233 1;