From b651ed4e94b120b221b1bbbf5e34afefb18ab315 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Tue, 29 Oct 2024 10:59:03 +0000 Subject: [PATCH] Bug 38050: create methods for admin CRUD list endpoints This patch implements the methods for the admin CRUD endpoints for lists/virtualshelves Signed-off-by: Martin Renvoize --- Koha/REST/V1/Lists.pm | 143 +++++++++++++++++++++++++++++++++++- api/v1/swagger/swagger.yaml | 4 +- 2 files changed, 141 insertions(+), 6 deletions(-) diff --git a/Koha/REST/V1/Lists.pm b/Koha/REST/V1/Lists.pm index a557627e7e1..8daf4f2575f 100644 --- a/Koha/REST/V1/Lists.pm +++ b/Koha/REST/V1/Lists.pm @@ -16,17 +16,153 @@ package Koha::REST::V1::Lists; # along with Koha; if not, see . use Modern::Perl; - use Mojo::Base 'Mojolicious::Controller'; - use Koha::Virtualshelves; - use Try::Tiny qw( catch try ); +use Data::Dumper; + =head1 API =head2 Methods +=head3 add + +Create a virtual shelf + +=cut + +sub add { + my $c = shift->openapi->valid_input or return; + + return try { + + # Check if owner_id exists if provided + my $body = $c->req->json; + if ( $body->{owner_id} ) { + my $owner = Koha::Patrons->find( $body->{owner_id} ); + unless ($owner) { + return $c->render( + status => 400, + openapi => { + error => "Invalid owner_id", + error_code => "invalid_owner" + } + ); + } + } + + # Set allow_change_from_staff=1 by default unless specified + $body->{allow_change_from_staff} = 1 unless exists $body->{allow_change_from_staff}; + + my $list = Koha::Virtualshelf->new_from_api($body); + $list->store->discard_changes; + + $c->res->headers->location( $c->req->url->to_string . '/' . $list->id ); + + return $c->render( + status => 201, + openapi => $list->to_api + ); + } catch { + $c->unhandled_exception($_); + }; +} + +=head3 update + +Update a virtual shelf + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $list = Koha::Virtualshelves->find( $c->param('list_id') ); + + return $c->render_resource_not_found("List") + unless $list; + + my $user = $c->stash('koha.user'); + + # Check if the list does not belong to the user + if ( $list->owner != $user->id ) { + + # Check if the user is allowed to modify the list + unless ( $list->allow_change_from_others || $list->allow_change_from_staff ) { + return $c->render( + status => 403, + openapi => { + error => "Cannot modify list without proper permissions", + error_code => "forbidden" + } + ); + } + } + + # Check allow_change_from_owner for own lists + if ( $list->owner == $user->id && $list->allow_change_from_owner == 0 ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - list cannot be modified", + error_code => "forbidden" + } + ); + } + + return try { + $list->set_from_api( $c->req->json ); + $list->store(); + + return $c->render( + status => 200, + openapi => $list->to_api + ); + } catch { + $c->unhandled_exception($_); + }; +} + +=head3 delete + +Delete a virtual shelf if it exists + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $list = Koha::Virtualshelves->find( $c->param('list_id') ); + + return $c->render_resource_not_found("List") + unless $list; + + my $user = $c->stash('koha.user'); + + # Check if the list does not belong to the user + if ( $list->owner != $user->id ) { + + # Check if the user is allowed to delete the list + unless ( $list->allow_change_from_others || $list->allow_change_from_staff ) { + return $c->render( + status => 403, + openapi => { + error => "Forbidden - you are not allowed to delete this list", + error_code => "forbidden" + } + ); + } + } + + return try { + $list->delete; + return $c->render_resource_deleted; + } catch { + $c->unhandled_exception($_); + }; +} + =head3 list_public =cut @@ -73,5 +209,4 @@ sub list_public { $c->unhandled_exception($_); }; } - 1; diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 51ed2bb3e9c..307a0059e24 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -447,8 +447,8 @@ paths: $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1desks" /lists: $ref: ./paths/lists.yaml#/~1lists - "/lists/{id}": - $ref: "./paths/lists.yaml#/~1lists~1{id}" + "/lists/{list_id}": + $ref: "./paths/lists.yaml#/~1lists~1{list_id}" "/oauth/login/{provider_code}/{interface}": $ref: ./paths/oauth.yaml#/~1oauth~1login~1{provider_code}~1{interface} /oauth/token: -- 2.48.1