clean
[sgn.git] / lib / SGN / Controller / AJAX / Genefamily / Manual.pm
blob726c843bdcff919f95b5fa01d56abfc441640166
1 =head1 NAME
3 SGN::Controller::AJAX::Genefamily::Manual - a REST controller class to provide the
4 backend for objects linked with manual curated gene family (a phenome locusgroup)
6 =head1 DESCRIPTION
8 Add new locus members to an existing gene family
10 =head1 AUTHOR
12 Naama Menda <nm249@cornell.edu>
15 =cut
17 package SGN::Controller::AJAX::Genefamily::Manual;
19 use Moose;
21 use List::MoreUtils qw /any /;
22 use Try::Tiny;
23 use CXGN::Phenome::Schema;
24 use CXGN::Chado::Publication;
26 use CXGN::Page::FormattingHelpers qw/ info_table_html html_alternate_show /;
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' },
37 sub display_locusgroup_members : Chained('/genefamily/manual/get_genefamily') :PathPart('members') : ActionClass('REST') { }
39 sub display_locusgroup_members_GET {
40 my ($self, $c) = @_;
41 my $html;
42 my %data;
43 my $locusgroup = $c->stash->{genefamily};
44 my $members = $locusgroup->get_cxgn_members;
45 foreach my $locus_id (keys %$members) {
46 my $locus = $members->{$locus_id}->{locus};
47 my $locus_name = $locus->get_locus_name;
48 my $common_name = $locus->get_common_name;
49 my $evidence = $members->{$locus_id}->{evidence};
50 my $ref = $members->{$locus_id}->{reference};
51 no warnings 'uninitialized';
52 $data{$common_name} .= qq|<a href="/locus/$locus_id/view">$locus_name</a> ($evidence. $ref)<br />| ;
54 foreach my $common_name (sort keys %data) {
55 $html .= info_table_html( $common_name => $data{$common_name},
56 '__border' => 0, );
58 my $hashref;
59 $hashref->{html} = $html;
60 $c->stash->{rest} = $hashref;
64 sub add_member:Path('/ajax/genefamily/manual/add') :ActionClass('REST') {}
66 sub add_member_GET :Args(0) {
67 my ($self, $c) = @_;
68 $c->stash->{rest} = { error => "Nothing here, it's a GET.." } ;
71 sub add_member_POST :Args(0) {
72 my ( $self, $c ) = @_;
73 my $locusgroup_id = $c->req->param('locusgroup_id');
74 my $locus_input = $c->req->param('locus') ;
75 my $evidence_id = $c->req->param('evidence_id') ;
76 my $reference_id = $c->req->param('reference_id') ||
77 CXGN::Chado::Publication::get_curator_ref($c->dbc->dbh);
78 if (!$locus_input) {
79 $self->status_bad_request($c, message => 'need locus input param' );
80 return;
82 my ($locus_name, $locus_symbol, $locus_id) = split (/\|/ ,$locus_input);
83 my $phenome_schema = $c->dbic_schema('CXGN::Phenome::Schema');
84 my $locus = $phenome_schema
85 ->resultset('Locus')
86 ->find({ locus_id => $locus_id, {} } );
87 if (!$locus) {
88 $c->stash->{rest} = { error => "no locus found for id '$locus_id' " };
89 return;
91 my $locusgroup = $phenome_schema->resultset("Locusgroup")->find({locusgroup_id => $locusgroup_id } ) ;
92 if (!$c->user) {
93 $c->stash->{rest} = { error => 'Must be logged in for associating loci! ' };
94 return;
96 if ( any { $_ eq 'curator' || $_ eq 'submitter' || $_ eq 'sequencer' } $c->user->roles() ) {
97 # if this fails, it will throw an acception and will (probably rightly) be counted as a server error
98 my $user_id = $c->user->get_object->get_sp_person_id;
99 if ($locusgroup && $locus_id) {
100 try {
101 my $locusgroup_member = $phenome_schema->resultset("LocusgroupMember")->find_or_create( {
102 locusgroup_id => $locusgroup_id,
103 locus_id => $locus_id,
104 sp_person_id => $user_id,
105 evidence_id => $evidence_id,
106 reference_id => $reference_id,
108 $c->stash->{rest} = ['success'];
109 # need to update the loci div!!
110 return;
111 } catch {
112 $c->stash->{rest} = { error => "Failed: $_" };
113 return;
115 } else {
116 $c->stash->{rest} = { error => 'need both valid locusgroup_id and locus_id for adding the locusgroup member! ' };
118 } else {
119 $c->stash->{rest} = { error => 'No privileges for adding new locus to the locusgroup. You must have an sgn submitter account. Please contact sgn-feedback@solgenomics.net for upgrading your user account. ' };