denny/ShinyCMS

View on GitHub
bin/database/data/insert-forums-demo-data

Summary

Maintainability
Test Coverage
#!/usr/bin/env perl

# ===================================================================
# File:        bin/database/insert-forum-demo-data
# Project:    ShinyCMS
# Purpose:    Insert forum demo data via DBIC
# 
# Author:    Denny de la Haye <2019@denny.me>
# Copyright (c) 2009-2019 Denny de la Haye
# 
# ShinyCMS is free software; you can redistribute it and/or modify it
# under the terms of either the GPL 2.0 or the Artistic License 2.0
# ===================================================================

use strict;
use warnings;

# CPAN modules
use DateTime::Duration;

# Load local helper lib and get connected schema object
use FindBin qw( $Bin );
use lib "$Bin/../../lib";
require 'helpers.pl';  ## no critic

my $schema = get_schema();


# Create some forum sections
my $section1 = $schema->resultset( 'ForumSection' )->find_or_create({
    name     => 'Hardware',
    url_name => 'hardware',
});
my $section2 = $schema->resultset( 'ForumSection' )->find_or_create({
    name     => 'Software',
    url_name => 'software',
});


# Create some forums
my $hw_forum1 = $section1->forums->find_or_create({
    name     => 'Laptops',
    url_name => 'laptops',
});
my $hw_forum2 = $section1->forums->find_or_create({
    name     => 'Desktops',
    url_name => 'desktops',
});
my $sw_forum1 = $section2->forums->find_or_create({
    name     => 'Linux',
    url_name => 'linux',
});


# Create some forum posts
my $body1 = <<'EOT';
This is where we will post more details of our forthcoming laptop competition!
EOT
my $forum_post1 = $hw_forum1->forum_posts->find_or_create({
    title     => 'Laptop Contest!',
    url_title => 'laptop-contest',
    author    => 1, # default admin user
    body      => $body1,
});
my $forum_post2 = $hw_forum1->forum_posts->find_or_create({
    title     => 'No talking',
    url_title => 'no-talking',
    author    => 1, # default admin user
    body      => 'No discussion here',
});
my $tomorrow = DateTime->now->add( days => 1 );
my $forum_post3 = $hw_forum1->forum_posts->find_or_create({
    title     => 'Future post',
    url_title => 'future',
    author    => 1, # default admin user
    body      => 'Future post...',
    posted    => $tomorrow->strftime( '%Y-%m-%d %H:%M:%S' ),
});
my $body4 = <<'EOT';
My Power On Self-Test is doing a weird sequence of beeps, and I'm not sure what 
to do next about it; should I send the laptop back (it's still in warranty) or 
is there anything else I can do to look into the issue before I do that?
EOT
my $forum_post4 = $hw_forum1->forum_posts->find_or_create({
    title     => 'BIOS POST',
    url_title => 'beep-beep-beep',
    author    => 1, # default admin user
    body      => $body4,
});

# Add a discussion thread to the first post
my $discussion1 = $schema->resultset( 'Discussion' )->find_or_create({
    resource_id   => $forum_post1->id,
    resource_type => 'ForumPost',
});
$forum_post1->update({ discussion => $discussion1->id });

# Add comments to the discussion
my $comment1 = <<EOT;
Hi, I'm interested in the difference between netbooks and laptops...
EOT
my $next_id = $discussion1->comments->get_column( 'id' )->max;
$next_id++;
my $c1 = $discussion1->comments->find_or_create({
    id          => $next_id,
    title       => 'Hardware testing...',
    author      => 1, # default admin user
    author_type => 'Site User',
    body        => $comment1,
});
my $comment2 = <<EOT;
What kind of thing do you want to know?
EOT
$next_id++;
my $c2 = $discussion1->comments->find_or_create({
    id          => $next_id,
    parent      => $c1->id,
    author      => 1, # default admin user
    author_type => 'Site User',
    body        => $comment2,
});
my $comment3 = <<EOT;
How about tablets?
EOT
$next_id++;
my $c3 = $discussion1->comments->find_or_create({
    id          => $next_id,
    author      => 1, # default admin user
    author_type => 'Site User',
    body        => $comment3,
});
my $comment4 = <<EOT;
Is there a clear test for which is which?
EOT
$next_id++;
my $c4 = $discussion1->comments->find_or_create({
    id          => $next_id,
    parent      => $c2->id,
    author      => 1, # default admin user
    author_type => 'Site User',
    body        => $comment4,
});
sleep 1;
my $comment5 = <<EOT;
Yeah, they should probably add a section for those.
EOT
$next_id++;
my $c5 = $discussion1->comments->find_or_create({
    id          => $next_id,
    parent      => $c3->id,
    author      => 1, # default admin user
    author_type => 'Site User',
    body        => $comment5,
});

# Add tags to some posts
my $tagset1 = $schema->resultset( 'Tagset' )->find_or_create({
    resource_id   => $forum_post1->id,
    resource_type => 'ForumPost',
});
$tagset1->tags->find_or_create({
    tag => 'test',
});
$tagset1->tags->find_or_create({
    tag => 'demo',
});
my $tagset3 = $schema->resultset( 'Tagset' )->find_or_create({
    resource_id   => $forum_post3->id,
    resource_type => 'ForumPost',
});
$tagset3->tags->find_or_create({
    tag => 'demo',
});