Clean up date format stuff

This commit is contained in:
swaggboi 2022-08-22 01:24:34 -04:00
parent b925f5c118
commit bf5a320e3e
3 changed files with 38 additions and 39 deletions

View File

@ -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. Reply model needs to become Remark (to fix the error handling stuff)
1. Add hyperlink somewhere to single thread view (whoopsie) 1. Add hyperlink somewhere to single thread view (whoopsie)
1. Paging for replies in single thread view 1. Paging for replies in single thread view
1. Pick a date format

View File

@ -9,16 +9,19 @@ has 'pg';
sub new($class, $pg, $pg_reference) { sub new($class, $pg, $pg_reference) {
bless { bless {
$pg => $pg_reference, $pg => $pg_reference,
replies_per_page => 5 replies_per_page => 5,
date_format => 'Dy Mon FMDD FMHH24:MI TZ YYYY'
}, $class }, $class
} }
sub get_replies_by_thread_id($self, $thread_id) { sub get_replies_by_thread_id($self, $thread_id) {
$self->pg->db->query(<<~'END_SQL', $thread_id)->hashes() my $date_format = %$self{'date_format'};
SELECT reply_id AS id,
TO_CHAR(reply_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date, $self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hashes();
reply_author AS author, SELECT reply_id AS id,
reply_body AS body TO_CHAR(reply_date, ?) AS date,
reply_author AS author,
reply_body AS body
FROM replies FROM replies
WHERE thread_id = ? WHERE thread_id = ?
AND NOT hidden_status AND NOT hidden_status
@ -26,8 +29,4 @@ sub get_replies_by_thread_id($self, $thread_id) {
END_SQL END_SQL
} }
#sub exception($self, $exception) {
# say $exception
#}
1; 1;

View File

@ -9,7 +9,8 @@ has 'pg';
sub new($class, $pg, $pg_reference) { sub new($class, $pg, $pg_reference) {
bless { bless {
$pg => $pg_reference, $pg => $pg_reference,
threads_per_page => 5 threads_per_page => 5,
date_format => 'Dy Mon FMDD FMHH24:MI TZ YYYY'
}, $class }, $class
} }
@ -29,12 +30,12 @@ sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) {
} }
sub get_all_threads($self) { sub get_all_threads($self) {
$self->pg->db->query(<<~'END_SQL')->hashes() $self->pg->db->query(<<~'END_SQL', %$self{'date_format'})->hashes()
SELECT thread_id AS id, SELECT thread_id AS id,
TO_CHAR(thread_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date, TO_CHAR(thread_date, ?) AS date,
thread_author AS author, thread_author AS author,
thread_title AS title, thread_title AS title,
thread_body AS body thread_body AS body
FROM threads FROM threads
WHERE NOT hidden_status WHERE NOT hidden_status
ORDER BY bump_date DESC; ORDER BY bump_date DESC;
@ -42,20 +43,22 @@ sub get_all_threads($self) {
} }
sub get_threads_by_page($self, $this_page = 1) { sub get_threads_by_page($self, $this_page = 1) {
my $date_format = %$self{'date_format'};
my $row_count = $self->{'threads_per_page'}; my $row_count = $self->{'threads_per_page'};
my $offset = ($this_page - 1) * $row_count; my $offset = ($this_page - 1) * $row_count;
$self->pg->db->query(<<~'END_SQL', $row_count, $offset)->hashes(); $self->pg->db
SELECT thread_id AS id, ->query(<<~'END_SQL', $date_format, $row_count, $offset)->hashes();
TO_CHAR(thread_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date, SELECT thread_id AS id,
thread_author AS author, TO_CHAR(thread_date, ?) AS date,
thread_title AS title, thread_author AS author,
thread_body AS body thread_title AS title,
FROM threads thread_body AS body
WHERE NOT hidden_status FROM threads
ORDER BY bump_date DESC WHERE NOT hidden_status
LIMIT ? OFFSET ?; ORDER BY bump_date DESC
END_SQL LIMIT ? OFFSET ?;
END_SQL
} }
sub threads_per_page($self, $value = undef) { sub threads_per_page($self, $value = undef) {
@ -81,19 +84,17 @@ sub get_thread_count($self) {
} }
sub get_thread_by_id($self, $thread_id) { sub get_thread_by_id($self, $thread_id) {
$self->pg->db->query(<<~'END_SQL', $thread_id)->hashes->[0] my $date_format = %$self{'date_format'};
SELECT thread_id AS id,
TO_CHAR(thread_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date, $self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash();
thread_author AS author, SELECT thread_id AS id,
thread_title AS title, TO_CHAR(thread_date, ?) AS date,
thread_body AS body thread_author AS author,
thread_title AS title,
thread_body AS body
FROM threads FROM threads
WHERE thread_id = ?; WHERE thread_id = ?;
END_SQL END_SQL
} }
#sub exception($self, $exception) {
# say $exception
#}
1; 1;