From edca297f78d5f727bb59b60617a06dcc692b46ae Mon Sep 17 00:00:00 2001 From: swag Date: Sun, 5 Dec 2021 02:52:31 -0500 Subject: [PATCH] Implement basic guestbook signing and reading --- README.md | 5 +++-- guestbook-ng.pl | 21 +++++++++++++++++++-- lib/GuestbookNg/Model/Message.pm | 11 +++++++++++ templates/index.html.ep | 19 ++++++++++++++++++- templates/layouts/default.html.ep | 8 ++++++-- templates/post.html.ep | 19 +++++++++++++++++++ 6 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 templates/post.html.ep diff --git a/README.md b/README.md index d1edccd..2f66a47 100644 --- a/README.md +++ b/README.md @@ -27,5 +27,6 @@ Add the `-v` option for more verbose output ## TODOs -1. Create hostname k/v pair in conf file -1. Create rows in table +1. Add tests for `/post` route +1. Probably rename `/post` to `/sign` +1. Remove the Test model and clean up diff --git a/guestbook-ng.pl b/guestbook-ng.pl index 20e2ab9..fca5a4c 100755 --- a/guestbook-ng.pl +++ b/guestbook-ng.pl @@ -7,6 +7,8 @@ use Mojolicious::Lite -signatures; use Mojo::Pg; use lib 'lib'; use GuestbookNg::Model::Test; +use GuestbookNg::Model::Message; +use Data::Dumper; # Uncomment for debugging # Plugins plugin 'Config'; @@ -30,7 +32,7 @@ helper test => sub { }; helper message => sub { - state $test = GuestbookNg::Model::Message->new(pg => shift->pg) + state $message = GuestbookNg::Model::Message->new(pg => shift->pg) }; # Routes @@ -39,7 +41,9 @@ under sub ($c) { }; get '/' => sub ($c) { - $c->render() + my $posts = $c->message->get_posts(); + + $c->render(posts => $posts); } => 'index'; any '/test' => sub ($c) { @@ -55,5 +59,18 @@ any '/test' => sub ($c) { ); }; +any '/post' => sub ($c) { + if ($c->req->method() eq 'POST') { + my $name = $c->param('name'); + my $message = $c->param('message'); + + $c->message->send_post($name, $message); + $c->redirect_to('index'); + } + else { + $c->render() + } +}; + # Send it app->start(); diff --git a/lib/GuestbookNg/Model/Message.pm b/lib/GuestbookNg/Model/Message.pm index a0d7f12..9defdd9 100644 --- a/lib/GuestbookNg/Model/Message.pm +++ b/lib/GuestbookNg/Model/Message.pm @@ -10,4 +10,15 @@ sub new($class, $pg, $object) { bless {$pg => $object} } +sub get_posts($self) { + $self->pg->db->query('SELECT date, name, msg FROM messages;')->arrays() +} + +sub send_post($self, $name, $msg) { + $self->pg->db->query( + 'INSERT INTO messages (date, name, msg) + VALUES (NOW(), ?, ?);', $name, $msg + ) +} + 1; diff --git a/templates/index.html.ep b/templates/index.html.ep index 9426c20..73ff448 100644 --- a/templates/index.html.ep +++ b/templates/index.html.ep @@ -1,3 +1,20 @@ % layout 'default'; % title 'Home'; -

Welcome to the Mojolicious real-time web framework!

+

Messages from the World Wide Web

+<% for my $row (reverse @$posts) { %> + + + + + + + + + + + + + +
Date:<%= @$row[0] %>
Name:<%= @$row[1] %>
Message:<%= @$row[2] %>
+
+<% } %> diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 0a644f6..0916ebb 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -5,7 +5,7 @@
-

<%= title %>

+

Guestbook-NG

@@ -13,10 +13,14 @@ - +
  <%= link_to Test => 'test' %>  <%= link_to Post => 'post' %><%= link_to Sign => 'post' %>
<%= content %> +
+

Maximize your dynamic innovation using battle-tested deep + learning models.

+
diff --git a/templates/post.html.ep b/templates/post.html.ep new file mode 100644 index 0000000..cf6032f --- /dev/null +++ b/templates/post.html.ep @@ -0,0 +1,19 @@ +% layout 'default'; +% title 'New Post'; +

Create a New Post

+
+ + + + + + + + + + + + + +
Name:<%= input_tag 'name' %>
Message:<%= text_area 'message', cols => 40, rows => 6 %>
 <%= submit_button 'Send it' %>
+