Implement migration for bump

This commit is contained in:
swagg boi 2022-10-08 00:42:31 -04:00
parent c3abf0e771
commit 451e751aae
8 changed files with 34 additions and 30 deletions

View file

@ -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');
}

View file

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

View file

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

2
migrations/6/down.sql Normal file
View file

@ -0,0 +1,2 @@
ALTER TABLE threads
DROP COLUMN bump_count;

12
migrations/6/up.sql Normal file
View file

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

View file

@ -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/);
};

View file

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