seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / People.pm
blobd37a5161ec535540df75b9a1fa311565e24d772d
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;
26 use CXGN::People::Roles;
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;
70 sub people_and_roles : Path('/ajax/people/people_and_roles') : ActionClass('REST') { }
72 sub people_and_roles_GET : Args(0) {
73 my $self = shift;
74 my $c = shift;
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) {
86 my $self = shift;
87 my $c = shift;
88 my $user = $c->user();
89 if (!$user){
90 $c->stash->{rest} = {error=>'You must be logged in first!'};
91 $c->detach;
93 if (!$user->check_roles("curator")) {
94 $c->stash->{rest} = {error=>'You must be logged in with the correct role!'};
95 $c->detach;
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};