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
This commit is contained in:
ngoomie 2024-08-19 21:28:05 -06:00
parent 3dab8cb6a0
commit 9c16cd0da5
4 changed files with 46 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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