1 package Koha::REST::V1::Library;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
23 use Scalar::Util qw( blessed );
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;
48 my $args = $c->req->params->to_hash;
50 for my $filter_param ( keys %$args ) {
51 $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
55 my $libraries = Koha::Libraries->search($filter);
56 return $c->render( status => 200, openapi => $libraries );
59 if ( $_->isa('DBIx::Class::Exception') ) {
60 return $c->render( status => 500,
61 openapi => { error => $_->{msg} } );
64 return $c->render( status => 500,
65 openapi => { error => "Something went wrong, check the logs."} );
72 Controller function that handles retrieving a single Koha::Library
77 my $c = shift->openapi->valid_input or return;
79 my $branchcode = $c->validation->param('branchcode');
80 my $library = Koha::Libraries->find({ branchcode => $branchcode });
82 return $c->render( status => 404,
83 openapi => { error => "Library not found" } );
86 return $c->render( status => 200, openapi => $library );
91 Controller function that handles adding a new Koha::Library object
96 my $c = shift->openapi->valid_input or return;
99 if (Koha::Libraries->find($c->req->json->{branchcode})) {
100 return $c->render( status => 400,
101 openapi => { error => 'Library already exists' } );
103 my $library = Koha::Library->new($c->validation->param('body'))->store;
104 my $branchcode = $library->branchcode;
105 $c->res->headers->location($c->req->url->to_string.'/'.$branchcode);
106 return $c->render( status => 201, openapi => $library);
109 if ( $_->isa('DBIx::Class::Exception') ) {
110 return $c->render( status => 500,
111 openapi => { error => $_->{msg} } );
114 return $c->render( status => 500,
115 openapi => { error => "Something went wrong, check the logs."} );
122 Controller function that handles updating a Koha::Library object
127 my $c = shift->openapi->valid_input or return;
131 $library = Koha::Libraries->find($c->validation->param('branchcode'));
132 $library->set($c->validation->param('body'))->store;
133 return $c->render( status => 200, openapi => $library );
136 if ( not defined $library ) {
137 return $c->render( status => 404,
138 openapi => { error => "Object not found" });
140 elsif ( $_->isa('DBIx::Class::Exception') ) {
141 return $c->render( status => 500,
142 openapi => { error => $_->{msg} } );
145 return $c->render( status => 500,
146 openapi => { error => "Something went wrong, check the logs."} );
153 Controller function that handles deleting a Koha::Library object
158 my $c = shift->openapi->valid_input or return;
162 $library = Koha::Libraries->find($c->validation->param('branchcode'));
164 return $c->render( status => 204, openapi => '');
167 if ( not defined $library ) {
168 return $c->render( status => 404, openapi => { error => "Object not found" } );
170 elsif ( $_->isa('DBIx::Class::Exception') ) {
171 return $c->render( status => 500,
172 openapi => { error => $_->{msg} } );
175 return $c->render( status => 500,
176 openapi => { error => "Something went wrong, check the logs."} );