Implement create_thread() method
This commit is contained in:
parent
9f1c9f6061
commit
c66b23b21a
16
PostText.pl
16
PostText.pl
|
@ -18,6 +18,10 @@ helper pg => sub {
|
||||||
state $pg = Mojo::Pg->new(app->config->{app->mode}{'pg_string'})
|
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
|
# Begin routing
|
||||||
under sub ($c) {
|
under sub ($c) {
|
||||||
$c->session(expires => time() + 31536000);
|
$c->session(expires => time() + 31536000);
|
||||||
|
@ -35,7 +39,17 @@ get '/view', sub ($c) {
|
||||||
|
|
||||||
# Post
|
# Post
|
||||||
any [qw{GET POST}], '/post', sub ($c) {
|
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
|
# Configure things
|
||||||
|
|
|
@ -25,4 +25,6 @@ Run the tests locally (against development environment)
|
||||||
## TODOs
|
## TODOs
|
||||||
|
|
||||||
1. Moar tests...
|
1. Moar tests...
|
||||||
1. Do something with submitted form data
|
1. Retreive threads for View route
|
||||||
|
1. Validate input
|
||||||
|
1. **Moar tests!!**
|
||||||
|
|
|
@ -6,8 +6,23 @@ use Mojo::Base -base, -signatures;
|
||||||
|
|
||||||
has 'pg';
|
has 'pg';
|
||||||
|
|
||||||
sub new($class, $pg, $pg_object) {
|
sub new($class, $pg, $pg_reference) {
|
||||||
bless {$pg => $pg_object}
|
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;
|
1;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user