# 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
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($_);
};
}
}
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"
}
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 );
}
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
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
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,
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
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
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
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;