Bug 24321: Clean /libraries
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 31 Dec 2019 14:07:37 +0000 (11:07 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 8 Jan 2020 14:42:36 +0000 (14:42 +0000)
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/REST/V1/Libraries.pm [new file with mode: 0644]
Koha/REST/V1/Library.pm [deleted file]
api/v1/swagger/paths/libraries.json
t/db_dependent/api/v1/libraries.t

diff --git a/Koha/REST/V1/Libraries.pm b/Koha/REST/V1/Libraries.pm
new file mode 100644 (file)
index 0000000..8d0b04e
--- /dev/null
@@ -0,0 +1,206 @@
+package Koha::REST::V1::Libraries;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Mojo::Base 'Mojolicious::Controller';
+use Koha::Libraries;
+
+use Scalar::Util qw( blessed );
+
+use Try::Tiny;
+
+=head1 NAME
+
+Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
+
+=head1 API
+
+=head2 Methods
+
+=cut
+
+=head3 list
+
+Controller function that handles listing Koha::Library objects
+
+=cut
+
+sub list {
+    my $c = shift->openapi->valid_input or return;
+
+    return try {
+        my $libraries_set = Koha::Libraries->new;
+        my $libraries     = $c->objects->search( $libraries_set );
+        return $c->render( status => 200, openapi => $libraries );
+    }
+    catch {
+        unless ( blessed $_ && $_->can('rethrow') ) {
+            return $c->render(
+                status  => 500,
+                openapi => { error => "Something went wrong, check Koha logs for details." }
+            );
+        }
+        return $c->render(
+            status  => 500,
+            openapi => { error => "$_" }
+        );
+    };
+}
+
+=head3 get
+
+Controller function that handles retrieving a single Koha::Library
+
+=cut
+
+sub get {
+    my $c = shift->openapi->valid_input or return;
+
+    my $library_id = $c->validation->param('library_id');
+    my $library = Koha::Libraries->find( $library_id );
+
+    unless ($library) {
+        return $c->render( status  => 404,
+                           openapi => { error => "Library not found" } );
+    }
+
+    return $c->render(
+        status  => 200,
+        openapi => $library->to_api
+    );
+}
+
+=head3 add
+
+Controller function that handles adding a new Koha::Library object
+
+=cut
+
+sub add {
+    my $c = shift->openapi->valid_input or return;
+
+    return try {
+        my $library = Koha::Library->new_from_api( $c->validation->param('body') );
+        $library->store;
+        $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
+
+        return $c->render(
+            status  => 201,
+            openapi => $library->to_api
+        );
+    }
+    catch {
+        unless ( blessed $_ && $_->can('rethrow') ) {
+            return $c->render(
+                status  => 500,
+                openapi => { error => "Something went wrong, check Koha logs for details." }
+            );
+        }
+        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
+            return $c->render(
+                status  => 409,
+                openapi => { error => $_->error, conflict => $_->duplicate_id }
+            );
+        }
+        else {
+            return $c->render(
+                status  => 500,
+                openapi => { error => "$_" }
+            );
+        }
+    };
+}
+
+=head3 update
+
+Controller function that handles updating a Koha::Library object
+
+=cut
+
+sub update {
+    my $c = shift->openapi->valid_input or return;
+
+    my $library = Koha::Libraries->find( $c->validation->param('library_id') );
+
+    if ( not defined $library ) {
+        return $c->render(
+            status  => 404,
+            openapi => { error => "Library not found" }
+        );
+    }
+
+    return try {
+        my $params = $c->req->json;
+        $library->set_from_api( $params );
+        $library->store();
+        return $c->render(
+            status  => 200,
+            openapi => $library->to_api
+        );
+    }
+    catch {
+        unless ( blessed $_ && $_->can('rethrow') ) {
+            return $c->render(
+                status  => 500,
+                openapi => { error => "Something went wrong, check Koha logs for details." }
+            );
+        }
+
+        return $c->render(
+            status  => 500,
+            openapi => { error => "$_" }
+        );
+    };
+}
+
+=head3 delete
+
+Controller function that handles deleting a Koha::Library object
+
+=cut
+
+sub delete {
+
+    my $c = shift->openapi->valid_input or return;
+
+    my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
+
+    if ( not defined $library ) {
+        return $c->render( status => 404, openapi => { error => "Library not found" } );
+    }
+
+    return try {
+        $library->delete;
+        return $c->render( status => 204, openapi => '');
+    }
+    catch {
+        unless ( blessed $_ && $_->can('rethrow') ) {
+            return $c->render(
+                status  => 500,
+                openapi => { error => "Something went wrong, check Koha logs for details." }
+            );
+        }
+
+        return $c->render(
+            status  => 500,
+            openapi => { error => "$_" }
+        );
+    };
+}
+
+1;
diff --git a/Koha/REST/V1/Library.pm b/Koha/REST/V1/Library.pm
deleted file mode 100644 (file)
index a40e049..0000000
+++ /dev/null
@@ -1,324 +0,0 @@
-package Koha::REST::V1::Library;
-
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
-#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-use Modern::Perl;
-
-use Mojo::Base 'Mojolicious::Controller';
-use Koha::Libraries;
-
-use Scalar::Util qw( blessed );
-
-use Try::Tiny;
-
-=head1 NAME
-
-Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
-
-=head1 API
-
-=head2 Methods
-
-=cut
-
-=head3 list
-
-Controller function that handles listing Koha::Library objects
-
-=cut
-
-sub list {
-    my $c = shift->openapi->valid_input or return;
-
-    return try {
-        my $libraries_set = Koha::Libraries->new;
-        my $libraries     = $c->objects->search( $libraries_set, \&_to_model, \&_to_api );
-        return $c->render( status => 200, openapi => $libraries );
-    }
-    catch {
-        unless ( blessed $_ && $_->can('rethrow') ) {
-            return $c->render(
-                status  => 500,
-                openapi => { error => "Something went wrong, check Koha logs for details." }
-            );
-        }
-        return $c->render(
-            status  => 500,
-            openapi => { error => "$_" }
-        );
-    };
-}
-
-=head3 get
-
-Controller function that handles retrieving a single Koha::Library
-
-=cut
-
-sub get {
-    my $c = shift->openapi->valid_input or return;
-
-    my $library_id = $c->validation->param('library_id');
-    my $library = Koha::Libraries->find( $library_id );
-
-    unless ($library) {
-        return $c->render( status  => 404,
-                           openapi => { error => "Library not found" } );
-    }
-
-    return $c->render(
-        status  => 200,
-        openapi => $library->to_api
-    );
-}
-
-=head3 add
-
-Controller function that handles adding a new Koha::Library object
-
-=cut
-
-sub add {
-    my $c = shift->openapi->valid_input or return;
-
-    return try {
-        my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) );
-        $library->store;
-        $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
-
-        return $c->render(
-            status  => 201,
-            openapi => $library->to_api
-        );
-    }
-    catch {
-        unless ( blessed $_ && $_->can('rethrow') ) {
-            return $c->render(
-                status  => 500,
-                openapi => { error => "Something went wrong, check Koha logs for details." }
-            );
-        }
-        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
-            return $c->render(
-                status  => 409,
-                openapi => { error => $_->error, conflict => $_->duplicate_id }
-            );
-        }
-        else {
-            return $c->render(
-                status  => 500,
-                openapi => { error => "$_" }
-            );
-        }
-    };
-}
-
-=head3 update
-
-Controller function that handles updating a Koha::Library object
-
-=cut
-
-sub update {
-    my $c = shift->openapi->valid_input or return;
-
-    my $library = Koha::Libraries->find( $c->validation->param('library_id') );
-
-    if ( not defined $library ) {
-        return $c->render(
-            status  => 404,
-            openapi => { error => "Library not found" }
-        );
-    }
-
-    return try {
-        my $params = $c->req->json;
-        $library->set( _to_model($params) );
-        $library->store();
-        return $c->render(
-            status  => 200,
-            openapi => $library->to_api
-        );
-    }
-    catch {
-        unless ( blessed $_ && $_->can('rethrow') ) {
-            return $c->render(
-                status  => 500,
-                openapi => { error => "Something went wrong, check Koha logs for details." }
-            );
-        }
-
-        return $c->render(
-            status  => 500,
-            openapi => { error => "$_" }
-        );
-    };
-}
-
-=head3 delete
-
-Controller function that handles deleting a Koha::Library object
-
-=cut
-
-sub delete {
-
-    my $c = shift->openapi->valid_input or return;
-
-    my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
-
-    if ( not defined $library ) {
-        return $c->render( status => 404, openapi => { error => "Library not found" } );
-    }
-
-    return try {
-        $library->delete;
-        return $c->render( status => 204, openapi => '');
-    }
-    catch {
-        unless ( blessed $_ && $_->can('rethrow') ) {
-            return $c->render(
-                status  => 500,
-                openapi => { error => "Something went wrong, check Koha logs for details." }
-            );
-        }
-
-        return $c->render(
-            status  => 500,
-            openapi => { error => "$_" }
-        );
-    };
-}
-
-=head3 _to_api
-
-Helper function that maps a hashref of Koha::Library attributes into REST api
-attribute names.
-
-=cut
-
-sub _to_api {
-    my $library = shift;
-
-    # Rename attributes
-    foreach my $column ( keys %{ $Koha::REST::V1::Library::to_api_mapping } ) {
-        my $mapped_column = $Koha::REST::V1::Library::to_api_mapping->{$column};
-        if (    exists $library->{ $column }
-             && defined $mapped_column )
-        {
-            # key /= undef
-            $library->{ $mapped_column } = delete $library->{ $column };
-        }
-        elsif (    exists $library->{ $column }
-                && !defined $mapped_column )
-        {
-            # key == undef => to be deleted
-            delete $library->{ $column };
-        }
-    }
-
-    return $library;
-}
-
-=head3 _to_model
-
-Helper function that maps REST api objects into Koha::Library
-attribute names.
-
-=cut
-
-sub _to_model {
-    my $library = shift;
-
-    foreach my $attribute ( keys %{ $Koha::REST::V1::Library::to_model_mapping } ) {
-        my $mapped_attribute = $Koha::REST::V1::Library::to_model_mapping->{$attribute};
-        if (    exists $library->{ $attribute }
-             && defined $mapped_attribute )
-        {
-            # key /= undef
-            $library->{ $mapped_attribute } = delete $library->{ $attribute };
-        }
-        elsif (    exists $library->{ $attribute }
-                && !defined $mapped_attribute )
-        {
-            # key == undef => to be deleted
-            delete $library->{ $attribute };
-        }
-    }
-
-    if ( exists $library->{pickup_location} ) {
-        $library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0;
-    }
-
-    return $library;
-}
-
-
-=head2 Global variables
-
-=head3 $to_api_mapping
-
-=cut
-
-our $to_api_mapping = {
-    branchcode       => 'library_id',
-    branchname       => 'name',
-    branchaddress1   => 'address1',
-    branchaddress2   => 'address2',
-    branchaddress3   => 'address3',
-    branchzip        => 'postal_code',
-    branchcity       => 'city',
-    branchstate      => 'state',
-    branchcountry    => 'country',
-    branchphone      => 'phone',
-    branchfax        => 'fax',
-    branchemail      => 'email',
-    branchreplyto    => 'reply_to_email',
-    branchreturnpath => 'return_path_email',
-    branchurl        => 'url',
-    issuing          => undef,
-    branchip         => 'ip',
-    branchprinter    => undef,
-    branchnotes      => 'notes',
-    marcorgcode      => 'marc_org_code',
-};
-
-=head3 $to_model_mapping
-
-=cut
-
-our $to_model_mapping = {
-    library_id        => 'branchcode',
-    name              => 'branchname',
-    address1          => 'branchaddress1',
-    address2          => 'branchaddress2',
-    address3          => 'branchaddress3',
-    postal_code       => 'branchzip',
-    city              => 'branchcity',
-    state             => 'branchstate',
-    country           => 'branchcountry',
-    phone             => 'branchphone',
-    fax               => 'branchfax',
-    email             => 'branchemail',
-    reply_to_email    => 'branchreplyto',
-    return_path_email => 'branchreturnpath',
-    url               => 'branchurl',
-    ip                => 'branchip',
-    notes             => 'branchnotes',
-    marc_org_code     => 'marcorgcode',
-};
-
-1;
index c4cd55b..1110e73 100644 (file)
@@ -1,8 +1,8 @@
 {
   "/libraries": {
     "get": {
-      "x-mojo-to": "Library#list",
-      "operationId": "listLibrary",
+      "x-mojo-to": "Libraries#list",
+      "operationId": "listLibraries",
       "tags": [
         "library"
       ],
           "description": "Case insensitive 'starts-with' search on OPAC info",
           "required": false,
           "type": "string"
+        },
+        {
+          "$ref": "../parameters.json#/match"
+        },
+        {
+          "$ref": "../parameters.json#/order_by"
+        },
+        {
+          "$ref": "../parameters.json#/page"
+        },
+        {
+          "$ref": "../parameters.json#/per_page"
         }
       ],
       "produces": [
       }
     },
     "post": {
-      "x-mojo-to": "Library#add",
+      "x-mojo-to": "Libraries#add",
       "operationId": "addLibrary",
       "tags": [
         "library"
   },
   "/libraries/{library_id}": {
     "get": {
-      "x-mojo-to": "Library#get",
+      "x-mojo-to": "Libraries#get",
       "operationId": "getLibrary",
       "tags": [
         "library"
       }
     },
     "put": {
-      "x-mojo-to": "Library#update",
+      "x-mojo-to": "Libraries#update",
       "operationId": "updateLibrary",
       "tags": [
         "library"
       }
     },
     "delete": {
-      "x-mojo-to": "Library#delete",
+      "x-mojo-to": "Libraries#delete",
       "operationId": "deleteLibrary",
       "tags": [
         "library"
index 5ac8ecc..8cdac30 100644 (file)
@@ -121,7 +121,7 @@ subtest 'get() tests' => sub {
 
     $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
       ->status_is( 200, 'SWAGGER3.2.2' )
-      ->json_is( '' => Koha::REST::V1::Library::_to_api( $library->TO_JSON ), 'SWAGGER3.3.2' );
+      ->json_is( '' => $library->to_api, 'SWAGGER3.3.2' );
 
     my $non_existent_code = $library->branchcode;
     $library->delete;
@@ -155,7 +155,7 @@ subtest 'add() tests' => sub {
     my $unauth_userid = $unauthorized_patron->userid;
 
     my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
-    my $library     = Koha::REST::V1::Library::_to_api( $library_obj->TO_JSON );
+    my $library     = $library_obj->to_api;
     $library_obj->delete;
 
     # Unauthorized attempt to write
@@ -245,7 +245,7 @@ subtest 'update() tests' => sub {
       );
 
     my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
-    my $library_with_updated_field = Koha::REST::V1::Library::_to_api( $deleted_library->TO_JSON );
+    my $library_with_updated_field = $deleted_library->to_api;
     $library_with_updated_field->{library_id} = $library_id;
     $deleted_library->delete;