Skip to content

Instantly share code, notes, and snippets.

@rkitover
Created December 14, 2012 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkitover/4286776 to your computer and use it in GitHub Desktop.
Save rkitover/4286776 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
use FindBin '$Bin';
use lib "$Bin/perl";
use EMS::DB '$db';
plugin 'EMS::Session';
under '/0.1' => sub { # API version
my $self = shift;
if (not $self->session) {
$self->res->code(400);
$self->respond_to(json => { json => {
error_message => 'you must be logged in to use the API',
}});
return 0;
}
# $self->app->log->debug($self->dumper($self->req->headers));
return 1;
};
get '/work_order_categories/*type' => {type => ''} => sub {
my $self = shift;
my $cat_type = $self->param('type');
$cat_type = undef if $cat_type eq '';
if (defined $cat_type && $cat_type !~ /^(?:r|p)\z/) {
$self->res->code(400);
$self->respond_to(json => { json => {
error_message => q|cat_type must be 'r' for REO or 'p' for P&P|,
}});
return 0;
}
my @data = $db->run(sub {
my $categories = $_->prepare_cached(<<'EOF');
SELECT id, category_name, type
FROM fsm_wo_category
WHERE status = 'A' AND type = IFNULL(?, type)
ORDER BY category_name ASC
EOF
map +{ category => $_ }, @{ $_->selectall_arrayref($categories, { Slice => {} }, $cat_type) };
});
my %data = (categories => \@data);
$self->respond_to(json => { json => \%data });
};
get '/work_order_types/*cat_id' => {cat_id => ''} => sub {
my $self = shift;
my $cat_id = $self->param('cat_id');
$cat_id = undef if $cat_id eq '';
if (defined $cat_id && $cat_id !~ /^\d+\z/) {
$self->res->code(400);
$self->respond_to(json => { json => {
error_message => 'cat_id must be an integer',
}});
return 0;
}
my $system = $self->param('system');
$system = (not defined $system) ? 'n' : $system eq '*' ? undef : $system;
my @data = $db->run(sub {
my $wo_types = $_->prepare_cached(<<'EOF');
SELECT wt.id id, wt.name name, wc.id category_id, wc.category_name, wc.type category_type
FROM fsm_wo_type wt
LEFT JOIN category_workorder_mapping cm
ON cm.workorder_type_id = wt.id
LEFT JOIN fsm_wo_category wc
ON wc.id = cm.category_id
WHERE cm.category_id <=> IFNULL(?, cm.category_id)
AND wt.system = IFNULL(?, wt.system)
ORDER BY wt.name ASC
EOF
map +{ work_order_type => $_ }, @{ $_->selectall_arrayref($wo_types, { Slice => {} }, $cat_id, $system) };
});
my %data = (work_order_types => \@data);
$self->respond_to(json => { json => \%data });
};
app->secret('ems_api_3!13'); # passphrase
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment