Add thread creation ability

This commit is contained in:
ngoomie 2024-08-08 01:22:31 -06:00
parent 51b2c54e9a
commit ff55197474
10 changed files with 119 additions and 34 deletions

View File

@ -67,17 +67,26 @@ sub startup {
# router
my $r = $self->routes;
# controller routes
## index page
$r->get('/')->to(
controller => 'Controller::Index',
action => 'index'
);
# view subforum
$r->get('/subforum/:id')->to(
controller => 'Controller::ViewSubf',
action => 'subf_view'
);
# controller routes
## index page
$r->get('/')->to(
controller => 'Controller::Index',
action => 'index'
# create thread
$r->get('/thread/new/:id')->to(
controller => 'Controller::NewThread',
action => 'thread_compose'
);
$r->post('/thread/new/:id')->to(
controller => 'Controller::NewThread',
action => 'thread_submit'
);
## registration page

View File

@ -0,0 +1,51 @@
package CharmBoard::Controller::NewThread;
use utf8;
use strict;
use warnings;
use experimental qw(try smartmatch);
use Mojo::Base 'Mojolicious::Controller', -signatures;
sub thread_compose {
my $self = shift;
my $subf_id = $self->param('id');
my $subf_cat =
$self->schema->resultset('Subforums')->cat_from_id($subf_id);
my $cat_title =
$self->schema->resultset('Categories')
->title_from_id($subf_cat);
$self->render(
template => 'thread_compose',
subf_id => $subf_id,
cat_title => $cat_title,
subf_title => $self->schema->resultset('Subforums')
->title_from_id($subf_id),
error => $self->flash('error'),
message => $self->flash('message')
)
}
sub thread_submit {
my $self = shift;
my $thread_title = $self->param('thread-title');
my $post_content = $self->param('post-content');
my $post_time = time;
my $catch_error;
# make sure post data is valid
try {
($thread_title, $post_content)
or die "Please fill both the title and post content fields"
} catch ($catch_error) {
$self->flash(error => $catch_error);
$self->redirect_to('/thread/new/:id')
}
}
1;
__END__

View File

@ -1,4 +1,5 @@
% layout 'default', title => $self->board_name;
% layout 'default',
% title => $self->board_name;
<% my $cat_header = begin %>
% my $_cat_id = shift; my $_name = shift;

View File

@ -1,4 +1,5 @@
% layout 'default', title => $self->board_name . ' - Login';
% layout 'default',
% title => $self->board_name . ' - Login';
% if ($error) {
<p style="color: red"><%= $error %></p>
%};

View File

@ -1,4 +1,5 @@
% layout 'default', title => $self->board_name . ' - Registration';
% layout 'default',
% title => $self->board_name . ' - Registration';
% if ($error) {
<p style="color: red"><%= $error %></p>
%};

View File

@ -1,4 +1,5 @@
% layout 'default', title => $subf_title . ' - ' . $self->board_name;
% layout 'default',
% title => $subf_title . ' - ' . $self->board_name;
% my @thread_list = @{stash('thread_list')};
<% my $thread_item = begin %>
@ -8,7 +9,8 @@
</div>
<% end %>
<a href="/"><%= $self->board_name %></a> » <%= $cat_title %> » <%= $subf_title %>
<a href="/"><%= $self->board_name %></a> » <%= $cat_title %> »
<%= $subf_title %>
<br /><br />
<% if (! @thread_list) { %>

View File

@ -0,0 +1,20 @@
% layout 'default',
% title => 'New thread - ' . $self->board_name;
<a href="/"><%= $self->board_name %></a> » <%= $cat_title %> »
<%= $subf_title %> » new thread
<br /><br />
<form method="post" action="/thread/new/<%= $subf_id %>">
<input
id="thread-title"
name="thread-title"
type="text"
placeholder="thread title"
/><br />
<textarea
id="post-content"
name="post-content"
cols="50" rows="5"
placeholder="post content">
</textarea><br /><br />
<input type="submit" value="post!" />
</form>