diff --git a/README.md b/README.md index 60c42a9..5f36ff6 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ Run the tests locally (against development environment) ## TODOs -1. Grow into full blown Mojo? 1. Document post_text.conf (whoopsie) 1. Bump button 1. CSS diff --git a/lib/PostText.pm b/lib/PostText.pm index 1cf3bfd..2eb9791 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -57,149 +57,37 @@ sub startup($self) { }); # Root redirect - $r->get('/', sub ($c) { $c->redirect_to('list') }); - - my $list = $r->under('/list'); - $list->get('/:list_page', [list_page => qr/[0-9]+/], {list_page => 1}, sub ($c) { - my $base_path = $c->match->path_for(list_page => undef)->{'path'}; - my $this_page = $c->param('list_page'); - my $last_page = $c->thread->last_page; - my $threads = $c->thread->by_page($this_page); - - $c->stash(status => 404) unless $threads->[0]; - - $c->stash( - threads => $threads, - this_page => $this_page, - last_page => $last_page, - base_path => $base_path - ); - - $c->render; - }); - - # Post - my $post = $r->under; - $post->any([qw{GET POST}], '/post', sub ($c) { - my $v; - - $v = $c->validation if $c->req->method eq 'POST'; - - if ($v && $v->has_data) { - my $thread_author = $c->param('author'); - my $thread_title = $c->param('title' ); - my $thread_body = $c->param('body' ); - - $v->required('author')->size(1, 63 ); - $v->required('title' )->size(1, 127 ); - $v->required('body' )->size(2, 4000); - - if ($v->has_error) { - $c->stash(status => 400) - } - else { - my $new_thread_id = $c->thread->create( - $thread_author, - $thread_title, - $thread_body - ); - - $c->session(author => $thread_author); - - return $c->redirect_to( - thread_page => {thread_id => $new_thread_id} - ); - } - } - - return $c->render; - }); - $post = $post->under('/post'); - $post->any([qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/], sub ($c) { - my ($thread_id, $v) = ($c->param('thread_id'), undef); - - $v = $c->validation if $c->req->method eq 'POST'; - - if ($v && $v->has_data) { - my $remark_author = $c->param('author'); - my $remark_body = $c->param('body' ); - - $v->required('author')->size(1, 63 ); - $v->required('body' )->size(2, 4000); - - if ($v->has_error) { - $c->stash(status => 400) - } - else { - $c->remark->create( - $thread_id, - $remark_author, - $remark_body - ); - - $c->session(author => $remark_author); - - return $c->redirect_to( - thread_page => {thread_id => $thread_id} - ); - } - } - - my $thread = $c->thread->by_id($thread_id); - my $last_remark = $c->remark->last_for($thread_id); - - $c->stash( - thread => $thread, - last_remark => $last_remark - ); - - return $c->render; - }); + $r->get('/', sub ($c) { $c->redirect_to('threads_list') }); # Thread - my $thread = $r->under('/thread/:thread_id', [thread_id => qr/[0-9]+/]); - $thread->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1}, sub ($c) { - my $thread_id = $c->param('thread_id'); - my $thread = $c->thread->by_id($thread_id); - my $base_path = $c->match->path_for(thread_page => undef)->{'path'}; - my $this_page = $c->param('thread_page'); - my $last_page = $c->remark->last_page_for($thread_id); - my $remarks = $c->remark->by_page_for($thread_id, $this_page); + my $threads_list = $r->under('/list'); + $threads_list + ->get('/:list_page', [list_page => qr/[0-9]+/], {list_page => 1}) + ->to('thread#by_page') + ->name('threads_list'); - if (my $thread_body = %$thread{'body'}) { - $c->stash( - thread => $thread, - base_path => $base_path, - this_page => $this_page, - last_page => $last_page, - remarks => $remarks - ) - } - else { - $c->stash( - thread => [], - status => 404 - ) - } + my $post_thread = $r->under; + $post_thread->any([qw{GET POST}], '/post')->to('thread#create') + ->name('post_thread'); - # Check for remarks or remark page number - $c->stash(status => 404) unless $remarks->[0] || 1 >= $this_page; - - $c->render; - }); + my $single_thread = + $r->under('/thread/:thread_id', [thread_id => qr/[0-9]+/]); + $single_thread + ->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1}) + ->to('thread#by_id') + ->name('single_thread'); # Remark - my $remark = $r->under('/remark'); - $remark->get('/:remark_id', [remark_id => qr/[0-9]+/], sub ($c) { - my $remark_id = $c->param('remark_id'); - my $remark = $c->remark->by_id($remark_id); + my $post_remark = $r->under('/post'); + $post_remark + ->any([qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/]) + ->to('remark#create') + ->name('post_remark'); - $c->stash(status => 404) unless $remark->{'id'}; - - $c->stash(remark => $remark); - - $c->render; - }); + my $single_remark = $r->under('/remark'); + $single_remark->get('/:remark_id', [remark_id => qr/[0-9]+/]) + ->to('remark#by_id') + ->name('single_remark'); } 1; diff --git a/lib/PostText/Controller/Remark.pm b/lib/PostText/Controller/Remark.pm index e69de29..f1931bc 100644 --- a/lib/PostText/Controller/Remark.pm +++ b/lib/PostText/Controller/Remark.pm @@ -0,0 +1,57 @@ +package PostText::Controller::Remark; + +use Mojo::Base 'Mojolicious::Controller', -signatures; + +sub by_id($self) { + my $remark_id = $self->param('remark_id'); + my $remark = $self->remark->by_id($remark_id); + + $self->stash(status => 404) unless $remark->{'id'}; + + $self->stash(remark => $remark); + + $self->render; +} + +sub create($self) { + my ($thread_id, $v) = ($self->param('thread_id'), undef); + + $v = $self->validation if $self->req->method eq 'POST'; + + if ($v && $v->has_data) { + my $remark_author = $self->param('author'); + my $remark_body = $self->param('body' ); + + $v->required('author')->size(1, 63 ); + $v->required('body' )->size(2, 4000); + + if ($v->has_error) { + $self->stash(status => 400) + } + else { + $self->remark->create( + $thread_id, + $remark_author, + $remark_body + ); + + $self->session(author => $remark_author); + + return $self->redirect_to( + single_thread => {thread_id => $thread_id} + ); + } + } + + my $thread = $self->thread->by_id($thread_id); + my $last_remark = $self->remark->last_for($thread_id); + + $self->stash( + thread => $thread, + last_remark => $last_remark + ); + + return $self->render; +} + +1; diff --git a/lib/PostText/Controller/Thread.pm b/lib/PostText/Controller/Thread.pm new file mode 100644 index 0000000..0e21267 --- /dev/null +++ b/lib/PostText/Controller/Thread.pm @@ -0,0 +1,88 @@ +package PostText::Controller::Thread; + +use Mojo::Base 'Mojolicious::Controller', -signatures; + +sub create($self) { + my $v; + + $v = $self->validation if $self->req->method eq 'POST'; + + if ($v && $v->has_data) { + my $thread_author = $self->param('author'); + my $thread_title = $self->param('title' ); + my $thread_body = $self->param('body' ); + + $v->required('author')->size(1, 63 ); + $v->required('title' )->size(1, 127 ); + $v->required('body' )->size(2, 4000); + + if ($v->has_error) { + $self->stash(status => 400) + } + else { + my $new_thread_id = $self->thread->create( + $thread_author, + $thread_title, + $thread_body + ); + + $self->session(author => $thread_author); + + return $self->redirect_to( + single_thread => {thread_id => $new_thread_id} + ); + } + } + + return $self->render; +} + +sub by_id($self) { + my $thread_id = $self->param('thread_id'); + my $thread = $self->thread->by_id($thread_id); + my $base_path = $self->match->path_for(thread_page => undef)->{'path'}; + my $this_page = $self->param('thread_page'); + my $last_page = $self->remark->last_page_for($thread_id); + my $remarks = $self->remark->by_page_for($thread_id, $this_page); + + if (my $thread_body = $thread->{'body'}) { + $self->stash( + thread => $thread, + base_path => $base_path, + this_page => $this_page, + last_page => $last_page, + remarks => $remarks + ) + } + else { + $self->stash( + thread => [], + status => 404 + ) + } + + # Check for remarks or remark page number + $self->stash(status => 404) unless $remarks->[0] || 1 >= $this_page; + + $self->render; +} + +sub by_page($self) { + my $base_path = $self->match->path_for(list_page => undef)->{'path'}; + my $this_page = $self->param('list_page'); + my $last_page = $self->thread->last_page; + my $threads = $self->thread->by_page($this_page); + + $self->stash(status => 404) unless $threads->[0]; + + $self->stash( + threads => $threads, + this_page => $this_page, + last_page => $last_page, + base_path => $base_path + ); + + $self->render; +} + +1; diff --git a/PostText.pl b/script/post_text similarity index 59% rename from PostText.pl rename to script/post_text index cb8dec8..a889fea 100755 --- a/PostText.pl +++ b/script/post_text @@ -3,8 +3,8 @@ # PostText v0.1 # Jul 22 -use Mojo::Base -strict; -use lib qw{lib}; +use Mojo::File qw(curfile); +use lib curfile->dirname->sibling('lib')->to_string; use Mojolicious::Commands; Mojolicious::Commands->start_app('PostText'); diff --git a/t/list.t b/t/list.t index fd9bdb6..6f70183 100644 --- a/t/list.t +++ b/t/list.t @@ -1,13 +1,10 @@ #!/usr/bin/env perl -use strict; -use warnings; +use Mojo::Base -strict; use Test::More; -use Mojo::File qw{curfile}; use Test::Mojo; -my $script = curfile->dirname->sibling('PostText.pl'); -my $t = Test::Mojo->new($script); +my $t = Test::Mojo->new('PostText'); $t->get_ok('/list' )->status_is(200)->text_like(h2 => qr/Threads List/); $t->get_ok('/list/1')->status_is(200)->text_like(h2 => qr/Threads List/); diff --git a/t/post.t b/t/post.t index 1b12a6d..ff36eda 100644 --- a/t/post.t +++ b/t/post.t @@ -1,13 +1,11 @@ #!/usr/bin/env perl -use strict; -use warnings; +use Mojo::Base -strict; use Test::More; -use Mojo::File qw{curfile}; use Test::Mojo; -my $script = curfile->dirname->sibling('PostText.pl'); -my $t = Test::Mojo->new($script); +my $t = Test::Mojo->new('PostText'); + my %valid_params = ( author => 'Anonymous', title => 'hi', diff --git a/t/remark.t b/t/remark.t index 40e2708..4d27a0b 100644 --- a/t/remark.t +++ b/t/remark.t @@ -1,13 +1,10 @@ #!/usr/bin/env perl -use strict; -use warnings; +use Mojo::Base -strict; use Test::More; -use Mojo::File qw{curfile}; use Test::Mojo; -my $script = curfile->dirname->sibling('PostText.pl'); -my $t = Test::Mojo->new($script); +my $t = Test::Mojo->new('PostText'); $t->get_ok('/remark/1')->status_is(200)->text_like(h2 => qr/Remark #1/); diff --git a/t/root.t b/t/root.t index f58f36d..7f13989 100644 --- a/t/root.t +++ b/t/root.t @@ -1,13 +1,10 @@ #!/usr/bin/env perl -use strict; -use warnings; +use Mojo::Base -strict; use Test::More; -use Mojo::File qw{curfile}; use Test::Mojo; -my $script = curfile->dirname->sibling('PostText.pl'); -my $t = Test::Mojo->new($script); +my $t = Test::Mojo->new('PostText'); $t->get_ok('/')->status_is(302); diff --git a/t/thread.t b/t/thread.t index d9df331..5897ce2 100644 --- a/t/thread.t +++ b/t/thread.t @@ -1,13 +1,10 @@ #!/usr/bin/env perl -use strict; -use warnings; +use Mojo::Base -strict; use Test::More; -use Mojo::File qw{curfile}; use Test::Mojo; -my $script = curfile->dirname->sibling('PostText.pl'); -my $t = Test::Mojo->new($script); +my $t = Test::Mojo->new('PostText'); $t->get_ok('/thread/1' )->status_is(200)->text_like(h2 => qr/Thread #1/); $t->get_ok('/thread/1/1')->status_is(200)->text_like(h2 => qr/Thread #1/); diff --git a/templates/layouts/main.html.ep b/templates/layouts/main.html.ep index 2efd096..7972f8a 100644 --- a/templates/layouts/main.html.ep +++ b/templates/layouts/main.html.ep @@ -7,8 +7,8 @@

Post::Text


<%= content =%> diff --git a/templates/list_page.html.ep b/templates/list_page.html.ep deleted file mode 100644 index 5472b3d..0000000 --- a/templates/list_page.html.ep +++ /dev/null @@ -1,28 +0,0 @@ -% layout 'main'; -% title 'Threads List'; -

<%= title %>

-
- <% for my $thread (@{$threads}) { =%> -
- -

<%= $thread->{'title'} %>

-

<%= $thread->{'date'} %>

-
<%= $thread->{'author'} %>
-

<%= truncate_text $thread->{'body'} %>

- -
- <% } =%> -
-<% if ($last_page && $last_page != 1) { =%> - -<% } =%> diff --git a/templates/remark_id.html.ep b/templates/remark/by_id.html.ep similarity index 81% rename from templates/remark_id.html.ep rename to templates/remark/by_id.html.ep index 0634141..a737f1d 100644 --- a/templates/remark_id.html.ep +++ b/templates/remark/by_id.html.ep @@ -10,5 +10,5 @@ diff --git a/templates/thread_id.html.ep b/templates/remark/create.html.ep similarity index 96% rename from templates/thread_id.html.ep rename to templates/remark/create.html.ep index 00d82a4..a4426fa 100644 --- a/templates/thread_id.html.ep +++ b/templates/remark/create.html.ep @@ -32,7 +32,7 @@

<%= $thread->{'title'} %>

<%= $thread->{'date'} %>

diff --git a/templates/thread_page.html.ep b/templates/thread/by_id.html.ep similarity index 82% rename from templates/thread_page.html.ep rename to templates/thread/by_id.html.ep index 72dd288..5755b39 100644 --- a/templates/thread_page.html.ep +++ b/templates/thread/by_id.html.ep @@ -10,7 +10,7 @@
<% if (my $first_remark = $remarks->[0]) { =%>
@@ -19,7 +19,7 @@

<%= $remark->{'date'} %>

<%= $remark->{'author'} %>
@@ -28,7 +28,7 @@ <% } =%>