Lines 16-32
package Koha::REST::V1::Lists;
Link Here
|
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
|
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
19 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
|
|
|
22 |
use Koha::Virtualshelves; |
20 |
use Koha::Virtualshelves; |
23 |
|
|
|
24 |
use Try::Tiny qw( catch try ); |
21 |
use Try::Tiny qw( catch try ); |
25 |
|
22 |
|
|
|
23 |
use Data::Dumper; |
24 |
|
26 |
=head1 API |
25 |
=head1 API |
27 |
|
26 |
|
28 |
=head2 Methods |
27 |
=head2 Methods |
29 |
|
28 |
|
|
|
29 |
=head3 add |
30 |
|
31 |
Create a virtual shelf |
32 |
|
33 |
=cut |
34 |
|
35 |
sub add { |
36 |
my $c = shift->openapi->valid_input or return; |
37 |
|
38 |
return try { |
39 |
|
40 |
# Check if owner_id exists if provided |
41 |
my $body = $c->req->json; |
42 |
if ( $body->{owner_id} ) { |
43 |
my $owner = Koha::Patrons->find( $body->{owner_id} ); |
44 |
unless ($owner) { |
45 |
return $c->render( |
46 |
status => 400, |
47 |
openapi => { |
48 |
error => "Invalid owner_id", |
49 |
error_code => "invalid_owner" |
50 |
} |
51 |
); |
52 |
} |
53 |
} |
54 |
|
55 |
# Set allow_change_from_staff=1 by default unless specified |
56 |
$body->{allow_change_from_staff} = 1 unless exists $body->{allow_change_from_staff}; |
57 |
|
58 |
my $list = Koha::Virtualshelf->new_from_api($body); |
59 |
$list->store->discard_changes; |
60 |
|
61 |
$c->res->headers->location( $c->req->url->to_string . '/' . $list->id ); |
62 |
|
63 |
return $c->render( |
64 |
status => 201, |
65 |
openapi => $list->to_api |
66 |
); |
67 |
} catch { |
68 |
$c->unhandled_exception($_); |
69 |
}; |
70 |
} |
71 |
|
72 |
=head3 update |
73 |
|
74 |
Update a virtual shelf |
75 |
|
76 |
=cut |
77 |
|
78 |
sub update { |
79 |
my $c = shift->openapi->valid_input or return; |
80 |
|
81 |
my $list = Koha::Virtualshelves->find( $c->param('list_id') ); |
82 |
|
83 |
return $c->render_resource_not_found("List") |
84 |
unless $list; |
85 |
|
86 |
my $user = $c->stash('koha.user'); |
87 |
|
88 |
# Check if the list does not belong to the user |
89 |
if ( $list->owner != $user->id ) { |
90 |
|
91 |
# Check if the user is allowed to modify the list |
92 |
unless ( $list->allow_change_from_others || $list->allow_change_from_staff ) { |
93 |
return $c->render( |
94 |
status => 403, |
95 |
openapi => { |
96 |
error => "Cannot modify list without proper permissions", |
97 |
error_code => "forbidden" |
98 |
} |
99 |
); |
100 |
} |
101 |
} |
102 |
|
103 |
# Check allow_change_from_owner for own lists |
104 |
if ( $list->owner == $user->id && $list->allow_change_from_owner == 0 ) { |
105 |
return $c->render( |
106 |
status => 403, |
107 |
openapi => { |
108 |
error => "Forbidden - list cannot be modified", |
109 |
error_code => "forbidden" |
110 |
} |
111 |
); |
112 |
} |
113 |
|
114 |
return try { |
115 |
$list->set_from_api( $c->req->json ); |
116 |
$list->store(); |
117 |
|
118 |
return $c->render( |
119 |
status => 200, |
120 |
openapi => $list->to_api |
121 |
); |
122 |
} catch { |
123 |
$c->unhandled_exception($_); |
124 |
}; |
125 |
} |
126 |
|
127 |
=head3 delete |
128 |
|
129 |
Delete a virtual shelf if it exists |
130 |
|
131 |
=cut |
132 |
|
133 |
sub delete { |
134 |
my $c = shift->openapi->valid_input or return; |
135 |
|
136 |
my $list = Koha::Virtualshelves->find( $c->param('list_id') ); |
137 |
|
138 |
return $c->render_resource_not_found("List") |
139 |
unless $list; |
140 |
|
141 |
my $user = $c->stash('koha.user'); |
142 |
|
143 |
# Check if the list does not belong to the user |
144 |
if ( $list->owner != $user->id ) { |
145 |
|
146 |
# Check if the user is allowed to delete the list |
147 |
unless ( $list->allow_change_from_others || $list->allow_change_from_staff ) { |
148 |
return $c->render( |
149 |
status => 403, |
150 |
openapi => { |
151 |
error => "Forbidden - you are not allowed to delete this list", |
152 |
error_code => "forbidden" |
153 |
} |
154 |
); |
155 |
} |
156 |
} |
157 |
|
158 |
return try { |
159 |
$list->delete; |
160 |
return $c->render_resource_deleted; |
161 |
} catch { |
162 |
$c->unhandled_exception($_); |
163 |
}; |
164 |
} |
165 |
|
30 |
=head3 list_public |
166 |
=head3 list_public |
31 |
|
167 |
|
32 |
=cut |
168 |
=cut |
Lines 73-77
sub list_public {
Link Here
|
73 |
$c->unhandled_exception($_); |
209 |
$c->unhandled_exception($_); |
74 |
}; |
210 |
}; |
75 |
} |
211 |
} |
76 |
|
|
|
77 |
1; |
212 |
1; |