From f0073d1f1f720d9209c2ff40db568c33614bd168 Mon Sep 17 00:00:00 2001 From: ngoomie Date: Sat, 24 Aug 2024 01:04:40 -0600 Subject: [PATCH] Fully implement basic thread submission --- lib/CharmBoard/Controller/NewThread.pm | 32 ++++++++++++++++--- lib/CharmBoard/Model/Schema/Source/Posts.pm | 5 +++ lib/CharmBoard/Model/Schema/Source/Threads.pm | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/CharmBoard/Controller/NewThread.pm b/lib/CharmBoard/Controller/NewThread.pm index 2798863..5a6e3cd 100644 --- a/lib/CharmBoard/Controller/NewThread.pm +++ b/lib/CharmBoard/Controller/NewThread.pm @@ -12,7 +12,7 @@ use Mojo::Base 'Mojolicious::Controller', -signatures; sub thread_compose { my $c = shift; - my $subf_id = $c->param('id'); + my $subf_id = $c->param('id'); my $subf_cat = $c->schema->resultset('Subforums')->cat_from_id($subf_id); my $cat_title = @@ -34,10 +34,11 @@ sub thread_submit { my $thread_title = $c->param('thread-title'); my $post_content = $c->param('post-content'); - my $post_time = time; my $subf_id = $c->param('id'); + my $post_time = time; my $catch_error; + my $thread_id; # make sure post data is valid try { @@ -45,11 +46,34 @@ sub thread_submit { or die "Please fill both the title and post content fields" } catch ($catch_error) { $c->flash(error => $catch_error); - $c->redirect_to('board/:id/new') + $c->redirect_to('/board/:id/new') } # now send it - + try { + my $thread = $c->schema->resultset('Threads')->create({ + thread_title => $thread_title, + thread_subf => $subf_id + }) + or die "Server error, thread creation failed"; + $thread->update + or die "Server error, thread creation failed"; + $thread_id = $thread->id; + print $thread_id; + my $post = $c->schema->resultset('Posts')->create({ + user_id => $c->session('user_id'), + thread_id => $thread_id, + post_date => time, + post_body => $post_content + }) + or die "Thread was created successfully, but without an OP"; + $post->update + or die "Thread was created successfully, but without an OP"; + } catch ($catch_error) { + $c->flash(error => $catch_error); + $c->redirect_to('/board/:id/new') + }; + $c->redirect_to('/thread/' . $thread_id) } 1; diff --git a/lib/CharmBoard/Model/Schema/Source/Posts.pm b/lib/CharmBoard/Model/Schema/Source/Posts.pm index e4308f5..d160c83 100644 --- a/lib/CharmBoard/Model/Schema/Source/Posts.pm +++ b/lib/CharmBoard/Model/Schema/Source/Posts.pm @@ -29,6 +29,11 @@ __PACKAGE__->add_columns( post_date => { data_type => 'integer', is_nullable => 0, + }, + post_body => + { + data_type => 'text', + is_nullable => 0 } ); diff --git a/lib/CharmBoard/Model/Schema/Source/Threads.pm b/lib/CharmBoard/Model/Schema/Source/Threads.pm index debf29c..2b776b6 100644 --- a/lib/CharmBoard/Model/Schema/Source/Threads.pm +++ b/lib/CharmBoard/Model/Schema/Source/Threads.pm @@ -13,6 +13,7 @@ __PACKAGE__->table('threads'); __PACKAGE__->add_columns( thread_id => { data_type => 'integer', + is_numeric => 1, is_auto_increment => 1, is_nullable => 0, },