From a75fee0ae5aadb7565d8d6ef23ded43121c5aa7e 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 --- Koha/REST/V1/Lists.pm | 112 ++++++++++++++++++++++++++++++++++-- api/v1/swagger/swagger.yaml | 4 +- 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/Koha/REST/V1/Lists.pm b/Koha/REST/V1/Lists.pm index a557627e7e..2fef438abc 100644 --- a/Koha/REST/V1/Lists.pm +++ b/Koha/REST/V1/Lists.pm @@ -16,17 +16,122 @@ 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 list + +List all virtual shelves + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $lists = $c->objects->search( Koha::Virtualshelves->new ); + return $c->render( status => 200, openapi => $lists ); + } catch { + $c->unhandled_exception($_); + }; + +} + +=head3 read + +List the contents of a virtual shelf + +=cut + +sub read { + my $c = shift->openapi->valid_input or return; + + return try { + my $list_id = Koha::Virtualshelves->find( $c->param('list_id') ); + return $c->render_resource_not_found("list_id") + unless $list_id; + + return $c->render( status => 200, openapi => $c->objects->to_api($list_id), ); + } catch { + $c->unhandled_exception($_); + }; +} + +=head3 create + +Create a virtual shelf + +=cut + +sub create { + my $c = shift->openapi->valid_input or return; + + return try { + + my $list = Koha::Virtualshelf->new_from_api( $c->req->json ); + $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 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; + + 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 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; + + return try { + $list->delete; + return $c->render_resource_deleted; + } catch { + $c->unhandled_exception($_); + }; +} + =head3 list_public =cut @@ -73,5 +178,4 @@ sub list_public { $c->unhandled_exception($_); }; } - 1; diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 51ed2bb3e9..307a0059e2 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.39.2