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({
56 borrowernumber => { -in => \@patron_ids }
59 # Get all branches associated with all our batches
62 foreach my $batch(@batches) {
63 my $branch_id = $batch->branchcode;
64 $branches->{$branch_id} = 1
66 my @branchcodes = keys %{$branches};
67 my $branch_results = Koha::Libraries->search({
68 branchcode => { -in => \@branchcodes }
71 # Get all batch statuses associated with all our batches
74 foreach my $batch(@batches) {
75 my $code = $batch->statuscode;
76 $statuses->{$code} = 1
78 my @statuscodes = keys %{$statuses};
79 my $status_results = Koha::IllbatchStatuses->search({
80 code => { -in => \@statuscodes }
83 # Populate the response
85 foreach my $it_batch(@batches) {
86 my $patron = $patron_results->find({ borrowernumber => $it_batch->borrowernumber});
87 my $branch = $branch_results->find({ branchcode => $it_batch->branchcode });
88 my $status = $status_results->find({ code => $it_batch->statuscode });
90 %{$it_batch->unblessed},
94 requests_count => $it_batch->requests_count
98 return $c->render( status => 200, openapi => \@to_return );
108 my $c = shift->openapi->valid_input;
110 my $batchid = $c->validation->param('illbatch_id');
112 my $batch = Koha::Illbatches->find($batchid);
114 if (not defined $batch) {
117 openapi => { error => "ILL batch not found" }
124 %{$batch->unblessed},
125 patron => $batch->patron->unblessed,
126 branch => $batch->branch->unblessed,
127 status => $batch->status->unblessed,
128 requests_count => $batch->requests_count
140 my $c = shift->openapi->valid_input or return;
142 my $body = $c->validation->param('body');
144 # We receive cardnumber, so we need to look up the corresponding
146 my $patron = Koha::Patrons->find({ cardnumber => $body->{cardnumber} });
148 if ( not defined $patron ) {
151 openapi => { error => "Patron with cardnumber " . $body->{cardnumber} . " not found" }
155 delete $body->{cardnumber};
156 $body->{borrowernumber} = $patron->borrowernumber;
159 my $batch = Koha::Illbatch->new( $body );
160 $batch->create_and_log;
161 $c->res->headers->location( $c->req->url->to_string . '/' . $batch->id );
164 %{$batch->unblessed},
165 patron => $batch->patron->unblessed,
166 branch => $batch->branch->unblessed,
167 status => $batch->status->unblessed,
178 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
181 openapi => { error => "A batch named " . $body->{name} . " already exists" }
185 $c->unhandled_exception($_);
196 my $c = shift->openapi->valid_input or return;
198 my $batch = Koha::Illbatches->find( $c->validation->param('illbatch_id') );
200 if ( not defined $batch ) {
203 openapi => { error => "ILL batch not found" }
207 my $params = $c->req->json;
208 delete $params->{cardnumber};
211 $batch->update_and_log( $params );
214 %{$batch->unblessed},
215 patron => $batch->patron->unblessed,
216 branch => $batch->branch->unblessed,
217 status => $batch->status->unblessed,
218 requests_count => $batch->requests_count
227 $c->unhandled_exception($_);
239 my $c = shift->openapi->valid_input or return;
241 my $batch = Koha::Illbatches->find( $c->validation->param( 'illbatch_id' ) );
243 if ( not defined $batch ) {
244 return $c->render( status => 404, openapi => { error => "ILL batch not found" } );
248 $batch->delete_and_log;
249 return $c->render( status => 204, openapi => '');
252 $c->unhandled_exception($_);