Implement create_thread() method

This commit is contained in:
swaggboi 2022-08-03 23:44:34 -04:00
parent 9f1c9f6061
commit c66b23b21a
3 changed files with 35 additions and 4 deletions

View File

@ -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

View File

@ -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!!**

View File

@ -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;