Main Koha release repository
https://koha-community.org
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
5.0 KiB
206 lines
5.0 KiB
package Koha::REST::V1::Libraries;
|
|
|
|
# This file is part of Koha.
|
|
#
|
|
# Koha is free software; you can redistribute it and/or modify it under the
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
# version.
|
|
#
|
|
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License along
|
|
# with Koha; if not, write to the Free Software Foundation, Inc.,
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
use Modern::Perl;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
use Koha::Libraries;
|
|
|
|
use Scalar::Util qw( blessed );
|
|
|
|
use Try::Tiny;
|
|
|
|
=head1 NAME
|
|
|
|
Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
|
|
|
|
=head1 API
|
|
|
|
=head2 Methods
|
|
|
|
=cut
|
|
|
|
=head3 list
|
|
|
|
Controller function that handles listing Koha::Library objects
|
|
|
|
=cut
|
|
|
|
sub list {
|
|
my $c = shift->openapi->valid_input or return;
|
|
|
|
return try {
|
|
my $libraries_set = Koha::Libraries->new;
|
|
my $libraries = $c->objects->search( $libraries_set );
|
|
return $c->render( status => 200, openapi => $libraries );
|
|
}
|
|
catch {
|
|
unless ( blessed $_ && $_->can('rethrow') ) {
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "Something went wrong, check Koha logs for details." }
|
|
);
|
|
}
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "$_" }
|
|
);
|
|
};
|
|
}
|
|
|
|
=head3 get
|
|
|
|
Controller function that handles retrieving a single Koha::Library
|
|
|
|
=cut
|
|
|
|
sub get {
|
|
my $c = shift->openapi->valid_input or return;
|
|
|
|
my $library_id = $c->validation->param('library_id');
|
|
my $library = Koha::Libraries->find( $library_id );
|
|
|
|
unless ($library) {
|
|
return $c->render( status => 404,
|
|
openapi => { error => "Library not found" } );
|
|
}
|
|
|
|
return $c->render(
|
|
status => 200,
|
|
openapi => $library->to_api
|
|
);
|
|
}
|
|
|
|
=head3 add
|
|
|
|
Controller function that handles adding a new Koha::Library object
|
|
|
|
=cut
|
|
|
|
sub add {
|
|
my $c = shift->openapi->valid_input or return;
|
|
|
|
return try {
|
|
my $library = Koha::Library->new_from_api( $c->validation->param('body') );
|
|
$library->store;
|
|
$c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
|
|
|
|
return $c->render(
|
|
status => 201,
|
|
openapi => $library->to_api
|
|
);
|
|
}
|
|
catch {
|
|
unless ( blessed $_ && $_->can('rethrow') ) {
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "Something went wrong, check Koha logs for details." }
|
|
);
|
|
}
|
|
if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
|
|
return $c->render(
|
|
status => 409,
|
|
openapi => { error => $_->error, conflict => $_->duplicate_id }
|
|
);
|
|
}
|
|
else {
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "$_" }
|
|
);
|
|
}
|
|
};
|
|
}
|
|
|
|
=head3 update
|
|
|
|
Controller function that handles updating a Koha::Library object
|
|
|
|
=cut
|
|
|
|
sub update {
|
|
my $c = shift->openapi->valid_input or return;
|
|
|
|
my $library = Koha::Libraries->find( $c->validation->param('library_id') );
|
|
|
|
if ( not defined $library ) {
|
|
return $c->render(
|
|
status => 404,
|
|
openapi => { error => "Library not found" }
|
|
);
|
|
}
|
|
|
|
return try {
|
|
my $params = $c->req->json;
|
|
$library->set_from_api( $params );
|
|
$library->store();
|
|
return $c->render(
|
|
status => 200,
|
|
openapi => $library->to_api
|
|
);
|
|
}
|
|
catch {
|
|
unless ( blessed $_ && $_->can('rethrow') ) {
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "Something went wrong, check Koha logs for details." }
|
|
);
|
|
}
|
|
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "$_" }
|
|
);
|
|
};
|
|
}
|
|
|
|
=head3 delete
|
|
|
|
Controller function that handles deleting a Koha::Library object
|
|
|
|
=cut
|
|
|
|
sub delete {
|
|
|
|
my $c = shift->openapi->valid_input or return;
|
|
|
|
my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
|
|
|
|
if ( not defined $library ) {
|
|
return $c->render( status => 404, openapi => { error => "Library not found" } );
|
|
}
|
|
|
|
return try {
|
|
$library->delete;
|
|
return $c->render( status => 204, openapi => '');
|
|
}
|
|
catch {
|
|
unless ( blessed $_ && $_->can('rethrow') ) {
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "Something went wrong, check Koha logs for details." }
|
|
);
|
|
}
|
|
|
|
return $c->render(
|
|
status => 500,
|
|
openapi => { error => "$_" }
|
|
);
|
|
};
|
|
}
|
|
|
|
1;
|
|
|