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

@ -9,25 +9,25 @@ use base qw(DBIx::Class::Core);
__PACKAGE__->table('users');
__PACKAGE__->add_columns(
user_id =>
user_id =>
{ data_type => 'integer',
is_numeric => 1,
is_nullable => 0,
is_auto_increment => 1,
},
username =>
username =>
{ data_type => 'text',
is_nullable => 0,
},
email =>
email =>
{ data_type => 'text',
is_nullable => 0,
},
password =>
password =>
{ data_type => 'text',
is_nullable => 0,
},
salt =>
salt =>
{ data_type => 'text',
is_nullable => 0,
},

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

@ -7,8 +7,8 @@ if ($self->session('is_auth') == 1) {
$userControls = "<a href=\"/logout\">logout</a>"}
else {
$userControls =
"<a href=\"/login\">login</a> |
<a href=\"/register\">register</a>"};
"<a href=\"/login\">login</a> |
<a href=\"/register\">register</a>"};
%>
<a href="/"><h2><%== $self->board_name %></h2></a>
<%== $userControls %><br /><br />

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>
%};
@ -8,23 +9,23 @@
<p>fields marked with <span style="color: red">*</span> are required</p>
<form method="post" action='/register'>
username: <input
id="username"
name="username"
id="username"
name="username"
/><span style="color: red"> *</span><br />
email: <input
id="email"
name="email"
type="email"
id="email"
name="email"
type="email"
/><span style="color: red"> *</span><br />
password: <input
id="password"
name="password"
type="password"
id="password"
name="password"
type="password"
/><span style="color: red"> *</span><br />
confirm password: <input
id="confirm-password"
name="confirm-password"
type="password"
id="confirm-password"
name="confirm-password"
type="password"
/><span style="color: red"> *</span><br />
<input type="submit" value="register account" />
</form>

View File

@ -1,14 +1,16 @@
% 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 %>
% my $_thread_id = shift; my $_thread_title = shift;
<div class=" thread-item thread-<%= $_thread_id %>">
<a href="/thread/<%= $_thread_id %>"></a>
<a href="/thread/<%= $_thread_id %>"></a>
</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) { %>
@ -17,7 +19,7 @@ like to <a href="/thread/new/<%= $subf_id %>">make one?</a>
<% } else {
foreach my $thread_id (@thread_list) { %>
<%= $thread_item->(
$thread_id,
$self->schema->resultset('Threads')->
title_from_id($thread_id)) %>
$thread_id,
$self->schema->resultset('Threads')->
title_from_id($thread_id)) %>
<% }} %>

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>