From f012bae2ed8b4d4ab16e09e908db6e33ed48ce08 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Tue, 29 Oct 2024 11:00:36 +0000 Subject: [PATCH] Bug 38050: create methods for public CRUD list endpoints This patch creates the methods for the public CRUD enpoints for lists/virtualshelves --- Koha/REST/V1/Lists.pm | 146 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/Koha/REST/V1/Lists.pm b/Koha/REST/V1/Lists.pm index 2fef438abc..8f1126252d 100644 --- a/Koha/REST/V1/Lists.pm +++ b/Koha/REST/V1/Lists.pm @@ -132,6 +132,152 @@ sub delete { }; } +=head3 public_create + +Create a public virtual shelf + +=cut + +sub public_create { + my $c = shift->openapi->valid_input or return; + + my $user = $c->stash('koha.user'); + my $json_body = $c->req->json; + + $json_body->{owner} = $user->id; + + return try { + + my $list = Koha::Virtualshelf->new_from_api($json_body); + $list->store->discard_changes; + $c->res->headers->location( $c->req->url->to_string . '/' . $list->id ); + return $c->render( + status => 201, + openapi => $c->objects->to_api($list), + ); + } catch { + $c->unhandled_exception($_); + }; +} + +=head3 public_read + +List the contents of a public virtual shelf or a virtual shelf you own + +=cut + +sub public_read { + my $c = shift->openapi->valid_input or return; + my $user = $c->stash('koha.user'); + + my $list = Koha::Virtualshelves->find( $c->param('list_id') ); + + #if the list owner != to the user id, return 403 + unless ( $list->owner == $user->id || $list->public == 1 ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - you can only view your own lists or lists that are public.", + error_code => "forbidden", + }, + ); + } + return $c->render_resource_not_found("List") + unless $list; + + return $c->render( status => 200, openapi => $c->objects->to_api($list), ); +} + +=head3 public_update + +Update a public virtual shelf or a shelf you own + +=cut + +sub public_update { + my $c = shift->openapi->valid_input or return; + my $user = $c->stash('koha.user'); + + my $list = Koha::Virtualshelves->find( $c->param('list_id') ); + + #if the list owner != to the user id, return 403 + if ( $list->owner != $user->id ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - you can only update your own lists", + error_code => "forbidden", + }, + ); + } + + #if the allow_change_from_owner is false, return 403 + if ( $list->allow_change_from_owner == 0 ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - you can't update this list", + error_code => "forbidden", + }, + ); + } + + return $c->render_resource_not_found("List") + unless $list; + + return try { + $list->set_from_api( $c->req->json ); + $list->store(); + return $c->render( status => 200, openapi => $c->objects->to_api($list), ); + } catch { + $c->unhandled_exception($_); + }; +} + +=head3 public_delete + +Delete a public virtual shelf you own + +=cut + +sub public_delete { + my $c = shift->openapi->valid_input or return; + my $user = $c->stash('koha.user'); + + my $list = Koha::Virtualshelves->find( $c->param('list_id') ); + return $c->render_resource_not_found("List") + unless $list; + + #if the list owner != to the user id, return 403 + if ( $list->owner != $user->id ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - you can only update your own lists", + error_code => "forbidden", + }, + ); + } + + #if the allow_change_from_owner is false, return 403 + if ( $list->allow_change_from_owner == 0 ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - you can't update this list", + error_code => "forbidden", + }, + ); + } + + return try { + $list->delete; + return $c->render_resource_deleted; + } catch { + $c->unhandled_exception($_); + }; +} + =head3 list_public =cut -- 2.39.2