2 package SGN
::Controller
::AJAX
::Dataset
;
6 BEGIN { extends
'Catalyst::Controller::REST' }
14 default => 'application/json',
16 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
19 sub store_dataset
:Path
('/ajax/dataset/save') Args
(0) {
25 $c->stash->{rest
} = { error
=> "Login required to perform requested action." };
29 if (! ($c->user->check_roles('submitter') || $c->user->check_roles('curator'))) {
30 $c->stash->{rest
} = { error
=> "Insufficient privileges to perform requested action." };
37 my $dataset_name = $c->req->param("name");
38 my $dataset_description = $c->req->param("description");
40 my $people_schema = $c->dbic_schema("CXGN::People::Schema");
41 if (CXGN
::Dataset
->exists_dataset_name($people_schema, $dataset_name)) {
42 $c->stash->{rest
} = { error
=> "The dataset with name $dataset_name already exists. Please chose another name." };
46 my $user_id = $c->user()->get_object()->get_sp_person_id();
48 my $dataset = CXGN
::Dataset
->new( {
49 schema
=> $c->dbic_schema("Bio::Chado::Schema"),
50 people_schema
=> $people_schema,
53 $dataset->sp_person_id($user_id);
54 $dataset->name($dataset_name);
55 $dataset->description($dataset_description);
57 foreach my $type (qw
| trials accessions years locations plots traits breeding_programs genotyping_protocols trial_types trial_designs category_order
|) {
58 print STDERR
"Storing data: $type. $data{$type}\n";
60 my $json = $c->req->param($type);
62 my $obj = JSON
::Any
->jsonToObj($json);
63 $dataset->$type($obj);
69 $c->stash->{rest
} = { message
=> "Stored Dataset Successfully!" };
72 sub get_datasets_by_user
:Path
('/ajax/dataset/by_user') Args
(0) {
76 my $user = $c->user();
78 $c->stash->{rest
} = { error
=> "No logged in user to display dataset information for." };
82 my $datasets = CXGN
::Dataset
->get_datasets_by_user(
83 $c->dbic_schema("CXGN::People::Schema"),
84 $user->get_object()->get_sp_person_id()
87 $c->stash->{rest
} = $datasets;
90 sub get_dataset
:Path
('/ajax/dataset/get') Args
(1) {
93 my $dataset_id = shift;
95 my $dataset = CXGN
::Dataset
->new(
97 schema
=> $c->dbic_schema("Bio::Chado::Schema"),
98 people_schema
=> $c->dbic_schema("CXGN::People::Schema"),
99 sp_dataset_id
=> $dataset_id,
102 my $dataset_data = $dataset->get_dataset_data();
104 $c->stash->{rest
} = { dataset
=> $dataset_data };
107 sub delete_dataset
:Path
('/ajax/dataset/delete') Args
(1) {
110 my $dataset_id = shift;
113 $c->stash->{rest
} = { error
=> "Deleting datasets requires login" };
117 my $logged_in_user = $c->user()->get_object()->get_sp_person_id();
119 my $dataset = CXGN
::Dataset
->new(
121 schema
=> $c->dbic_schema("Bio::Chado::Schema"),
122 people_schema
=> $c->dbic_schema("CXGN::People::Schema"),
123 sp_dataset_id
=> $dataset_id,
126 print STDERR
"Dataset owner: ".$dataset->sp_person_id.", logged in: $logged_in_user\n";
127 if ($dataset->sp_person_id() != $logged_in_user) {
128 $c->stash->{rest
} = { error
=> "Only the owner can delete a dataset" };
132 my $error = $dataset->delete();
135 $c->stash->{rest
} = { error
=> $error };
138 $c->stash->{rest
} = { success
=> 1 };