1 package Koha::REST::V1::TransferLimits;
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';
21 use Koha::Item::Transfer::Limits;
24 use Koha::Exceptions::TransferLimit;
26 use Scalar::Util qw( blessed );
28 use Try::Tiny qw( catch try );
32 Koha::REST::V1::TransferLimits - Koha REST API for handling libraries (V1)
42 Controller function that handles listing Koha::Item::Transfer::Limits objects
47 my $c = shift->openapi->valid_input or return;
50 my $limits_set = Koha::Item::Transfer::Limits->new;
51 my $limits = $c->objects->search( $limits_set );
52 return $c->render( status => 200, openapi => $limits );
55 $c->unhandled_exception( $_ );
61 Controller function that handles adding a new transfer limit
66 my $c = shift->openapi->valid_input or return;
69 my $params = $c->validation->param( 'body' );
70 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
72 if ( Koha::Item::Transfer::Limits->search( $transfer_limit->attributes_from_api($params) )->count == 0 ) {
73 $transfer_limit->store;
75 Koha::Exceptions::TransferLimit::Duplicate->throw();
80 openapi => $transfer_limit->to_api
84 if ( blessed $_ && $_->isa('Koha::Exceptions::TransferLimit::Duplicate') ) {
87 openapi => { error => "$_" }
91 $c->unhandled_exception($_);
97 Controller function that handles deleting a transfer limit
103 my $c = shift->openapi->valid_input or return;
105 my $transfer_limit = Koha::Item::Transfer::Limits->find( $c->validation->param( 'limit_id' ) );
107 if ( not defined $transfer_limit ) {
108 return $c->render( status => 404, openapi => { error => "Transfer limit not found" } );
112 $transfer_limit->delete;
113 return $c->render( status => 204, openapi => '');
116 $c->unhandled_exception($_);
122 Controller function that handles adding a new transfer limit
127 my $c = shift->openapi->valid_input or return;
130 my $params = $c->validation->param( 'body' );
132 my @libraries = Koha::Libraries->search->as_list;
134 my @from_branches = $params->{from_library_id} ? $params->{from_library_id} : map { $_->id } @libraries;
135 my @to_branches = $params->{to_library_id} ? $params->{to_library_id} : map { $_->id } @libraries;
138 foreach my $from ( @from_branches ) {
139 foreach my $to ( @to_branches ) {
140 my $limit_params = { %$params };
142 $limit_params->{from_library_id} = $from;
143 $limit_params->{to_library_id} = $to;
145 next if $to eq $from;
147 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $limit_params );
148 my $exists = Koha::Item::Transfer::Limits->search( $transfer_limit->unblessed )->count;
150 $transfer_limit->store;
151 push( @results, $transfer_limit->to_api());
155 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
163 $c->unhandled_exception($_);
169 Controller function that handles batch deleting transfer limits
175 my $c = shift->openapi->valid_input or return;
178 my $params = $c->validation->param( 'body' );
179 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
180 my $search_params = $transfer_limit->unblessed;
182 Koha::Item::Transfer::Limits->search($search_params)->delete;
184 return $c->render( status => 204, openapi => '');
187 $c->unhandled_exception($_);