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);