Mojo-Leds
view release on metacpan or search on metacpan
lib/Mojo/Leds/Rest.pm view on Meta::CPAN
my $c = shift;
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro;
my $json = $c->_json_from_body;
return unless ($json);
# json deve essere un array
return $c->_raise_error( 'Not an array of records', 422 )
unless ( ref($json) eq 'ARRAY' );
my @recs = $c->_listupdate($json);
$c->render_json( \@recs );
}
sub patch {
my $c = shift;
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro;
my $json = $c->_json_from_body;
return unless ($json);
my $rec = $c->_patch($json);
return unless ($rec);
$c->render_json( $c->_rec2json($rec) );
}
sub read {
my $c = shift;
$c->render_json( $c->_rec2json );
}
sub render_json {
my $c = shift;
my $json = shift;
my $status = shift || 200;
$c->render( json => $json, status => $status );
}
sub resource_lookup {
my $c = shift;
my $rec = $c->_resource_lookup;
$rec || return $c->_raise_error( 'Element not found', 404 );
$c->stash( $c->_class_name . '::record' => $rec );
return $rec;
}
sub searchDB {
my $c = shift;
my $qry = shift;
my $opt = shift;
my $f_search = $c->f_search;
return $c->tableDB->$f_search( $qry, $opt );
}
sub tableDB {
my $c = shift;
my $helper = $c->dbHelper;
my $f_table = $c->f_table;
return $c->helpers->$helper->$f_table( $c->table );
}
sub update {
my $c = shift;
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro;
my $json = $c->_json_from_body;
return unless ($json);
my $rec = $c->_update($json);
return unless ($rec);
$c->render_json( $c->_rec2json($rec) );
}
sub _class_name {
return ref shift;
}
sub _json_from_body {
my $c = shift;
my $content = $c->req->body;
my $json;
eval { $json = decode_json $content};
if ($@) {
$@ =~ s/\sat\s\/(.*?)\n$//g;
return $c->_raise_error( $@, 400 );
}
return $json;
}
sub _raise_error {
my $c = shift;
my $txt = shift;
my $status = shift || 400;
$c->render_json(
{
status => $status,
message => $txt
},
$status
);
return undef;
}
1;
=pod
=head1 NAME
Mojo::Leds::Rest - Abstract class for RESTFul webservices interface
=head1 VERSION
version 1.18
=head1 RESTFul API
=head2 create
PUT /url/
create a new record
B<Parameters:>
( run in 0.796 second using v1.01-cache-2.11-cpan-ceb78f64989 )