Grow into full-blown Mojo

This commit is contained in:
swag 2022-10-01 18:00:23 -04:00
parent 39f01309d6
commit d316efb952
17 changed files with 217 additions and 199 deletions

View File

@ -24,7 +24,6 @@ Run the tests locally (against development environment)
## TODOs ## TODOs
1. Grow into full blown Mojo?
1. Document post_text.conf (whoopsie) 1. Document post_text.conf (whoopsie)
1. Bump button 1. Bump button
1. CSS 1. CSS

View File

@ -57,149 +57,37 @@ sub startup($self) {
}); });
# Root redirect # Root redirect
$r->get('/', sub ($c) { $c->redirect_to('list') }); $r->get('/', sub ($c) { $c->redirect_to('threads_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;
});
# Thread # Thread
my $thread = $r->under('/thread/:thread_id', [thread_id => qr/[0-9]+/]); my $threads_list = $r->under('/list');
$thread->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1}, sub ($c) { $threads_list
my $thread_id = $c->param('thread_id'); ->get('/:list_page', [list_page => qr/[0-9]+/], {list_page => 1})
my $thread = $c->thread->by_id($thread_id); ->to('thread#by_page')
my $base_path = $c->match->path_for(thread_page => undef)->{'path'}; ->name('threads_list');
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);
if (my $thread_body = %$thread{'body'}) { my $post_thread = $r->under;
$c->stash( $post_thread->any([qw{GET POST}], '/post')->to('thread#create')
thread => $thread, ->name('post_thread');
base_path => $base_path,
this_page => $this_page,
last_page => $last_page,
remarks => $remarks
)
}
else {
$c->stash(
thread => [],
status => 404
)
}
# Check for remarks or remark page number my $single_thread =
$c->stash(status => 404) unless $remarks->[0] || 1 >= $this_page; $r->under('/thread/:thread_id', [thread_id => qr/[0-9]+/]);
$single_thread
$c->render; ->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1})
}); ->to('thread#by_id')
->name('single_thread');
# Remark # Remark
my $remark = $r->under('/remark'); my $post_remark = $r->under('/post');
$remark->get('/:remark_id', [remark_id => qr/[0-9]+/], sub ($c) { $post_remark
my $remark_id = $c->param('remark_id'); ->any([qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/])
my $remark = $c->remark->by_id($remark_id); ->to('remark#create')
->name('post_remark');
$c->stash(status => 404) unless $remark->{'id'}; my $single_remark = $r->under('/remark');
$single_remark->get('/:remark_id', [remark_id => qr/[0-9]+/])
$c->stash(remark => $remark); ->to('remark#by_id')
->name('single_remark');
$c->render;
});
} }
1; 1;

View File

@ -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;

View File

@ -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;

View File

@ -3,8 +3,8 @@
# PostText v0.1 # PostText v0.1
# Jul 22 # Jul 22
use Mojo::Base -strict; use Mojo::File qw(curfile);
use lib qw{lib}; use lib curfile->dirname->sibling('lib')->to_string;
use Mojolicious::Commands; use Mojolicious::Commands;
Mojolicious::Commands->start_app('PostText'); Mojolicious::Commands->start_app('PostText');

View File

@ -1,13 +1,10 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use Mojo::Base -strict;
use warnings;
use Test::More; use Test::More;
use Mojo::File qw{curfile};
use Test::Mojo; use Test::Mojo;
my $script = curfile->dirname->sibling('PostText.pl'); my $t = Test::Mojo->new('PostText');
my $t = Test::Mojo->new($script);
$t->get_ok('/list' )->status_is(200)->text_like(h2 => qr/Threads List/); $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/); $t->get_ok('/list/1')->status_is(200)->text_like(h2 => qr/Threads List/);

View File

@ -1,13 +1,11 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use Mojo::Base -strict;
use warnings;
use Test::More; use Test::More;
use Mojo::File qw{curfile};
use Test::Mojo; use Test::Mojo;
my $script = curfile->dirname->sibling('PostText.pl'); my $t = Test::Mojo->new('PostText');
my $t = Test::Mojo->new($script);
my %valid_params = ( my %valid_params = (
author => 'Anonymous', author => 'Anonymous',
title => 'hi', title => 'hi',

View File

@ -1,13 +1,10 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use Mojo::Base -strict;
use warnings;
use Test::More; use Test::More;
use Mojo::File qw{curfile};
use Test::Mojo; use Test::Mojo;
my $script = curfile->dirname->sibling('PostText.pl'); my $t = Test::Mojo->new('PostText');
my $t = Test::Mojo->new($script);
$t->get_ok('/remark/1')->status_is(200)->text_like(h2 => qr/Remark #1/); $t->get_ok('/remark/1')->status_is(200)->text_like(h2 => qr/Remark #1/);

View File

@ -1,13 +1,10 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use Mojo::Base -strict;
use warnings;
use Test::More; use Test::More;
use Mojo::File qw{curfile};
use Test::Mojo; use Test::Mojo;
my $script = curfile->dirname->sibling('PostText.pl'); my $t = Test::Mojo->new('PostText');
my $t = Test::Mojo->new($script);
$t->get_ok('/')->status_is(302); $t->get_ok('/')->status_is(302);

View File

@ -1,13 +1,10 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use Mojo::Base -strict;
use warnings;
use Test::More; use Test::More;
use Mojo::File qw{curfile};
use Test::Mojo; use Test::Mojo;
my $script = curfile->dirname->sibling('PostText.pl'); my $t = Test::Mojo->new('PostText');
my $t = Test::Mojo->new($script);
$t->get_ok('/thread/1' )->status_is(200)->text_like(h2 => qr/Thread #1/); $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/); $t->get_ok('/thread/1/1')->status_is(200)->text_like(h2 => qr/Thread #1/);

View File

@ -7,8 +7,8 @@
<body> <body>
<h1>Post::Text</h1> <h1>Post::Text</h1>
<nav> <nav>
<%= link_to List => 'list' %> <%= link_to List => 'threads_list' %>
<%= link_to New => 'post' %> <%= link_to New => 'post_thread' %>
</nav> </nav>
<hr> <hr>
<%= content =%> <%= content =%>

View File

@ -1,28 +0,0 @@
% layout 'main';
% title 'Threads List';
<h2><%= title %></h2>
<div class="threads">
<% for my $thread (@{$threads}) { =%>
<article class="thread">
<nav class="id">
<%= link_to "#$thread->{'id'}",
thread_page => {thread_id => $thread->{'id'}} %>
</nav>
<h3 class="title"><%= $thread->{'title'} %></h3>
<h4 class="date"><%= $thread->{'date'} %></h4>
<h5 class="author"><%= $thread->{'author'} %></h5>
<p class="body"><%= truncate_text $thread->{'body'} %></p>
<nav class="remarks">
<%= link_to Remark => thread_id => {thread_id => $thread->{'id'}} %>
<%= link_to thread_page => {thread_id => $thread->{'id'}}, begin %>
(<%= $thread->{'remark_count'} %> remarks)
<% end %>
</nav>
</article>
<% } =%>
</div>
<% if ($last_page && $last_page != 1) { =%>
<nav>
<%= pagination $this_page, $last_page, ($base_path . '/{page}') %>
</nav>
<% } =%>

View File

@ -10,5 +10,5 @@
</article> </article>
</div> </div>
<nav> <nav>
<%= link_to Thread => thread_page => {thread_id => $remark->{'thread_id'}} %> <%= link_to Thread => single_thread => {thread_id => $remark->{'thread_id'}} %>
</nav> </nav>

View File

@ -32,7 +32,7 @@
<article class="thread"> <article class="thread">
<nav class="id"> <nav class="id">
<%= link_to "#$thread->{'id'}", <%= link_to "#$thread->{'id'}",
thread_page => {thread_id => $thread->{'id'}} %> single_thread => {thread_id => $thread->{'id'}} %>
</nav> </nav>
<h3 class="title"><%= $thread->{'title'} %></h3> <h3 class="title"><%= $thread->{'title'} %></h3>
<h4 class="date"><%= $thread->{'date'} %></h4> <h4 class="date"><%= $thread->{'date'} %></h4>

View File

@ -10,7 +10,7 @@
</article> </article>
</div> </div>
<nav> <nav>
<%= link_to Remark => thread_id => {thread_id => $thread->{'id'}} %> <%= link_to Remark => post_remark => {thread_id => $thread->{'id'}} %>
</nav> </nav>
<% if (my $first_remark = $remarks->[0]) { =%> <% if (my $first_remark = $remarks->[0]) { =%>
<div class="remarks"> <div class="remarks">
@ -19,7 +19,7 @@
<article class="remark"> <article class="remark">
<nav class="id"> <nav class="id">
<%= link_to "#$remark->{'id'}", <%= link_to "#$remark->{'id'}",
remark_id => {remark_id => $remark->{'id'}} %> single_remark => {remark_id => $remark->{'id'}} %>
</nav> </nav>
<h4 class="date"><%= $remark->{'date'} %></h4> <h4 class="date"><%= $remark->{'date'} %></h4>
<h5 class="author"><%= $remark->{'author'} %></h5> <h5 class="author"><%= $remark->{'author'} %></h5>
@ -28,7 +28,7 @@
<% } =%> <% } =%>
</div> </div>
<nav> <nav>
<%= link_to Remark => thread_id => {thread_id => $thread->{'id'}} %> <%= link_to Remark => post_remark => {thread_id => $thread->{'id'}} %>
</nav> </nav>
<nav> <nav>
<% if ($last_page && $last_page != 1) { =%> <% if ($last_page && $last_page != 1) { =%>

View File

@ -0,0 +1,28 @@
% layout 'main';
% title 'Threads List';
<h2><%= title %></h2>
<div class="threads">
<% for my $thread (@{$threads}) { =%>
<article class="thread">
<nav class="id">
<%= link_to "#$thread->{'id'}",
single_thread => {thread_id => $thread->{'id'}} %>
</nav>
<h3 class="title"><%= $thread->{'title'} %></h3>
<h4 class="date"><%= $thread->{'date'} %></h4>
<h5 class="author"><%= $thread->{'author'} %></h5>
<p class="body"><%= truncate_text $thread->{'body'} %></p>
<nav class="remarks">
<%= link_to Remark => post_remark => {thread_id => $thread->{'id'}} %>
<%= link_to single_thread => {thread_id => $thread->{'id'}}, begin %>
(<%= $thread->{'remark_count'} %> remarks)
<% end %>
</nav>
</article>
<% } =%>
</div>
<% if ($last_page && $last_page != 1) { =%>
<nav>
<%= pagination $this_page, $last_page, ($base_path . '/{page}') %>
</nav>
<% } =%>