Implement get_threads() and send em to /view

This commit is contained in:
swaggboi 2022-08-04 22:46:35 -04:00
parent c66b23b21a
commit 48de91f31b
4 changed files with 32 additions and 5 deletions

View File

@ -9,6 +9,7 @@ use Mojo::Pg;
# Load the local modules too # Load the local modules too
use lib 'lib'; use lib 'lib';
use PostText::Model::Thread; use PostText::Model::Thread;
#use Data::Dumper; # For your debugging pleasure
# Load Mojo plugins # Load Mojo plugins
plugin 'Config'; plugin 'Config';
@ -34,14 +35,18 @@ get '/', sub ($c) { $c->redirect_to('view') };
# View # View
get '/view', sub ($c) { get '/view', sub ($c) {
$c->render() my $threads = $c->thread->get_threads();
$c->stash(threads => $threads);
$c->render();
}; };
# Post # Post
any [qw{GET POST}], '/post', sub ($c) { any [qw{GET POST}], '/post', sub ($c) {
my $thread_author = $c->param('name'); my $thread_author = $c->param('name' );
my $thread_title = $c->param('title'); my $thread_title = $c->param('title');
my $thread_body = $c->param('post'); my $thread_body = $c->param('post' );
if ($thread_author && $thread_title && $thread_body) { if ($thread_author && $thread_title && $thread_body) {
$c->thread->create_thread($thread_author, $thread_title, $thread_body); $c->thread->create_thread($thread_author, $thread_title, $thread_body);

View File

@ -25,6 +25,6 @@ Run the tests locally (against development environment)
## TODOs ## TODOs
1. Moar tests... 1. Moar tests...
1. Retreive threads for View route 1. Pick a date format
1. Validate input 1. Validate input
1. **Moar tests!!** 1. **Moar tests!!**

View File

@ -25,4 +25,17 @@ sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) {
END_SQL END_SQL
} }
sub get_threads($self) {
$self->pg->db->query(<<~'END_SQL')->hashes()
SELECT thread_id AS id,
TO_CHAR(thread_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date,
thread_author AS author,
thread_title AS title,
thread_body AS body
FROM threads
WHERE NOT hidden_status
ORDER BY thread_date DESC;
END_SQL
}
1; 1;

View File

@ -1,4 +1,13 @@
% layout 'main'; % layout 'main';
% title 'View Threads'; % title 'View Threads';
<h2><%= title %></h2> <h2><%= title %></h2>
<p>Some threads here...</p> <div class="threads">
<% for my $thread (@$threads) { =%>
<article class="thread">
<h3 class="title"><%= %$thread{'title'} %></h3>
<h4 class="date"><%= %$thread{'date'} %></h4>
<h5 class="author"><%= %$thread{'author'} %></h5>
<p class="body"><%= %$thread{'body'} %></p>
</article>
<% } =%>
</div>