Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / AdvancedEditorMacro.pm
index 6e0f04d..472d4ce 100644 (file)
@@ -11,22 +11,23 @@ package Koha::REST::V1::AdvancedEditorMacro;
 # 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.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
 use Mojo::Base 'Mojolicious::Controller';
 use Koha::AdvancedEditorMacros;
 
-use Try::Tiny;
+use Try::Tiny qw( catch try );
 
-=head1 API
+=head1 Name
 
-=head2 Class Methods
+Koha::REST::V1::AdvancedEditorMacro
 
-=cut
+=head1 API
+
+=head2 Methods
 
 =head3 list
 
@@ -38,19 +39,20 @@ sub list {
     my $c = shift->openapi->valid_input or return;
     my $patron = $c->stash('koha.user');
     return try {
-        my $macros_set = Koha::AdvancedEditorMacros->search({ -or => { shared => 1, borrowernumber => $patron->borrowernumber } });
-        my $macros = $c->objects->search( $macros_set, \&_to_model, \&_to_api );
-        return $c->render( status => 200, openapi => $macros );
+        my $macros_set = Koha::AdvancedEditorMacros->search(
+            {
+                -or =>
+                  { shared => 1, borrowernumber => $patron->borrowernumber }
+            }
+        );
+        my $macros = $c->objects->search( $macros_set );
+        return $c->render(
+            status  => 200,
+            openapi => $macros
+        );
     }
     catch {
-        if ( $_->isa('DBIx::Class::Exception') ) {
-            return $c->render( status  => 500,
-                               openapi => { error => $_->{msg} } );
-        }
-        else {
-            return $c->render( status => 500,
-                openapi => { error => "Something went wrong, check the logs. $_"} );
-        }
+        $c->unhandled_exception($_);
     };
 
 }
@@ -73,11 +75,9 @@ sub get {
     }
     if( $macro->shared ){
         return $c->render( status => 403, openapi => {
-            error => "This macro is shared, you must access it via advancededitormacros/shared"
+            error => "This macro is shared, you must access it via advanced_editor/macros/shared"
         });
     }
-    warn $macro->borrowernumber;
-    warn $patron->borrowernumber;
     if( $macro->borrowernumber != $patron->borrowernumber ){
         return $c->render( status => 403, openapi => {
             error => "You do not have permission to access this macro"
@@ -105,7 +105,7 @@ sub get_shared {
     }
     unless( $macro->shared ){
         return $c->render( status => 403, openapi => {
-            error => "This macro is not shared, you must access it via advancededitormacros"
+            error => "This macro is not shared, you must access it via advanced_editor/macros"
         });
     }
     return $c->render( status => 200, openapi => $macro->to_api );
@@ -126,15 +126,17 @@ sub add {
     }
 
     return try {
-        my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) );
-        $macro->store;
+        my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') );
+        $macro->store->discard_changes;
         $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
         return $c->render(
             status  => 201,
             openapi => $macro->to_api
         );
     }
-    catch { handle_error($_) };
+    catch {
+        $c->unhandled_exception($_);
+    };
 }
 
 =head3 add_shared
@@ -151,15 +153,17 @@ sub add_shared {
                            openapi => { error => "To create private macros you must use advancededitor" } );
     }
     return try {
-        my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) );
-        $macro->store;
+        my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') );
+        $macro->store->discard_changes;
         $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
         return $c->render(
             status  => 201,
             openapi => $macro->to_api
         );
     }
-    catch { handle_error($_) };
+    catch {
+        $c->unhandled_exception($_);
+    };
 }
 
 =head3 update
@@ -181,7 +185,7 @@ sub update {
 
     if( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
         return $c->render( status  => 403,
-                           openapi => { error => "To update a macro as shared you must use the advancededitormacros/shared endpoint" } );
+                           openapi => { error => "To update a macro as shared you must use the advanced_editor/macros/shared endpoint" } );
     } else {
         unless ( $macro->borrowernumber == $patron->borrowernumber ){
             return $c->render( status  => 403,
@@ -191,11 +195,13 @@ sub update {
 
     return try {
         my $params = $c->req->json;
-        $macro->set( _to_model($params) );
-        $macro->store();
+        $macro->set_from_api( $params );
+        $macro->store->discard_changes;
         return $c->render( status => 200, openapi => $macro->to_api );
     }
-    catch { handle_error($_) };
+    catch {
+        $c->unhandled_exception($_);
+    };
 }
 
 =head3 update_shared
@@ -221,11 +227,13 @@ sub update_shared {
 
     return try {
         my $params = $c->req->json;
-        $macro->set( _to_model($params) );
-        $macro->store();
+        $macro->set_from_api( $params );
+        $macro->store->discard_changes;
         return $c->render( status => 200, openapi => $macro->to_api );
     }
-    catch { handle_error($_) };
+    catch {
+        $c->unhandled_exception($_);
+    };
 }
 
 =head3 delete
@@ -256,9 +264,11 @@ sub delete {
 
     return try {
         $macro->delete;
-        return $c->render( status => 200, openapi => "" );
+        return $c->render( status => 204, openapi => q{} );
     }
-    catch { handle_error($_) };
+    catch {
+        $c->unhandled_exception($_);
+    };
 }
 
 =head3 delete_shared
@@ -283,114 +293,11 @@ sub delete_shared {
 
     return try {
         $macro->delete;
-        return $c->render( status => 200, openapi => "" );
-    }
-    catch { handle_error($_,$c) };
-}
-
-=head3 _handle_error
-
-Helper function that passes exception or error
-
-=cut
-
-sub _handle_error {
-    my ($err,$c) = @_;
-    if ( $err->isa('DBIx::Class::Exception') ) {
-        return $c->render( status  => 500,
-                           openapi => { error => $err->{msg} } );
-    }
-    else {
-        return $c->render( status => 500,
-            openapi => { error => "Something went wrong, check the logs."} );
-    }
-};
-
-
-=head3 _to_api
-
-Helper function that maps a hashref of Koha::AdvancedEditorMacro attributes into REST api
-attribute names.
-
-=cut
-
-sub _to_api {
-    my $macro = shift;
-
-    # Rename attributes
-    foreach my $column ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping } ) {
-        my $mapped_column = $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping->{$column};
-        if (    exists $macro->{ $column }
-             && defined $mapped_column )
-        {
-            # key /= undef
-            $macro->{ $mapped_column } = delete $macro->{ $column };
-        }
-        elsif (    exists $macro->{ $column }
-                && !defined $mapped_column )
-        {
-            # key == undef => to be deleted
-            delete $macro->{ $column };
-        }
-    }
-
-    return $macro;
-}
-
-=head3 _to_model
-
-Helper function that maps REST api objects into Koha::AdvancedEditorMacros
-attribute names.
-
-=cut
-
-sub _to_model {
-    my $macro = shift;
-
-    foreach my $attribute ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_model_mapping } ) {
-        my $mapped_attribute = $Koha::REST::V1::AdvancedEditorMacro::to_model_mapping->{$attribute};
-        if (    exists $macro->{ $attribute }
-             && defined $mapped_attribute )
-        {
-            # key /= undef
-            $macro->{ $mapped_attribute } = delete $macro->{ $attribute };
-        }
-        elsif (    exists $macro->{ $attribute }
-                && !defined $mapped_attribute )
-        {
-            # key == undef => to be deleted
-            delete $macro->{ $attribute };
-        }
+        return $c->render( status => 204, openapi => q{} );
     }
-
-    if ( exists $macro->{shared} ) {
-        $macro->{shared} = ($macro->{shared}) ? 1 : 0;
-    }
-
-
-    return $macro;
+    catch {
+        $c->unhandled_exception($_);
+    };
 }
 
-=head2 Global variables
-
-=head3 $to_api_mapping
-
-=cut
-
-our $to_api_mapping = {
-    id                  => 'macro_id',
-    macro               => 'macro_text',
-    borrowernumber      => 'patron_id',
-};
-
-=head3 $to_model_mapping
-
-=cut
-
-our $to_model_mapping = {
-    macro_id         => 'id',
-    macro_text       => 'macro',
-    patron_id        => 'borrowernumber',
-};
-
 1;