1 package Koha::REST::V1::Libraries;
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';
23 use Scalar::Util qw( blessed );
25 use Try::Tiny qw( catch try );
29 Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
39 Controller function that handles listing Koha::Library objects
44 my $c = shift->openapi->valid_input or return;
47 my $libraries = $c->objects->search( Koha::Libraries->new );
48 return $c->render( status => 200, openapi => $libraries );
51 $c->unhandled_exception($_);
57 Controller function that handles retrieving a single Koha::Library
62 my $c = shift->openapi->valid_input or return;
65 my $library = Koha::Libraries->find( $c->param('library_id') );
70 openapi => { error => "Library not found" }
76 openapi => $c->objects->to_api($library),
79 $c->unhandled_exception($_);
85 Controller function that handles adding a new Koha::Library object
90 my $c = shift->openapi->valid_input or return;
93 my $library = Koha::Library->new_from_api( $c->req->json );
95 $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
99 openapi => $c->objects->to_api($library),
103 if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
106 openapi => { error => $_->error, conflict => $_->duplicate_id }
110 $c->unhandled_exception($_);
116 Controller function that handles updating a Koha::Library object
121 my $c = shift->openapi->valid_input or return;
123 my $library = Koha::Libraries->find( $c->param('library_id') );
125 if ( not defined $library ) {
128 openapi => { error => "Library not found" }
133 my $params = $c->req->json;
134 $library->set_from_api( $params );
138 openapi => $c->objects->to_api($library),
142 $c->unhandled_exception($_);
148 Controller function that handles deleting a Koha::Library object
154 my $c = shift->openapi->valid_input or return;
156 my $library = Koha::Libraries->find( $c->param( 'library_id' ) );
158 if ( not defined $library ) {
159 return $c->render( status => 404, openapi => { error => "Library not found" } );
164 return $c->render( status => 204, openapi => '');
167 $c->unhandled_exception($_);
173 Controller function that handles retrieving the library's desks
178 my $c = shift->openapi->valid_input or return;
180 return $c->render( status => 404, openapi => { error => "Feature disabled" } )
181 unless C4::Context->preference('UseCirculationDesks');
184 my $library = Koha::Libraries->find( $c->param('library_id') );
189 openapi => { error => "Library not found" }
195 openapi => $c->objects->to_api( $library->desks )
198 $c->unhandled_exception($_);
202 =head3 list_cash_registers
204 Controller function that handles retrieving the library's cash registers
208 sub list_cash_registers {
209 my $c = shift->openapi->valid_input or return;
211 return $c->render( status => 404, openapi => { error => "Feature disabled" } )
212 unless C4::Context->preference('UseCashRegisters');
215 my $library = Koha::Libraries->find( $c->param('library_id') );
220 openapi => { error => "Library not found" }
226 openapi => $c->objects->to_api( $library->cash_registers )
229 $c->unhandled_exception($_);