|
Lines 16-32
package Koha::REST::V1::Lists;
Link Here
|
| 16 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
16 |
# along with Koha; if not, see <https://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 |
|
| 26 |
=head1 API |
23 |
=head1 API |
| 27 |
|
24 |
|
| 28 |
=head2 Methods |
25 |
=head2 Methods |
| 29 |
|
26 |
|
|
|
27 |
=head3 add |
| 28 |
|
| 29 |
Create a virtual shelf |
| 30 |
|
| 31 |
=cut |
| 32 |
|
| 33 |
sub add { |
| 34 |
my $c = shift->openapi->valid_input or return; |
| 35 |
|
| 36 |
return try { |
| 37 |
|
| 38 |
# Check if owner_id exists if provided |
| 39 |
my $body = $c->req->json; |
| 40 |
if ( $body->{owner_id} ) { |
| 41 |
my $owner = Koha::Patrons->find( $body->{owner_id} ); |
| 42 |
unless ($owner) { |
| 43 |
return $c->render( |
| 44 |
status => 400, |
| 45 |
openapi => { |
| 46 |
error => "Invalid owner_id", |
| 47 |
error_code => "invalid_owner" |
| 48 |
} |
| 49 |
); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
# Set allow_change_from_staff=1 by default unless specified |
| 54 |
$body->{allow_change_from_staff} = 1 unless exists $body->{allow_change_from_staff}; |
| 55 |
|
| 56 |
my $list = Koha::Virtualshelf->new_from_api($body); |
| 57 |
$list->store->discard_changes; |
| 58 |
|
| 59 |
$c->res->headers->location( $c->req->url->to_string . '/' . $list->id ); |
| 60 |
|
| 61 |
return $c->render( |
| 62 |
status => 201, |
| 63 |
openapi => $list->to_api |
| 64 |
); |
| 65 |
} catch { |
| 66 |
$c->unhandled_exception($_); |
| 67 |
}; |
| 68 |
} |
| 69 |
|
| 70 |
=head3 update |
| 71 |
|
| 72 |
Update a virtual shelf |
| 73 |
|
| 74 |
=cut |
| 75 |
|
| 76 |
sub update { |
| 77 |
my $c = shift->openapi->valid_input or return; |
| 78 |
|
| 79 |
my $list = Koha::Virtualshelves->find( $c->param('list_id') ); |
| 80 |
|
| 81 |
return $c->render_resource_not_found("List") |
| 82 |
unless $list; |
| 83 |
|
| 84 |
my $user = $c->stash('koha.user'); |
| 85 |
|
| 86 |
unless ( $list->can_be_managed( $user->id ) ) { |
| 87 |
return $c->render( |
| 88 |
status => 403, |
| 89 |
openapi => { |
| 90 |
error => "Cannot modify list without proper permissions", |
| 91 |
error_code => "forbidden" |
| 92 |
} |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
return try { |
| 97 |
$list->set_from_api( $c->req->json ); |
| 98 |
$list->store(); |
| 99 |
|
| 100 |
return $c->render( |
| 101 |
status => 200, |
| 102 |
openapi => $list->to_api |
| 103 |
); |
| 104 |
} catch { |
| 105 |
$c->unhandled_exception($_); |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
=head3 delete |
| 110 |
|
| 111 |
Delete a virtual shelf if it exists |
| 112 |
|
| 113 |
=cut |
| 114 |
|
| 115 |
sub delete { |
| 116 |
my $c = shift->openapi->valid_input or return; |
| 117 |
|
| 118 |
my $list = Koha::Virtualshelves->find( $c->param('list_id') ); |
| 119 |
|
| 120 |
return $c->render_resource_not_found("List") |
| 121 |
unless $list; |
| 122 |
|
| 123 |
my $user = $c->stash('koha.user'); |
| 124 |
|
| 125 |
unless ( $list->can_be_deleted( $user->id ) ) { |
| 126 |
return $c->render( |
| 127 |
status => 403, |
| 128 |
openapi => { |
| 129 |
error => "Cannot delete this list", |
| 130 |
error_code => "forbidden" |
| 131 |
} |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
return try { |
| 136 |
$list->delete; |
| 137 |
return $c->render( status => 204, openapi => {} ); |
| 138 |
} catch { |
| 139 |
$c->unhandled_exception($_); |
| 140 |
}; |
| 141 |
} |
| 142 |
|
| 30 |
=head3 list_public |
143 |
=head3 list_public |
| 31 |
|
144 |
|
| 32 |
=cut |
145 |
=cut |
|
Lines 73-77
sub list_public {
Link Here
|
| 73 |
$c->unhandled_exception($_); |
186 |
$c->unhandled_exception($_); |
| 74 |
}; |
187 |
}; |
| 75 |
} |
188 |
} |
| 76 |
|
|
|
| 77 |
1; |
189 |
1; |