From e73170c1f2050afd662304bb7da6ff6bca9d6e00 Mon Sep 17 00:00:00 2001 From: swaggboi Date: Tue, 23 Aug 2022 19:06:33 -0400 Subject: [PATCH] Implement form for remarking to threads --- PostText.pl | 80 ++++++++++++++----- README.md | 7 +- templates/post_remark.html.ep | 25 ++++++ .../{post.html.ep => post_thread.html.ep} | 0 4 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 templates/post_remark.html.ep rename templates/{post.html.ep => post_thread.html.ep} (100%) diff --git a/PostText.pl b/PostText.pl index f70eff1..7483dd1 100755 --- a/PostText.pl +++ b/PostText.pl @@ -62,35 +62,73 @@ group { }; # Post -any [qw{GET POST}], '/post', sub ($c) { - my $v; +group { + under '/post'; - $v = $c->validation() if $c->req->method eq 'POST'; + any [qw{GET POST}], '/', sub ($c) { + my $v; - if ($v && $v->has_data) { - my $thread_author = $c->param('name' ) || 'Anonymous'; - my $thread_title = $c->param('title'); - my $thread_body = $c->param('post' ); + $v = $c->validation() if $c->req->method eq 'POST'; - $v->required('name' )->size(1, 63 ); - $v->required('title')->size(1, 127 ); - $v->required('post' )->size(2, 4000); + if ($v && $v->has_data) { + my $thread_author = $c->param('name' ) || 'Anonymous'; + my $thread_title = $c->param('title'); + my $thread_body = $c->param('post' ); - if ($v->has_error) { - $c->stash(status => 400) + $v->required('name' )->size(1, 63 ); + $v->required('title')->size(1, 127 ); + $v->required('post' )->size(2, 4000); + + if ($v->has_error) { + $c->stash(status => 400) + } + else { + $c->thread->create_thread( + $thread_author, + $thread_title, + $thread_body + ); + + return $c->redirect_to('view'); + } } - else { - $c->thread->create_thread( - $thread_author, - $thread_title, - $thread_body - ); - return $c->redirect_to('view'); + return $c->render(template => 'post_thread'); + }; + + any [qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/], sub ($c) { + my $v; + + $v = $c->validation() if $c->req->method eq 'POST'; + + if ($v && $v->has_data) { + my $thread_id = $c->param('thread_id'); + my $remark_name = $c->param('name'); + my $remark_body = $c->param('post'); + + $v->required('name' )->size(1, 63 ); + $v->required('post' )->size(2, 4000); + + if ($v->has_error) { + $c->stash(status => 400) + } + else { + $c->remark->create_remark( + $thread_id, + $remark_name, + $remark_body + ); + + # Gotta be a better way to name this route... + return $c->redirect_to( + 'thread_idremark_page', + {thread_id => $thread_id} + ); + } } - } - return $c->render(); + return $c->render(template => 'post_remark'); + }; }; # Thread diff --git a/README.md b/README.md index ecc02ae..8f65c13 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Install dependencies Run it in development mode - morbo PostText.pl + morbo -w assets/css/ -w lib/ -w migrations/ -w t/ -w templates/ PostText.pl Now try requesting http://localhost:3000 @@ -24,10 +24,11 @@ Run the tests locally (against development environment) ## TODOs -1. Form to create new remarks +1. Hyperlink to remarks form +1. Is there something I can do about route names? 1. I'm kinda hardcoding the single-thread view `link_to` in the templates because I cannot for the life of me figure out how to use `url_for` to populate the `thread_id` placeholder. Probably need to clean-up the HTML too, just used `` cuz I didn't know what else to use -1. Just clean stuff up in general lol +1. Tests for remark form diff --git a/templates/post_remark.html.ep b/templates/post_remark.html.ep new file mode 100644 index 0000000..44c2481 --- /dev/null +++ b/templates/post_remark.html.ep @@ -0,0 +1,25 @@ +% layout 'main'; +% title 'New Remark'; +

<%= title %>

+
+
+ <%= label_for name => 'Author' %> + <%= text_field name =>'Anonymous', maxlength => 63, minlength => 1 %> + <% if (my $error = validation->error('name')) { =%> +

Invalid name: 1 to 63 characters please.

+ <% } =%> +
+
+ <%= label_for post => 'Text' %> + <%= text_area 'post', ( + maxlength => 4000, + minlength => 2, + required => 'true', + rows => 6 + ) %> + <% if (my $error = validation->error('post')) { =%> +

Invalid post: Up to 4,000 characters only.

+ <% } =%> +
+ <%= submit_button 'Post', class => 'post button' %> +
diff --git a/templates/post.html.ep b/templates/post_thread.html.ep similarity index 100% rename from templates/post.html.ep rename to templates/post_thread.html.ep