seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / Contact.pm
blobc9995fff92af5640d543cbdb6568d6ef1ea29f38
2 =head1 NAME
4 SGN::Controller::AJAX::Contact - a REST controller class to provide the
5 functions for posting the contact form as an issue on github
7 =head1 DESCRIPTION
9 When the contact form is submitted it is posted as an issue to github
11 =head1 AUTHOR
13 Nicolas Morales <nm529@cornell.edu>
15 =cut
17 package SGN::Controller::AJAX::Contact;
19 use Moose;
20 use Data::Dumper;
21 use LWP::UserAgent;
22 use JSON;
24 BEGIN { extends 'Catalyst::Controller::REST' }
26 __PACKAGE__->config(
27 default => 'application/json',
28 stash_key => 'rest',
29 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
32 sub submit_contact_form : Path('/ajax/contact/submit') : ActionClass('REST') { }
34 sub submit_contact_form_POST : Args(0) {
35 my $self = shift;
36 my $c = shift;
37 my $title = $c->req->param('title');
38 my $body = $c->req->param('body');
39 my $security_answer = $c->req->param('security_answer');
40 my $security_attempt = $c->req->param('security_attempt');
42 if (!$security_attempt || !$security_answer || $security_answer ne $security_attempt){
43 $c->stash->{rest} = {error => "You must be a clever bot"};
44 $c->detach;
47 my $github_access_token = $c->config->{github_access_token};
48 my $website_name = $c->config->{project_name};
49 my $ua = LWP::UserAgent->new;
51 my $server_endpoint = "https://api.github.com/repos/solgenomics/contactform/issues?access_token=$github_access_token";
52 my $req = HTTP::Request->new(POST => $server_endpoint);
53 $req->header('content-type' => 'application/json');
55 my $post_data = { "title"=>$title, "body"=> $body, "labels"=>[$website_name] };
56 $req->content( encode_json $post_data);
58 my $resp = $ua->request($req);
59 if ($resp->is_success) {
60 my $message = $resp->decoded_content;
61 my $message_hash = decode_json $message;
62 #print STDERR Dumper $message_hash;
63 if ($message_hash->{id}){
64 $c->stash->{rest} = {success => 1};
65 } else {
66 $c->stash->{rest} = {error => 'The message was not posted to github correctly. Please try again.'};
68 } else {
69 $c->stash->{rest} = {error => "There was an error submitting the message. Please try again."};