From 451e751aae26727c18dd3898357c67411b6d9831 Mon Sep 17 00:00:00 2001 From: swag Date: Sat, 8 Oct 2022 00:42:31 -0400 Subject: [PATCH] Implement migration for bump --- lib/PostText.pm | 4 ++-- lib/PostText/Controller/Bump.pm | 14 -------------- lib/PostText/Controller/Thread.pm | 9 +++++++++ lib/PostText/Model/Bump.pm | 0 migrations/6/down.sql | 2 ++ migrations/6/up.sql | 12 ++++++++++++ t/bump.t | 14 -------------- t/thread.t | 9 +++++++++ 8 files changed, 34 insertions(+), 30 deletions(-) delete mode 100644 lib/PostText/Controller/Bump.pm delete mode 100644 lib/PostText/Model/Bump.pm create mode 100644 migrations/6/down.sql create mode 100644 migrations/6/up.sql delete mode 100644 t/bump.t diff --git a/lib/PostText.pm b/lib/PostText.pm index 9d6d1a6..9a8bb34 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -35,7 +35,7 @@ sub startup($self) { # Finish configuring some things $self->secrets($self->config->{'secrets'}) || die $@; - $self->pg->migrations->from_dir('migrations')->migrate(5); + $self->pg->migrations->from_dir('migrations')->migrate(6); if (my $threads_per_page = $self->config->{'threads_per_page'}) { $self->thread->per_page($threads_per_page) @@ -93,7 +93,7 @@ sub startup($self) { # Bump my $bump_thread = $r->under('/bump'); $bump_thread->get('/:thread_id', [thread_id => qr/[0-9]+/]) - ->to('bump#create') + ->to('thread#bump') ->name('bump_thread'); } diff --git a/lib/PostText/Controller/Bump.pm b/lib/PostText/Controller/Bump.pm deleted file mode 100644 index 252fd86..0000000 --- a/lib/PostText/Controller/Bump.pm +++ /dev/null @@ -1,14 +0,0 @@ -package PostText::Controller::Bump; - -use Mojo::Base 'Mojolicious::Controller', -signatures; - -sub create($self) { - my $thread_id = $self->param('thread_id'); - - $self->thread->bump($thread_id); - $self->flash(info => "Thread #$thread_id has been bumped.🔝"); - - $self->redirect_to('threads_list'); -} - -1; diff --git a/lib/PostText/Controller/Thread.pm b/lib/PostText/Controller/Thread.pm index 0e21267..b798ef9 100644 --- a/lib/PostText/Controller/Thread.pm +++ b/lib/PostText/Controller/Thread.pm @@ -85,4 +85,13 @@ sub by_page($self) { $self->render; } +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->redirect_to('threads_list'); +} + 1; diff --git a/lib/PostText/Model/Bump.pm b/lib/PostText/Model/Bump.pm deleted file mode 100644 index e69de29..0000000 diff --git a/migrations/6/down.sql b/migrations/6/down.sql new file mode 100644 index 0000000..89e83da --- /dev/null +++ b/migrations/6/down.sql @@ -0,0 +1,2 @@ +ALTER TABLE threads + DROP COLUMN bump_count; diff --git a/migrations/6/up.sql b/migrations/6/up.sql new file mode 100644 index 0000000..da28448 --- /dev/null +++ b/migrations/6/up.sql @@ -0,0 +1,12 @@ + ALTER TABLE threads + ADD COLUMN bump_count INTEGER + NOT NULL +DEFAULT 0; + +UPDATE threads t + SET bump_count = r.remark_count + FROM (SELECT COUNT(*) AS remark_count, + thread_id + FROM remarks + GROUP BY thread_id) AS r + WHERE t.thread_id = r.thread_id; diff --git a/t/bump.t b/t/bump.t deleted file mode 100644 index 4321244..0000000 --- a/t/bump.t +++ /dev/null @@ -1,14 +0,0 @@ -use Mojo::Base -strict; -use Test::More; -use Test::Mojo; - -my $t = Test::Mojo->new('PostText'); - -subtest 'Bumping thread', sub { - $t->get_ok('/list')->status_is(200) - ->element_exists('a[href~="bump"]') - ->text_like(h2 => qr/Threads List/); - - $t->get_ok('/bump/1')->status_is(302) - ->header_like(Location => qr/list/); -}; diff --git a/t/thread.t b/t/thread.t index 7f1a10a..158b00f 100644 --- a/t/thread.t +++ b/t/thread.t @@ -30,6 +30,15 @@ subtest 'View single thread', sub { $t->get_ok('/thread/1/1')->status_is(200)->text_like(h2 => qr/Thread #1/); }; +subtest 'Bumping thread', sub { + $t->get_ok('/list')->status_is(200) + ->element_exists('a[href~="bump"]') + ->text_like(h2 => qr/Threads List/); + + $t->get_ok('/bump/1')->status_is(302) + ->header_like(Location => qr/list/); +}; + subtest 'Post new thread', sub { $t->ua->max_redirects(1);