seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / BrAPIAndroidApp.pm
bloba6fccdf0526cfcdadceb585e88d7d818e583f13d
2 =head1 NAME
4 SGN::Controller::AJAX::BrAPIAndroidApp - a REST controller class to provide additional functionality required for Android app
6 =head1 DESCRIPTION
9 =head1 AUTHOR
11 Nicolas Morales <nm529@cornell.edu>
13 =cut
15 package SGN::Controller::AJAX::BrAPIAndroidApp;
17 use Moose;
18 use Data::Dumper;
19 use JSON;
20 use CXGN::People::Login;
21 use CXGN::DB::Connection;
23 BEGIN { extends 'Catalyst::Controller::REST' }
25 __PACKAGE__->config(
26 default => 'application/json',
27 stash_key => 'rest',
28 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
31 sub new_database : Path('/brapiapp/new_database') : ActionClass('REST') { }
33 sub new_database_GET : Args(0) {
34 my $self = shift;
35 my $c = shift;
36 my $name = $c->req->param('databaseName');
37 my $url = $c->req->param('databaseURL');
38 my $session_id = $c->req->param('accessToken');
39 my $bcs_schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
41 my $previous_name_search = $bcs_schema->resultset('General::Db')->search({name=>$name});
42 if ($previous_name_search->count > 0){
43 $c->stash->{rest} = {error => "The given database name already exists and cannot be added!"};
44 $c->detach();
46 my $previous_url_search = $bcs_schema->resultset('General::Db')->search({url=>$url});
47 if ($previous_url_search->count > 0){
48 $c->stash->{rest} = {error => "The given database URL already exists and cannot be added!"};
49 $c->detach();
52 my $dbh = $c->dbc->dbh;
53 my $cookie_info = CXGN::Login->new($dbh)->query_from_cookie($session_id);
54 if ($cookie_info){
56 my $new_entry = $bcs_schema->resultset('General::Db')->create({
57 name=>$name,
58 url=>$url,
59 description=>'BrAPI_App_Database_Display'
60 });
61 if ($new_entry->db_id){
62 $c->stash->{rest} = {success => 1};
63 } else {
64 $c->stash->{rest} = {error => 'The new database entry was not saved!'};
66 } else {
67 $c->stash->{rest} = {error => "User Not Logged In"};
72 sub list_databases : Path('/brapiapp/list_databases') : ActionClass('REST') { }
74 sub list_databases_GET : Args(0) {
75 my $self = shift;
76 my $c = shift;
77 my $bcs_schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
78 my @db_list;
80 my $db_rs = $bcs_schema->resultset('General::Db')->search({description=>'BrAPI_App_Database_Display'}, {order_by=>{'-asc'=>'db_id'}});
81 while(my $r = $db_rs->next()){
82 push @db_list, [$r->name, $r->url];
84 $c->stash->{rest} = {database_list=>\@db_list};
87 sub remove_database : Path('/brapiapp/remove_database') : ActionClass('REST') { }
89 sub remove_database_POST : Args(0) {
90 my $self = shift;
91 my $c = shift;
92 my $session_id = $c->req->param('accessToken');
93 my $database_name = $c->req->param('databaseName');
94 my $bcs_schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
96 my $dbh = $c->dbc->dbh;
97 my $cookie_info = CXGN::Login->new($dbh)->query_from_cookie($session_id);
98 if ($cookie_info){
99 my $db = $bcs_schema->resultset('General::Db')->find({description=>'BrAPI_App_Database_Display', name=>$database_name});
100 $db->delete();
101 $c->stash->{rest} = {success => 1};
102 } else {
103 $c->stash->{rest} = {error => "User Not Logged In"};
107 sub register : Path('/brapiapp/register') : ActionClass('REST') { }
109 sub register_POST : Args(0) {
110 my $self = shift;
111 my $c = shift;
112 my $dbh = CXGN::DB::Connection->new();
113 my $username = $c->req->param('username');
114 my $password = $c->req->param('password');
115 my $email = $c->req->param('email');
116 my $organization = $c->req->param('organization');
117 my $first_name = $c->req->param('first_name');
118 my $last_name = $c->req->param('last_name');
120 my $new_user = CXGN::People::Login->new($dbh);
121 $new_user -> set_username($username);
122 $new_user -> set_password($password);
123 $new_user -> set_pending_email($email);
124 $new_user -> set_private_email($email);
125 $new_user -> set_confirm_code("ConfirmedByBrAPIApp");
126 #$new_user -> set_disabled("x");
127 $new_user -> set_organization($organization);
128 $new_user -> store();
130 #this is being added because the person object still uses two different objects, despite the fact that we've merged the tables
131 my $person_id=$new_user->get_sp_person_id();
132 my $new_person=CXGN::People::Person->new($dbh,$person_id);
133 $new_person->set_first_name($first_name);
134 $new_person->set_last_name($last_name);
135 $new_person->store();
137 $c->stash->{rest} = {success => 1};
140 sub search_parameters : Path('/brapiapp/searchparameters') : ActionClass('REST') { }
142 sub search_parameters_GET : Args(0) {
143 my $self = shift;
144 my $c = shift;
145 my $bcs_schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
146 my $current_call = $c->req->param('call');
147 my $cv_id = $bcs_schema->resultset('Cv::Cv')->find({name=>'BrAPIParameters'})->cv_id();
148 my $brapi_search_params = $bcs_schema->resultset('Cv::Cvterm')->find({cv_id=>$cv_id, name=>$current_call})->definition();
149 my @search_params = split ',', $brapi_search_params;
150 $c->stash->{rest} = {
151 success => 1,
152 parameters => \@search_params