From 07353e7d7ea55b63e027e1539314ded4a19993fe Mon Sep 17 00:00:00 2001 From: swag Date: Sun, 19 Dec 2021 19:09:16 -0500 Subject: [PATCH] SQL style clean-up --- guestbook-ng.pl | 4 ++-- lib/GuestbookNg/Model/Message.pm | 15 +++++++++------ migrations/3/down.sql | 11 +++++++++++ migrations/3/up.sql | 11 +++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 migrations/3/down.sql create mode 100644 migrations/3/up.sql diff --git a/guestbook-ng.pl b/guestbook-ng.pl index 1bcdda1..6b765ca 100755 --- a/guestbook-ng.pl +++ b/guestbook-ng.pl @@ -58,7 +58,7 @@ get '/' => sub ($c) { } => 'index'; any [qw{GET POST}], '/sign' => sub ($c) { - if ($c->req->method() eq 'POST') { + if ($c->param('name') && $c->param('message')) { my $name = $c->param('name'); my $message = $c->param('message'); my $answer = $c->param('answer'); @@ -77,6 +77,6 @@ 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(2); +app->pg->migrations->from_dir('migrations')->migrate(3); app->start(); diff --git a/lib/GuestbookNg/Model/Message.pm b/lib/GuestbookNg/Model/Message.pm index 5604ac9..ccf8fa4 100644 --- a/lib/GuestbookNg/Model/Message.pm +++ b/lib/GuestbookNg/Model/Message.pm @@ -15,16 +15,19 @@ sub new($class, $pg, $pg_object) { sub get_posts($self) { $self->pg->db - ->query('SELECT to_char(date, \'Dy Mon DD HH:MI:SS AM TZ YYYY\'), - name, msg + ->query('SELECT to_char( + message_date, + \'Dy Mon DD HH:MI:SS AM TZ YYYY\' + ), + visitor_name, message FROM messages - ORDER BY date DESC;')->arrays() + ORDER BY message_date DESC;')->arrays() } -sub create_post($self, $name, $msg) { +sub create_post($self, $name, $message) { $self->pg->db->query( - 'INSERT INTO messages (date, name, msg) - VALUES (NOW(), ?, ?);', $name, $msg + 'INSERT INTO messages (message_date, visitor_name, message) + VALUES (NOW(), ?, ?);', $name, $message ) } diff --git a/migrations/3/down.sql b/migrations/3/down.sql new file mode 100644 index 0000000..919f6eb --- /dev/null +++ b/migrations/3/down.sql @@ -0,0 +1,11 @@ + ALTER TABLE messages +RENAME COLUMN message_id TO id; + + ALTER TABLE messages +RENAME COLUMN message_date TO date; + + ALTER TABLE messages +RENAME COLUMN visitor_name TO name; + + ALTER TABLE messages +RENAME COLUMN message TO msg; diff --git a/migrations/3/up.sql b/migrations/3/up.sql new file mode 100644 index 0000000..149aca7 --- /dev/null +++ b/migrations/3/up.sql @@ -0,0 +1,11 @@ + ALTER TABLE messages +RENAME COLUMN id TO message_id; + + ALTER TABLE messages +RENAME COLUMN date TO message_date; + + ALTER TABLE messages +RENAME COLUMN name TO visitor_name; + + ALTER TABLE messages +RENAME COLUMN msg TO message;