diff --git a/PostText.pl b/PostText.pl index cfa45cc..e0e6c0e 100755 --- a/PostText.pl +++ b/PostText.pl @@ -18,6 +18,10 @@ helper pg => sub { state $pg = Mojo::Pg->new(app->config->{app->mode}{'pg_string'}) }; +helper thread => sub { + state $thread = PostText::Model::Thread->new(pg => shift->pg) +}; + # Begin routing under sub ($c) { $c->session(expires => time() + 31536000); @@ -35,7 +39,17 @@ get '/view', sub ($c) { # Post any [qw{GET POST}], '/post', sub ($c) { - $c->render() + my $thread_author = $c->param('name'); + my $thread_title = $c->param('title'); + my $thread_body = $c->param('post'); + + if ($thread_author && $thread_title && $thread_body) { + $c->thread->create_thread($thread_author, $thread_title, $thread_body); + + return $c->redirect_to('view'); + } + + return $c->render(); }; # Configure things diff --git a/README.md b/README.md index a9fa13c..68e9125 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,6 @@ Run the tests locally (against development environment) ## TODOs 1. Moar tests... -1. Do something with submitted form data +1. Retreive threads for View route +1. Validate input +1. **Moar tests!!** diff --git a/lib/PostText/Model/Thread.pm b/lib/PostText/Model/Thread.pm index 07b7483..c303684 100644 --- a/lib/PostText/Model/Thread.pm +++ b/lib/PostText/Model/Thread.pm @@ -6,8 +6,23 @@ use Mojo::Base -base, -signatures; has 'pg'; -sub new($class, $pg, $pg_object) { - bless {$pg => $pg_object} +sub new($class, $pg, $pg_reference) { + bless {$pg => $pg_reference} +} + +sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) { + my @data = ($author, $title, $body, $hidden, $flagged); + + $self->pg->db->query(<<~'END_SQL', @data); + INSERT INTO threads ( + thread_author, + thread_title, + thread_body, + hidden_status, + flagged_status + ) + VALUES (?, ?, ?, ?, ?); + END_SQL } 1;