Back-end stuff to get the URL field working

This commit is contained in:
swag 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) { any [qw{GET POST}], '/sign' => sub ($c) {
if ($c->req->method() eq 'POST' && $c->param('message')) { if ($c->req->method() eq 'POST' && $c->param('message')) {
my $name = $c->param('name') || 'Anonymous'; my $name = $c->param('name') || 'Anonymous';
my $url = $c->param('url');
my $message = $c->param('message'); my $message = $c->param('message');
my $answer = $c->param('answer'); 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'); $c->redirect_to('index');
} }
else { else {
@ -93,7 +94,7 @@ app->secrets(app->config->{'secrets'}) || die $@;
app->message->max_posts(app->config->{'max_posts'}) app->message->max_posts(app->config->{'max_posts'})
if 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->store->paths(['assets']);
app->asset->process('swagg.css', 'css/swagg.css'); 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) $self->pg->db->query(<<~'END_SQL', $name, $message)
INSERT INTO messages (message_date, visitor_name, message) INSERT INTO messages (message_date, visitor_name, message)
VALUES (NOW(), ?, ?); VALUES (NOW(), ?, ?);
END_SQL END_SQL
}
} }
sub max_posts($self, $value = undef) { 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 $t = Test::Mojo->new($script);
my %form = ( my %form = (
name => 'swagg boi', name => 'swagg boi',
url => 'http://localhost',
message => 'Ayy... lmao', message => 'Ayy... lmao',
answer => 'false' answer => 'false'
); );