Bug 7317: Interlibrary loans framework for Koha.
[koha.git] / Koha / REST / V1 / Illrequests.pm
1 package Koha::REST::V1::Illrequests;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Illrequests;
23 use Koha::Library;
24
25 sub list {
26     my ($c, $args, $cb) = @_;
27
28     my $filter;
29     $args //= {};
30     my $output = [];
31
32     # Create a hash where all keys are embedded values
33     # Enables easy checking
34     my %embed;
35     if (defined $args->{embed}) {
36         %embed = map { $_ => 1 }  @{$args->{embed}};
37         delete $args->{embed};
38     }
39
40     for my $filter_param ( keys %$args ) {
41         my @values = split(/,/, $args->{$filter_param});
42         $filter->{$filter_param} = \@values;
43     }
44
45     my $requests = Koha::Illrequests->search($filter);
46
47     while (my $request = $requests->next) {
48         my $unblessed = $request->unblessed;
49         # Add the request's id_prefix
50         $unblessed->{id_prefix} = $request->id_prefix;
51         # Augment the request response with patron details
52         # if appropriate
53         if (defined $embed{patron}) {
54             my $patron = $request->patron;
55             $unblessed->{patron} = {
56                 firstname  => $patron->firstname,
57                 surname    => $patron->surname,
58                 cardnumber => $patron->cardnumber
59             };
60         }
61         # Augment the request response with metadata details
62         # if appropriate
63         if (defined $embed{metadata}) {
64             $unblessed->{metadata} = $request->metadata;
65         }
66         # Augment the request response with status details
67         # if appropriate
68         if (defined $embed{capabilities}) {
69             $unblessed->{capabilities} = $request->capabilities;
70         }
71         # Augment the request response with branch details
72         # if appropriate
73         if (defined $embed{branch}) {
74             $unblessed->{branch} = Koha::Libraries->find(
75                 $request->branchcode
76             )->unblessed;
77         }
78         push @{$output}, $unblessed
79     }
80
81     return $c->$cb( $output, 200 );
82
83 }
84
85 1;