|
Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Lists; |
| 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::Virtualshelves; |
| 23 |
|
| 24 |
use Try::Tiny qw( catch try ); |
| 25 |
|
| 26 |
=head1 API |
| 27 |
|
| 28 |
=head2 Methods |
| 29 |
|
| 30 |
=head3 list_public |
| 31 |
|
| 32 |
=cut |
| 33 |
|
| 34 |
sub list_public { |
| 35 |
my $c = shift->openapi->valid_input or return; |
| 36 |
|
| 37 |
my $user = $c->stash('koha.user'); |
| 38 |
|
| 39 |
my $only_mine = delete $c->validation->output->{only_mine}; |
| 40 |
my $only_public = delete $c->validation->output->{only_public}; |
| 41 |
|
| 42 |
return $c->render( |
| 43 |
status => 400, |
| 44 |
openapi => { error => "only_mine and only_public are mutually exclussive" } |
| 45 |
) if $only_mine and $only_public; |
| 46 |
|
| 47 |
if ( !$user and $only_mine ) { |
| 48 |
$c->render( |
| 49 |
status => 200, |
| 50 |
openapi => [], |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return try { |
| 55 |
my $lists_set = Koha::Virtualshelves->new; |
| 56 |
if ( $only_mine ) { |
| 57 |
$lists_set = $lists_set->search({ owner => $user->id }); |
| 58 |
} |
| 59 |
elsif ( $only_public ) { |
| 60 |
$lists_set = $lists_set->filter_by_public; |
| 61 |
} |
| 62 |
else { |
| 63 |
$lists_set = $lists_set->filter_by_readable({ patron_id => $user->id }); |
| 64 |
} |
| 65 |
|
| 66 |
my $lists = $c->objects->search( $lists_set ); |
| 67 |
return $c->render( status => 200, openapi => $lists ); |
| 68 |
} |
| 69 |
catch { |
| 70 |
$c->unhandled_exception($_); |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
1; |