Grow into full-blown Mojo

This commit is contained in:
swagg boi 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
1. Grow into full blown Mojo?
1. Document post_text.conf (whoopsie)
1. Bump button
1. CSS

View file

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

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
# 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');

View file

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

View file

@ -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',

View file

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

View file

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

View file

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

View file

@ -7,8 +7,8 @@
<body>
<h1>Post::Text</h1>
<nav>
<%= link_to List => 'list' %>
<%= link_to New => 'post' %>
<%= link_to List => 'threads_list' %>
<%= link_to New => 'post_thread' %>
</nav>
<hr>
<%= 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>
</div>
<nav>
<%= link_to Thread => thread_page => {thread_id => $remark->{'thread_id'}} %>
<%= link_to Thread => single_thread => {thread_id => $remark->{'thread_id'}} %>
</nav>

View file

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

View file

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