From 0cd3142250bd2ad1f52f74b58d07c27a08568f4a Mon Sep 17 00:00:00 2001 From: swag Date: Thu, 13 Oct 2022 23:57:58 -0400 Subject: [PATCH] Implement flag button --- README.md | 1 - lib/PostText.pm | 10 ++++++++++ lib/PostText/Controller/Remark.pm | 14 ++++++++++++++ lib/PostText/Controller/Thread.pm | 12 +++++++++++- lib/PostText/Model/Remark.pm | 16 ++++++++++++++++ lib/PostText/Model/Thread.pm | 16 ++++++++++++++++ t/remark.t | 8 ++++++-- t/thread.t | 12 ++++++++++-- templates/remark/by_id.html.ep | 1 + templates/thread/by_id.html.ep | 4 ++++ templates/thread/by_page.html.ep | 3 +++ 11 files changed, 91 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4f96c6b..1fa049b 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ Run the tests locally (against development environment): ## TODOs -1. Implement flag post button 1. Implement [bcrypt](https://metacpan.org/pod/Mojolicious::Plugin::BcryptSecure) 1. Some sort of admin/moderator login and view diff --git a/lib/PostText.pm b/lib/PostText.pm index e846c9a..8b68045 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -81,6 +81,11 @@ sub startup($self) { ->to('thread#bump') ->name('bump_thread'); + $thread->under('/flag') + ->get('/:thread_id', [thread_id => qr/[0-9]+/]) + ->to('thread#flag') + ->name('flag_thread'); + # Remark my $remark = $r->under('/remark'); @@ -93,6 +98,11 @@ sub startup($self) { ->get('/:remark_id', [remark_id => qr/[0-9]+/]) ->to('remark#by_id') ->name('single_remark'); + + $remark->under('/flag') + ->get('/:remark_id', [remark_id => qr/[0-9]+/]) + ->to('remark#flag') + ->name('flag_remark'); } 1; diff --git a/lib/PostText/Controller/Remark.pm b/lib/PostText/Controller/Remark.pm index 6ef53ce..ab7c1fa 100644 --- a/lib/PostText/Controller/Remark.pm +++ b/lib/PostText/Controller/Remark.pm @@ -57,4 +57,18 @@ sub create($self) { return $self->render; } +sub flag($self) { + my $remark_id = $self->param('remark_id'); + my $thread_id = $self->remark->thread_id_for($remark_id); + my $redirect_url = + $self->url_for('single_thread', thread_id => $thread_id) + ->fragment('info')->to_abs; + + + $self->remark->flag($remark_id); + $self->flash(info => "Remark #$remark_id has been flagged. 🚩"); + + $self->redirect_to($redirect_url); +} + 1; diff --git a/lib/PostText/Controller/Thread.pm b/lib/PostText/Controller/Thread.pm index f6cc724..0ba3a70 100644 --- a/lib/PostText/Controller/Thread.pm +++ b/lib/PostText/Controller/Thread.pm @@ -89,11 +89,21 @@ sub bump($self) { my $thread_id = $self->param('thread_id'); $self->thread->bump($thread_id); - $self->flash(info => "Thread #$thread_id has been bumped.🔝"); + $self->flash(info => "Thread #$thread_id has been bumped. 🔝"); $self->redirect_to( $self->url_for('threads_list')->fragment('info')->to_abs ); } +sub flag($self) { + my $thread_id = $self->param('thread_id'); + my $redirect_url = $self->url_for('threads_list')->fragment('info')->to_abs; + + $self->thread->flag($thread_id); + $self->flash(info => "Thread #$thread_id has been flagged. 🚩"); + + $self->redirect_to($redirect_url); +} + 1; diff --git a/lib/PostText/Model/Remark.pm b/lib/PostText/Model/Remark.pm index d65c294..7f600bd 100644 --- a/lib/PostText/Model/Remark.pm +++ b/lib/PostText/Model/Remark.pm @@ -106,4 +106,20 @@ sub thread_id_for($self, $remark_id) { END_SQL } +sub flag($self, $remark_id) { + $self->pg->db->query(<<~'END_SQL', $remark_id) + UPDATE remarks + SET flagged_status = TRUE + WHERE remark_id = ?; + END_SQL +} + +sub unflag($self, $remark_id) { + $self->pg->db->query(<<~'END_SQL', $remark_id) + UPDATE remarks + SET flagged_status = FALSE + WHERE remark_id = ?; + END_SQL +} + 1; diff --git a/lib/PostText/Model/Thread.pm b/lib/PostText/Model/Thread.pm index 30ee8a6..3679c6d 100644 --- a/lib/PostText/Model/Thread.pm +++ b/lib/PostText/Model/Thread.pm @@ -97,4 +97,20 @@ sub bump($self, $thread_id) { END_SQL } +sub flag($self, $thread_id) { + $self->pg->db->query(<<~'END_SQL', $thread_id) + UPDATE threads + SET flagged_status = TRUE + WHERE thread_id = ?; + END_SQL +} + +sub unflag($self, $thread_id) { + $self->pg->db->query(<<~'END_SQL', $thread_id) + UPDATE threads + SET flagged_status = FALSE + WHERE thread_id = ?; + END_SQL +} + 1; diff --git a/t/remark.t b/t/remark.t index a3f0388..cf1df17 100644 --- a/t/remark.t +++ b/t/remark.t @@ -46,7 +46,11 @@ subtest 'Post new remark', sub { subtest 'Flagging remark', sub { $t->get_ok('/remark/single/1')->status_is(200) ->element_exists('a[href*="flag"]') - ->text_like(h2 => qr/Thread #1/); -} + ->text_like(h2 => qr/Remark #1/); + + $t->get_ok('/remark/flag/1')->status_is(200) + ->element_exists('p[class="field-with-info"]') + ->text_like(p => qr/Remark #1 has been flagged/); +}; done_testing(); diff --git a/t/thread.t b/t/thread.t index 743d321..427737b 100644 --- a/t/thread.t +++ b/t/thread.t @@ -72,9 +72,13 @@ subtest 'Bumping thread', sub { ->element_exists('a[href*="bump"]') ->text_like(h2 => qr/Threads List/); + $t->get_ok('/thread/single/1')->status_is(200) + ->element_exists('a[href*="bump"]') + ->text_like(h2 => qr/Thread #1/); + $t->get_ok('/thread/bump/1')->status_is(200) ->element_exists('p[class="field-with-info"]') - ->text_like(p => qr/Thread #[0-9]+ has been bumped/); + ->text_like(p => qr/Thread #1 has been bumped/); }; subtest 'Flagging thread', sub { @@ -85,6 +89,10 @@ subtest 'Flagging thread', sub { $t->get_ok('/thread/single/1')->status_is(200) ->element_exists('a[href*="flag"]') ->text_like(h2 => qr/Thread #1/); -} + + $t->get_ok('/thread/flag/1')->status_is(200) + ->element_exists('p[class="field-with-info"]') + ->text_like(p => qr/Thread #1 has been flagged/); +}; done_testing(); diff --git a/templates/remark/by_id.html.ep b/templates/remark/by_id.html.ep index a737f1d..e405419 100644 --- a/templates/remark/by_id.html.ep +++ b/templates/remark/by_id.html.ep @@ -11,4 +11,5 @@ diff --git a/templates/thread/by_id.html.ep b/templates/thread/by_id.html.ep index 3286cb3..1b67be2 100644 --- a/templates/thread/by_id.html.ep +++ b/templates/thread/by_id.html.ep @@ -12,6 +12,7 @@ <% if (my $first_remark = $remarks->[0]) { =%>
@@ -25,6 +26,9 @@

<%= $remark->{'date'} %>

<%= $remark->{'author'} %>

<%= $remark->{'body'} %>

+ <% } =%>
diff --git a/templates/thread/by_page.html.ep b/templates/thread/by_page.html.ep index b5e007d..744eea4 100644 --- a/templates/thread/by_page.html.ep +++ b/templates/thread/by_page.html.ep @@ -24,6 +24,9 @@ Bump (<%= $thread->{'bump_count'} %> bumps) <% end %> + <% } =%>