Bug 36480: Add GET /libraries/:library_id/desks
[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 => $library->to_api
77         );
78     }
79     catch {
80         $c->unhandled_exception($_);
81     };
82 }
83
84 =head3 add
85
86 Controller function that handles adding a new Koha::Library object
87
88 =cut
89
90 sub add {
91     my $c = shift->openapi->valid_input or return;
92
93     return try {
94         my $library = Koha::Library->new_from_api( $c->req->json );
95         $library->store;
96         $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
97
98         return $c->render(
99             status  => 201,
100             openapi => $c->objects->to_api($library),
101         );
102     }
103     catch {
104         if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
105             return $c->render(
106                 status  => 409,
107                 openapi => { error => $_->error, conflict => $_->duplicate_id }
108             );
109         }
110
111         $c->unhandled_exception($_);
112     };
113 }
114
115 =head3 update
116
117 Controller function that handles updating a Koha::Library object
118
119 =cut
120
121 sub update {
122     my $c = shift->openapi->valid_input or return;
123
124     my $library = Koha::Libraries->find( $c->param('library_id') );
125
126     if ( not defined $library ) {
127         return $c->render(
128             status  => 404,
129             openapi => { error => "Library not found" }
130         );
131     }
132
133     return try {
134         my $params = $c->req->json;
135         $library->set_from_api( $params );
136         $library->store();
137         return $c->render(
138             status  => 200,
139             openapi => $c->objects->to_api($library),
140         );
141     }
142     catch {
143         $c->unhandled_exception($_);
144     };
145 }
146
147 =head3 delete
148
149 Controller function that handles deleting a Koha::Library object
150
151 =cut
152
153 sub delete {
154
155     my $c = shift->openapi->valid_input or return;
156
157     my $library = Koha::Libraries->find( $c->param( 'library_id' ) );
158
159     if ( not defined $library ) {
160         return $c->render( status => 404, openapi => { error => "Library not found" } );
161     }
162
163     return try {
164         $library->delete;
165         return $c->render( status => 204, openapi => '');
166     }
167     catch {
168         $c->unhandled_exception($_);
169     };
170 }
171
172 =head3 list_desks
173
174 Controller function that handles retrieving the library's desks
175
176 =cut
177
178 sub list_desks {
179     my $c = shift->openapi->valid_input or return;
180
181     return $c->render( status => 404, openapi => { error => "Feature disabled" } )
182         unless C4::Context->preference('UseCirculationDesks');
183
184     return try {
185         my $library = Koha::Libraries->find( $c->param('library_id') );
186
187         unless ($library) {
188             return $c->render(
189                 status  => 404,
190                 openapi => { error => "Library not found" }
191             );
192         }
193
194         return $c->render(
195             status  => 200,
196             openapi => $c->objects->to_api( $library->desks )
197         );
198     } catch {
199         $c->unhandled_exception($_);
200     };
201 }
202
203 1;