Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / Items.pm
1 package Koha::REST::V1::Items;
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
22 use Koha::Items;
23
24 use List::MoreUtils qw( any );
25 use Try::Tiny qw( catch try );
26
27 =head1 NAME
28
29 Koha::REST::V1::Items - Koha REST API for handling items (V1)
30
31 =head1 API
32
33 =head2 Methods
34
35 =cut
36
37 =head3 list
38
39 Controller function that handles listing Koha::Item objects
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47         my $items_set = Koha::Items->new;
48         my $items     = $c->objects->search( $items_set );
49         return $c->render(
50             status  => 200,
51             openapi => $items
52         );
53     }
54     catch {
55         $c->unhandled_exception($_);
56     };
57 }
58
59
60 =head3 get
61
62 Controller function that handles retrieving a single Koha::Item
63
64 =cut
65
66 sub get {
67     my $c = shift->openapi->valid_input or return;
68
69     try {
70         my $item = Koha::Items->find($c->validation->param('item_id'));
71         unless ( $item ) {
72             return $c->render(
73                 status => 404,
74                 openapi => { error => 'Item not found'}
75             );
76         }
77         return $c->render( status => 200, openapi => $item->to_api );
78     }
79     catch {
80         $c->unhandled_exception($_);
81     };
82 }
83
84 =head3 pickup_locations
85
86 Method that returns the possible pickup_locations for a given item
87 used for building the dropdown selector
88
89 =cut
90
91 sub pickup_locations {
92     my $c = shift->openapi->valid_input or return;
93
94     my $item_id = $c->validation->param('item_id');
95     my $item = Koha::Items->find( $item_id );
96
97     unless ($item) {
98         return $c->render(
99             status  => 404,
100             openapi => { error => "Item not found" }
101         );
102     }
103
104     my $patron_id = delete $c->validation->output->{patron_id};
105     my $patron    = Koha::Patrons->find( $patron_id );
106
107     unless ($patron) {
108         return $c->render(
109             status  => 400,
110             openapi => { error => "Patron not found" }
111         );
112     }
113
114     return try {
115
116         my $ps_set = $item->pickup_locations( { patron => $patron } );
117
118         my $pickup_locations = $c->objects->search( $ps_set );
119         my @response = ();
120
121         if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
122
123             my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
124             my $libraries    = $c->objects->search($libraries_rs);
125
126             @response = map {
127                 my $library = $_;
128                 $library->{needs_override} = (
129                     any { $_->{library_id} eq $library->{library_id} }
130                     @{$pickup_locations}
131                   )
132                   ? Mojo::JSON->false
133                   : Mojo::JSON->true;
134                 $library;
135             } @{$libraries};
136
137             return $c->render(
138                 status  => 200,
139                 openapi => \@response
140             );
141         }
142
143         @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
144
145         return $c->render(
146             status  => 200,
147             openapi => \@response
148         );
149     }
150     catch {
151         $c->unhandled_exception($_);
152     };
153 }
154
155 1;