From 48de91f31bf949aba59cf63935a5f266fd7c19e1 Mon Sep 17 00:00:00 2001 From: swaggboi Date: Thu, 4 Aug 2022 22:46:35 -0400 Subject: [PATCH] Implement get_threads() and send em to /view --- PostText.pl | 11 ++++++++--- README.md | 2 +- lib/PostText/Model/Thread.pm | 13 +++++++++++++ templates/view.html.ep | 11 ++++++++++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/PostText.pl b/PostText.pl index e0e6c0e..9b94a2f 100755 --- a/PostText.pl +++ b/PostText.pl @@ -9,6 +9,7 @@ use Mojo::Pg; # Load the local modules too use lib 'lib'; use PostText::Model::Thread; +#use Data::Dumper; # For your debugging pleasure # Load Mojo plugins plugin 'Config'; @@ -34,14 +35,18 @@ get '/', sub ($c) { $c->redirect_to('view') }; # View get '/view', sub ($c) { - $c->render() + my $threads = $c->thread->get_threads(); + + $c->stash(threads => $threads); + + $c->render(); }; # Post 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_body = $c->param('post'); + my $thread_body = $c->param('post' ); if ($thread_author && $thread_title && $thread_body) { $c->thread->create_thread($thread_author, $thread_title, $thread_body); diff --git a/README.md b/README.md index 68e9125..0858175 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,6 @@ Run the tests locally (against development environment) ## TODOs 1. Moar tests... -1. Retreive threads for View route +1. Pick a date format 1. Validate input 1. **Moar tests!!** diff --git a/lib/PostText/Model/Thread.pm b/lib/PostText/Model/Thread.pm index c303684..6e42593 100644 --- a/lib/PostText/Model/Thread.pm +++ b/lib/PostText/Model/Thread.pm @@ -25,4 +25,17 @@ sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) { 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; diff --git a/templates/view.html.ep b/templates/view.html.ep index 069175b..2a7c3db 100644 --- a/templates/view.html.ep +++ b/templates/view.html.ep @@ -1,4 +1,13 @@ % layout 'main'; % title 'View Threads';

<%= title %>

-

Some threads here...

+
+ <% for my $thread (@$threads) { =%> +
+

<%= %$thread{'title'} %>

+

<%= %$thread{'date'} %>

+
<%= %$thread{'author'} %>
+

<%= %$thread{'body'} %>

+
+ <% } =%> +