Bugzilla – Attachment 176463 Details for
Bug 38050
Add REST endpoints for working with "lists"/"virtual shelves"
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 38050: create methods for admin CRUD list endpoints
Bug-38050-create-methods-for-admin-CRUD-list-endpo.patch (text/plain), 5.17 KB, created by
Jacob O'Mara
on 2025-01-13 17:51:00 UTC
(
hide
)
Description:
Bug 38050: create methods for admin CRUD list endpoints
Filename:
MIME Type:
Creator:
Jacob O'Mara
Created:
2025-01-13 17:51:00 UTC
Size:
5.17 KB
patch
obsolete
>From 88dd3d6b67a251d669f75f2f360de8d832c56c34 Mon Sep 17 00:00:00 2001 >From: Jacob O'Mara <jacobomara901@gmail.com> >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 | 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 a557627e7e..8daf4f2575 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 <http://www.gnu.org/licenses>. > > 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 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.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 38050
:
174807
|
174808
|
174809
|
174810
|
176462
|
176463
|
176464
|
176465
|
176466
|
176467
|
176769
|
176770
|
176771
|
176772
|
176773
|
176774