App-Dochazka-REST
view release on metacpan or search on metacpan
lib/App/Dochazka/REST/Dispatch.pm view on Meta::CPAN
=head3 handler_put_activity_code
Handler for the 'PUT activity/code/:code' resource.
=cut
sub handler_put_activity_code {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_put_activity_code" );
my $context = $self->context;
# first pass
return 1 if ( $pass == 1 );
# second pass
# - create or modify?
my $code = $context->{'mapping'}->{'code'};
my $entity = $context->{'request_entity'};
if ( ! defined($entity) ) {
$self->mrest_declare_status( 'code' => 400, 'explanation' => 'Missing request entity' );
return $fail;
}
my $act = shared_first_pass_lookup( $self, 'code', $code );
$self->nullify_declared_status;
# - perform insert/update operation
if ( $act ) {
return shared_update_activity( $self, $act, $entity );
} else {
return shared_insert_activity( $self, $code, $entity );
}
}
=head2 Component handlers
=head3 handler_get_component_all
Handler for 'GET component/all'
=cut
sub handler_get_component_all {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_get_component_all" );
# first pass
return 1 if $pass == 1 ;
# second pass
return App::Dochazka::REST::Model::Component::get_all_components(
$self->context->{'dbix_conn'},
);
}
=head3 handler_post_component_cid
Handler for 'POST component/cid' resource.
=cut
sub handler_post_component_cid {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_post_component_cid" );
# first pass
return 1 if $pass == 1;
# second pass
# - check that entity is kosher
my $status = shared_entity_check( $self, 'cid' );
return $status unless $status->ok;
my $context = $self->context;
my $entity = $context->{'request_entity'};
# - get cid and look it up
my $cid = $entity->{'cid'};
my $comp = shared_first_pass_lookup( $self, 'CID', $cid );
return $fail unless $cid;
# - perform the update
return shared_update_component( $self, $comp, $entity );
}
=head3 handler_post_component_path
Handler for 'POST component/path' resource. This is a little more complicated
because it can be either create or modify.
=cut
sub handler_post_component_path {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_post_component_path" );
# first pass
return 1 if $pass == 1;
# second pass
# - check that entity is kosher
my $status = shared_entity_check( $self, 'path' );
return $status unless $status->ok;
my $context = $self->context;
my $entity = $context->{'request_entity'};
# - create or modify?
my $path = $entity->{'path'};
my $comp = shared_first_pass_lookup( $self, 'path', $path );
$self->nullify_declared_status;
# - perform the insert/update
if ( $comp ) {
return shared_update_component( $self, $comp, $entity );
} else {
my $status = shared_entity_check( $self, 'path', 'source', 'acl' );
return $status unless $status->ok;
return shared_insert_component( $self, $path, $entity );
}
}
=head3 handler_component_cid
Handler for the 'component/cid/:cid' resource.
=cut
sub handler_component_cid {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_component_cid" );
my $context = $self->context;
# first pass
if ( $pass == 1 ) {
my $comp = shared_first_pass_lookup( $self, 'cid', $context->{'mapping'}->{'cid'} );
return 0 unless $comp;
$context->{'stashed_component_object'} = $comp;
return 1;
}
# second pass
if ( $context->{'method'} eq 'GET' ) {
return $CELL->status_ok( 'DISPATCH_COMPONENT_FOUND',
payload => $context->{'stashed_component_object'}
);
} elsif ( $context->{'method'} eq 'PUT' ) {
return shared_update_component(
$self,
$context->{'stashed_component_object'},
$context->{'request_entity'}
);
} elsif ( $context->{'method'} eq 'DELETE' ) {
return $context->{'stashed_component_object'}->delete( $context );
}
return $CELL->status_crit("Aaaabllaaaaaaahhh Component! Swallowed by the abyss" );
}
=head2 Employee handlers
=head3 handler_get_employee_count
Handler for 'GET employee/count/?:priv' resource.
=cut
sub handler_get_employee_count {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_get_employee_count" );
# first pass
return 1 if $pass == 1;
# second pass
my $result;
if ( my $priv = $self->context->{'mapping'}->{'priv'} ) {
$result = noof_employees_by_priv( $self->context->{'dbix_conn'}, lc $priv );
} else {
$result = noof_employees_by_priv( $self->context->{'dbix_conn'}, 'total' );
}
return $result;
}
=head3 handler_get_employee_list
Handler for 'GET employee/list/?:priv' resource.
=cut
sub handler_get_employee_list {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_get_employee_list" );
( run in 0.491 second using v1.01-cache-2.11-cpan-39bf76dae61 )