1 package Koha::REST::V1::Items;
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';
24 use List::MoreUtils qw( any );
25 use Try::Tiny qw( catch try );
29 Koha::REST::V1::Items - Koha REST API for handling items (V1)
39 Controller function that handles listing Koha::Item objects
44 my $c = shift->openapi->valid_input or return;
47 my $items_set = Koha::Items->new;
48 my $items = $c->objects->search( $items_set );
55 $c->unhandled_exception($_);
62 Controller function that handles retrieving a single Koha::Item
67 my $c = shift->openapi->valid_input or return;
70 my $item = Koha::Items->find($c->validation->param('item_id'));
74 openapi => { error => 'Item not found'}
77 return $c->render( status => 200, openapi => $item->to_api );
80 $c->unhandled_exception($_);
84 =head3 pickup_locations
86 Method that returns the possible pickup_locations for a given item
87 used for building the dropdown selector
91 sub pickup_locations {
92 my $c = shift->openapi->valid_input or return;
94 my $item_id = $c->validation->param('item_id');
95 my $item = Koha::Items->find( $item_id );
100 openapi => { error => "Item not found" }
104 my $patron_id = delete $c->validation->output->{patron_id};
105 my $patron = Koha::Patrons->find( $patron_id );
110 openapi => { error => "Patron not found" }
116 my $ps_set = $item->pickup_locations( { patron => $patron } );
118 my $pickup_locations = $c->objects->search( $ps_set );
121 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
123 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
124 my $libraries = $c->objects->search($libraries_rs);
128 $library->{needs_override} = (
129 any { $_->{library_id} eq $library->{library_id} }
139 openapi => \@response
143 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
147 openapi => \@response
151 $c->unhandled_exception($_);