1 package Koha::REST::V1::Illbatches;
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 Koha::IllbatchStatuses;
24 use Koha::Illrequests;
26 use Try::Tiny qw( catch try );
30 Koha::REST::V1::Illbatches
36 Return a list of available ILL batches
41 my $c = shift->openapi->valid_input;
43 #FIXME: This should be $c->objects-search
44 my @batches = Koha::Illbatches->search()->as_list;
46 #FIXME: Below should be coming from $c->objects accessors
47 # Get all patrons associated with all our batches
50 foreach my $batch (@batches) {
51 my $patron_id = $batch->borrowernumber;
52 $patrons->{$patron_id} = 1;
54 my @patron_ids = keys %{$patrons};
55 my $patron_results = Koha::Patrons->search( { borrowernumber => { -in => \@patron_ids } } );
57 # Get all branches associated with all our batches
60 foreach my $batch (@batches) {
61 my $branch_id = $batch->branchcode;
62 $branches->{$branch_id} = 1;
64 my @branchcodes = keys %{$branches};
65 my $branch_results = Koha::Libraries->search( { branchcode => { -in => \@branchcodes } } );
67 # Get all batch statuses associated with all our batches
70 foreach my $batch (@batches) {
71 my $code = $batch->statuscode;
72 $statuses->{$code} = 1;
74 my @statuscodes = keys %{$statuses};
75 my $status_results = Koha::IllbatchStatuses->search( { code => { -in => \@statuscodes } } );
77 # Populate the response
79 foreach my $it_batch (@batches) {
80 my $patron = $patron_results->find( { borrowernumber => $it_batch->borrowernumber } );
81 my $branch = $branch_results->find( { branchcode => $it_batch->branchcode } );
82 my $status = $status_results->find( { code => $it_batch->statuscode } );
84 batch_id => $it_batch->id,
85 backend => $it_batch->backend,
86 library_id => $it_batch->branchcode,
87 name => $it_batch->name,
88 statuscode => $it_batch->statuscode,
89 patron_id => $it_batch->borrowernumber,
93 requests_count => $it_batch->requests_count
97 return $c->render( status => 200, openapi => \@to_return );
107 my $c = shift->openapi->valid_input;
109 my $batchid = $c->param('ill_batch_id');
111 my $batch = Koha::Illbatches->find($batchid);
113 if ( not defined $batch ) {
116 openapi => { error => "ILL batch not found" }
123 batch_id => $batch->id,
124 backend => $batch->backend,
125 library_id => $batch->branchcode,
126 name => $batch->name,
127 statuscode => $batch->statuscode,
128 patron_id => $batch->borrowernumber,
129 patron => $batch->patron->unblessed,
130 branch => $batch->branch->unblessed,
131 status => $batch->status->unblessed,
132 requests_count => $batch->requests_count
144 my $c = shift->openapi->valid_input or return;
146 my $body = $c->req->json;
148 # We receive cardnumber, so we need to look up the corresponding
150 my $patron = Koha::Patrons->find( { cardnumber => $body->{cardnumber} } );
152 if ( not defined $patron ) {
155 openapi => { error => "Patron with cardnumber " . $body->{cardnumber} . " not found" }
159 delete $body->{cardnumber};
160 $body->{borrowernumber} = $patron->borrowernumber;
161 $body->{branchcode} = delete $body->{library_id};
164 my $batch = Koha::Illbatch->new($body);
165 $batch->create_and_log;
166 $c->res->headers->location( $c->req->url->to_string . '/' . $batch->id );
169 batch_id => $batch->id,
170 backend => $batch->backend,
171 library_id => $batch->branchcode,
172 name => $batch->name,
173 statuscode => $batch->statuscode,
174 patron_id => $batch->borrowernumber,
175 patron => $batch->patron->unblessed,
176 branch => $batch->branch->unblessed,
177 status => $batch->status->unblessed,
187 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
190 openapi => { error => "A batch named " . $body->{name} . " already exists" }
194 $c->unhandled_exception($_);
205 my $c = shift->openapi->valid_input or return;
207 my $batch = Koha::Illbatches->find( $c->param('ill_batch_id') );
209 if ( not defined $batch ) {
212 openapi => { error => "ILL batch not found" }
216 my $params = $c->req->json;
217 delete $params->{cardnumber};
218 $params->{borrowernumber} = delete $params->{patron_id} if $params->{patron_id};
219 $params->{branchcode} = delete $params->{library_id} if $params->{library_id};
222 $batch->update_and_log($params);
225 batch_id => $batch->id,
226 backend => $batch->backend,
227 library_id => $batch->branchcode,
228 name => $batch->name,
229 statuscode => $batch->statuscode,
230 patron_id => $batch->borrowernumber,
231 patron => $batch->patron->unblessed,
232 branch => $batch->branch->unblessed,
233 status => $batch->status->unblessed,
234 requests_count => $batch->requests_count
242 $c->unhandled_exception($_);
254 my $c = shift->openapi->valid_input or return;
256 my $batch = Koha::Illbatches->find( $c->param('ill_batch_id') );
258 if ( not defined $batch ) {
259 return $c->render( status => 404, openapi => { error => "ILL batch not found" } );
263 $batch->delete_and_log;
264 return $c->render( status => 204, openapi => '' );
266 $c->unhandled_exception($_);