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->param('only_mine'); |
40 |
my $only_public = delete $c->param('only_public'); |
41 |
|
42 |
if ( !$user && $only_mine ) { |
43 |
return $c->render( |
44 |
status => 400, |
45 |
openapi => { |
46 |
error => "Bad request - only_mine can only be passed by logged in users", |
47 |
error_code => "only_mine_forbidden", |
48 |
}, |
49 |
); |
50 |
} |
51 |
|
52 |
return try { |
53 |
|
54 |
my $lists_set = Koha::Virtualshelves->new; |
55 |
|
56 |
if ($only_mine) { |
57 |
$lists_set = $lists_set->search( { owner => $user->id } ); |
58 |
} |
59 |
|
60 |
if ( $only_public || !$user ) { |
61 |
$lists_set = $lists_set->filter_by_public; |
62 |
} else { |
63 |
$lists_set = $lists_set->filter_by_readable( { patron_id => $user->id } ); |
64 |
} |
65 |
|
66 |
return $c->render( |
67 |
status => 200, |
68 |
openapi => $c->objects->search($lists_set), |
69 |
); |
70 |
} catch { |
71 |
$c->unhandled_exception($_); |
72 |
}; |
73 |
} |
74 |
|
75 |
1; |