Bug 26633: Add REST API for managing transfer limits
[koha.git] / Koha / REST / V1 / TransferLimits.pm
1 package Koha::REST::V1::TransferLimits;
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::Item::Transfer::Limits;
22 use Koha::Libraries;
23
24 use Koha::Exceptions::TransferLimit;
25
26 use Scalar::Util qw( blessed );
27
28 use Try::Tiny;
29
30 =head1 NAME
31
32 Koha::REST::V1::TransferLimits - Koha REST API for handling libraries (V1)
33
34 =head1 API
35
36 =head2 Methods
37
38 =cut
39
40 =head3 list
41
42 Controller function that handles listing Koha::Item::Transfer::Limits objects
43
44 =cut
45
46 sub list {
47     my $c = shift->openapi->valid_input or return;
48
49     return try {
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 );
53     }
54     catch {
55         $c->unhandled_exception( $_ );
56     };
57 }
58
59 =head3 add
60
61 Controller function that handles adding a new transfer limit
62
63 =cut
64
65 sub add {
66     my $c = shift->openapi->valid_input or return;
67
68     return try {
69         my $params = $c->validation->param( 'body' );
70         my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
71
72         if ( Koha::Item::Transfer::Limits->search( $transfer_limit->attributes_from_api($params) )->count == 0 ) {
73             $transfer_limit->store;
74         } else {
75             Koha::Exceptions::TransferLimit::Duplicate->throw()
76         }
77
78         return $c->render(
79             status  => 201,
80             openapi => $transfer_limit->to_api
81         );
82     }
83     catch {
84         if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
85             return $c->render(
86                 status  => 409,
87                 openapi => { error => $_->error, conflict => $_->duplicate_id }
88             );
89         }
90
91         $c->unhandled_exception($_);
92     };
93 }
94
95 =head3 delete
96
97 Controller function that handles deleting a transfer limit
98
99 =cut
100
101 sub delete {
102
103     my $c = shift->openapi->valid_input or return;
104
105     my $transfer_limit = Koha::Item::Transfer::Limits->find( $c->validation->param( 'limit_id' ) );
106
107     if ( not defined $transfer_limit ) {
108         return $c->render( status => 404, openapi => { error => "Transfer limit not found" } );
109     }
110
111     return try {
112         $transfer_limit->delete;
113         return $c->render( status => 204, openapi => '');
114     }
115     catch {
116         $c->unhandled_exception($_);
117     };
118 }
119
120 =head3 batch_add
121
122 Controller function that handles adding a new transfer limit
123
124 =cut
125
126 sub batch_add {
127     my $c = shift->openapi->valid_input or return;
128
129     return try {
130         my $params = $c->validation->param( 'body' );
131
132         my @libraries = Koha::Libraries->search->as_list;
133
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;
136
137         my @results;
138         foreach my $from ( @from_branches ) {
139             foreach my $to ( @to_branches ) {
140                 my $limit_params = { %$params };
141
142                 $limit_params->{from_library_id} = $from;
143                 $limit_params->{to_library_id} = $to;
144
145                 next if $to eq $from;
146
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;
149                 unless ( $exists ) {
150                     $transfer_limit->store;
151                     push( @results, $transfer_limit->to_api());
152                 }
153             }
154         }
155         my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
156
157         return $c->render(
158             status  => 201,
159             openapi => \@results
160         );
161     }
162     catch {
163         $c->unhandled_exception($_);
164     };
165 }
166
167 =head3 batch_delete
168
169 Controller function that handles batch deleting transfer limits
170
171 =cut
172
173 sub batch_delete {
174
175     my $c = shift->openapi->valid_input or return;
176
177     return try {
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;
181
182         Koha::Item::Transfer::Limits->search($search_params)->delete;
183
184         return $c->render( status => 204, openapi => '');
185     }
186     catch {
187         $c->unhandled_exception($_);
188     };
189 }
190
191 1;