4 SGN::Controller::AJAX::People - a REST controller class to provide the
5 backend for the sgn_people schema
9 REST interface for searching people, getting user data, etc.
13 Naama Menda <nm249@cornell.edu>
18 package SGN
::Controller
::AJAX
::People
;
23 use List
::MoreUtils qw
/any /;
25 use CXGN
::People
::Schema
;
26 use CXGN
::People
::Roles
;
28 BEGIN { extends
'Catalyst::Controller::REST' }
31 default => 'application/json',
33 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
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.
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;
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 ?
60 my $sth = $c->dbc->dbh->prepare($q);
61 $sth->execute( lc "$person\%" , lc "$person\%" );
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;
70 sub people_and_roles
: Path
('/ajax/people/people_and_roles') : ActionClass
('REST') { }
72 sub people_and_roles_GET
: Args
(0) {
75 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
76 my $person_roles = CXGN
::People
::Roles
->new({ bcs_schema
=>$schema });
77 my $sp_persons = $person_roles->get_sp_persons();
78 my $sp_roles = $person_roles->get_sp_roles();
79 my %results = ( sp_persons
=> $sp_persons, sp_roles
=> $sp_roles );
80 $c->stash->{rest
} = \
%results;
83 sub add_person_role
: Path
('/ajax/people/add_person_role') : ActionClass
('REST') { }
85 sub add_person_role_GET
: Args
(0) {
88 my $user = $c->user();
90 $c->stash->{rest
} = {error
=>'You must be logged in first!'};
93 if (!$user->check_roles("curator")) {
94 $c->stash->{rest
} = {error
=>'You must be logged in with the correct role!'};
97 my $sp_person_id = $c->req->param('sp_person_id');
98 my $sp_role_id = $c->req->param('sp_role_id');
99 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
100 my $person_roles = CXGN
::People
::Roles
->new({ bcs_schema
=>$schema });
101 my $add_role = $person_roles->add_sp_person_role($sp_person_id, $sp_role_id);
102 $c->stash->{rest
} = {success
=>1};