From 9c16cd0da59176cfd1525c7f6bfe45019c2ffd56 Mon Sep 17 00:00:00 2001 From: ngoomie Date: Mon, 19 Aug 2024 21:28:05 -0600 Subject: [PATCH] Split cat/subf listing into its own Model module as per #12 I was struggling with getting the model to access my schema helper and, I'm gonna be real with you chief, I don't even know how I managed to fix it. But it works... I guess? lmao --- README.md | 1 + lib/CharmBoard.pm | 1 + lib/CharmBoard/Controller/Index.pm | 27 +------------------ lib/CharmBoard/Model/Forums.pm | 43 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 26 deletions(-) create mode 100644 lib/CharmBoard/Model/Forums.pm diff --git a/README.md b/README.md index 70ba6fd..2d0a726 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ CharmBoard is forum software written in Perl, inspired by AcmlmBoard/its derivat - Perl5 - `Mojolicious` ([website](https://www.mojolicious.org/), [metacpan](https://metacpan.org/pod/Mojolicious)) + - `Mojolicious::Plugin::Model` - `Mojolicious::Plugin::Renderer::WithoutCache` — only needed in dev environment - `DBI` - `DBIx::Class` diff --git a/lib/CharmBoard.pm b/lib/CharmBoard.pm index 2230659..f6d0f8f 100644 --- a/lib/CharmBoard.pm +++ b/lib/CharmBoard.pm @@ -15,6 +15,7 @@ sub startup { # load plugins that require no additional conf $self->plugin('TagHelpers'); + $self->plugin('Model', {namespaces => ['CharmBoard::Model']}); # load configuration from config file my $config = diff --git a/lib/CharmBoard/Controller/Index.pm b/lib/CharmBoard/Controller/Index.pm index 8658f9a..a36a47a 100644 --- a/lib/CharmBoard/Controller/Index.pm +++ b/lib/CharmBoard/Controller/Index.pm @@ -12,34 +12,9 @@ use Tree::Simple; sub index { my $self = shift; - # fetch a list of all categories - my @all_cat = $self->schema->resultset('Categories')->fetch_all; - - # create a Tree::Simple object that will contain the list - # of categories and the subforums that belong to them - my $tree = Tree::Simple->new("subf_list", Tree::Simple->ROOT); - - my (@fetch_subf, $cat_branch); - foreach my $iter_cat (@all_cat) { - - # create branch of subf_list for the current category - $cat_branch = Tree::Simple->new($iter_cat, $tree); - - # fetch all subforums that belong to this category - @fetch_subf = - $self->schema->resultset('Subforums') - ->fetch_by_cat($iter_cat); - - # add each fetched subforum as children of the branch - # for the current category - foreach my $iter_subf (@fetch_subf) { - Tree::Simple->new($iter_subf, $cat_branch) - } - } - $self->render( template => 'index', - category_tree => $tree + category_tree => $self->model('forums')->list_full ) } diff --git a/lib/CharmBoard/Model/Forums.pm b/lib/CharmBoard/Model/Forums.pm new file mode 100644 index 0000000..0877661 --- /dev/null +++ b/lib/CharmBoard/Model/Forums.pm @@ -0,0 +1,43 @@ +package CharmBoard::Model::Forums; + +use utf8; +use strict; +use warnings; +use experimental qw(try); +use feature ':5.20'; + +use Mojo::Base 'MojoX::Model'; +use Tree::Simple; + +sub list_full { + my $self = shift; + # fetch a list of all categories + my @_all_cat = $self->{app}->schema->resultset('Categories')->fetch_all; + + # create a Tree::Simple object that will contain the list + # of categories and the subforums that belong to them + my $_tree = Tree::Simple->new("subf_list", Tree::Simple->ROOT); + + my (@_fetch_subf, $_cat_branch); + foreach my $_iter_cat (@_all_cat) { + + # create branch of subf_list for the current category + $_cat_branch = Tree::Simple->new($_iter_cat, $_tree); + + # fetch all subforums that belong to this category + @_fetch_subf = + $self->{app}->schema->resultset('Subforums') + ->fetch_by_cat($_iter_cat); + + # add each fetched subforum as children of the branch + # for the current category + foreach my $_iter_subf (@_fetch_subf) { + Tree::Simple->new($_iter_subf, $_cat_branch) + } + } + + return $_tree; +} + +1; +__END__ \ No newline at end of file