Back-end stuff to get the URL field working

This commit is contained in:
swagg boi 2021-12-31 23:45:21 -05:00
parent fb8cc6427e
commit d9b114f9ea
5 changed files with 21 additions and 7 deletions

View file

@ -63,10 +63,11 @@ get '/' => sub ($c) {
any [qw{GET POST}], '/sign' => sub ($c) {
if ($c->req->method() eq 'POST' && $c->param('message')) {
my $name = $c->param('name') || 'Anonymous';
my $url = $c->param('url');
my $message = $c->param('message');
my $answer = $c->param('answer');
$c->message->create_post($name, $message) if $answer;
$c->message->create_post($name, $message, $url) if $answer;
$c->redirect_to('index');
}
else {
@ -93,7 +94,7 @@ app->secrets(app->config->{'secrets'}) || die $@;
app->message->max_posts(app->config->{'max_posts'})
if app->config->{'max_posts'};
app->pg->migrations->from_dir('migrations')->migrate(3);
app->pg->migrations->from_dir('migrations')->migrate(4);
app->asset->store->paths(['assets']);
app->asset->process('swagg.css', 'css/swagg.css');

View file

@ -38,11 +38,19 @@ sub get_posts($self, $this_page = undef) {
}
}
sub create_post($self, $name, $message) {
sub create_post($self, $name, $message, $url = undef) {
if ($url) {
$self->pg->db->query(<<~'END_SQL', $name, $url, $message)
INSERT INTO messages (message_date, visitor_name, homepage_url, message)
VALUES (NOW(), ?, ?, ?);
END_SQL
}
else {
$self->pg->db->query(<<~'END_SQL', $name, $message)
INSERT INTO messages (message_date, visitor_name, message)
VALUES (NOW(), ?, ?);
END_SQL
}
}
sub max_posts($self, $value = undef) {

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

@ -0,0 +1,2 @@
ALTER TABLE messages
DROP COLUMN homepage_url;

2
migrations/4/up.sql Normal file
View file

@ -0,0 +1,2 @@
ALTER TABLE messages
ADD homepage_url VARCHAR(256);

View file

@ -10,6 +10,7 @@ my $script = curfile->dirname->sibling('guestbook-ng.pl');
my $t = Test::Mojo->new($script);
my %form = (
name => 'swagg boi',
url => 'http://localhost',
message => 'Ayy... lmao',
answer => 'false'
);