clean
[sgn.git] / lib / SGN / Controller / AJAX / People.pm
blob26c5c38cab2543ee5b68d0d941f7619fa694bc44
2 =head1 NAME
4 SGN::Controller::AJAX::People - a REST controller class to provide the
5 backend for the sgn_people schema
7 =head1 DESCRIPTION
9 REST interface for searching people, getting user data, etc.
11 =head1 AUTHOR
13 Naama Menda <nm249@cornell.edu>
16 =cut
18 package SGN::Controller::AJAX::People;
20 use Moose;
22 use Data::Dumper;
23 use List::MoreUtils qw /any /;
24 use Try::Tiny;
25 use CXGN::People::Schema;
28 BEGIN { extends 'Catalyst::Controller::REST' }
30 __PACKAGE__->config(
31 default => 'application/json',
32 stash_key => 'rest',
33 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
38 =head2 autocomplete
40 Public Path: /ajax/people/autocomplete
42 Autocomplete a person name. Takes a single GET param,
43 C<person>, responds with a JSON array of completions for that term.
45 =cut
47 sub autocomplete : Local : ActionClass('REST') { }
49 sub autocomplete_GET :Args(1) {
50 my ( $self, $c , $print_id ) = @_;
52 my $person = $c->req->param('term');
53 # trim and regularize whitespace
54 $person =~ s/(^\s+|\s+)$//g;
55 $person =~ s/\s+/ /g;
56 my $q = "SELECT sp_person_id, first_name, last_name FROM sgn_people.sp_person
57 WHERE lower(first_name) like ? OR lower(last_name) like ?
58 LIMIT 20";
60 my $sth = $c->dbc->dbh->prepare($q);
61 $sth->execute( lc "$person\%" , lc "$person\%" );
62 my @results;
63 while (my ($sp_person_id, $first_name, $last_name) = $sth->fetchrow_array ) {
64 $sp_person_id = $print_id ? "," . $sp_person_id : undef;
65 push @results , "$first_name, $last_name $sp_person_id";
67 $c->{stash}->{rest} = \@results;
72 ###