From bf5a320e3e3bbc2fbeb5b8695d70503e7621a0e6 Mon Sep 17 00:00:00 2001 From: swaggboi Date: Mon, 22 Aug 2022 01:24:34 -0400 Subject: [PATCH] Clean up date format stuff --- README.md | 1 - lib/PostText/Model/Reply.pm | 19 ++++++------ lib/PostText/Model/Thread.pm | 57 ++++++++++++++++++------------------ 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index cdc070e..b132100 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,3 @@ Run the tests locally (against development environment) 1. Reply model needs to become Remark (to fix the error handling stuff) 1. Add hyperlink somewhere to single thread view (whoopsie) 1. Paging for replies in single thread view -1. Pick a date format diff --git a/lib/PostText/Model/Reply.pm b/lib/PostText/Model/Reply.pm index 0c79e08..887a33d 100644 --- a/lib/PostText/Model/Reply.pm +++ b/lib/PostText/Model/Reply.pm @@ -9,16 +9,19 @@ has 'pg'; sub new($class, $pg, $pg_reference) { bless { $pg => $pg_reference, - replies_per_page => 5 + replies_per_page => 5, + date_format => 'Dy Mon FMDD FMHH24:MI TZ YYYY' }, $class } sub get_replies_by_thread_id($self, $thread_id) { - $self->pg->db->query(<<~'END_SQL', $thread_id)->hashes() - SELECT reply_id AS id, - TO_CHAR(reply_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date, - reply_author AS author, - reply_body AS body + my $date_format = %$self{'date_format'}; + + $self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hashes(); + SELECT reply_id AS id, + TO_CHAR(reply_date, ?) AS date, + reply_author AS author, + reply_body AS body FROM replies WHERE thread_id = ? AND NOT hidden_status @@ -26,8 +29,4 @@ sub get_replies_by_thread_id($self, $thread_id) { END_SQL } -#sub exception($self, $exception) { -# say $exception -#} - 1; diff --git a/lib/PostText/Model/Thread.pm b/lib/PostText/Model/Thread.pm index 51a871d..e75b51e 100644 --- a/lib/PostText/Model/Thread.pm +++ b/lib/PostText/Model/Thread.pm @@ -9,7 +9,8 @@ has 'pg'; sub new($class, $pg, $pg_reference) { bless { $pg => $pg_reference, - threads_per_page => 5 + threads_per_page => 5, + date_format => 'Dy Mon FMDD FMHH24:MI TZ YYYY' }, $class } @@ -29,12 +30,12 @@ sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) { } sub get_all_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 + $self->pg->db->query(<<~'END_SQL', %$self{'date_format'})->hashes() + SELECT thread_id AS id, + TO_CHAR(thread_date, ?) AS date, + thread_author AS author, + thread_title AS title, + thread_body AS body FROM threads WHERE NOT hidden_status ORDER BY bump_date DESC; @@ -42,20 +43,22 @@ sub get_all_threads($self) { } sub get_threads_by_page($self, $this_page = 1) { + my $date_format = %$self{'date_format'}; my $row_count = $self->{'threads_per_page'}; my $offset = ($this_page - 1) * $row_count; - $self->pg->db->query(<<~'END_SQL', $row_count, $offset)->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 bump_date DESC - LIMIT ? OFFSET ?; - END_SQL + $self->pg->db + ->query(<<~'END_SQL', $date_format, $row_count, $offset)->hashes(); + SELECT thread_id AS id, + TO_CHAR(thread_date, ?) AS date, + thread_author AS author, + thread_title AS title, + thread_body AS body + FROM threads + WHERE NOT hidden_status + ORDER BY bump_date DESC + LIMIT ? OFFSET ?; + END_SQL } sub threads_per_page($self, $value = undef) { @@ -81,19 +84,17 @@ sub get_thread_count($self) { } sub get_thread_by_id($self, $thread_id) { - $self->pg->db->query(<<~'END_SQL', $thread_id)->hashes->[0] - 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 + my $date_format = %$self{'date_format'}; + + $self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash(); + SELECT thread_id AS id, + TO_CHAR(thread_date, ?) AS date, + thread_author AS author, + thread_title AS title, + thread_body AS body FROM threads WHERE thread_id = ?; END_SQL } -#sub exception($self, $exception) { -# say $exception -#} - 1;