denny/ShinyCMS

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

Summary

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

# ===================================================================
# File:        bin/database/insert-shop-demo-data
# Project:    ShinyCMS
# Purpose:    Insert shop 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;

# 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();

# If we're in test mode, wipe the data first
if ( defined $ENV{ SHINYCMS_TEST } ) {
    $schema->resultset( 'BasketItem' )->delete;
    $schema->resultset( 'Basket'     )->delete;
    $schema->resultset( 'OrderItem'  )->delete;
    $schema->resultset( 'Order'      )->delete;
    $schema->resultset( 'ShopProductTypeElement' )->delete;
    $schema->resultset( 'ShopItemPostageOption'  )->delete;
    $schema->resultset( 'ShopItemElement'  )->delete;
    $schema->resultset( 'ShopItemCategory' )->delete;
    $schema->resultset( 'ShopItemView'     )->delete;
    $schema->resultset( 'ShopItemLike'     )->delete;
    $schema->resultset( 'ShopItem'         )->delete;
    $schema->resultset( 'ShopProductType'  )->delete;
    $schema->resultset( 'ShopCategory'     )->search({ parent => { '!=', undef } })->delete;
    $schema->resultset( 'ShopCategory'     )->delete;
}

# Create some shop categories
my $shop_category1 = $schema->resultset( 'ShopCategory' )->find_or_create({
    name        => 'Widgets',
    url_name    => 'widgets',
    description => 'This is the widgets section.',
});
my $shop_category2 = $schema->resultset( 'ShopCategory' )->find_or_create({
    name        => 'Doodahs',
    url_name    => 'doodahs',
    description => 'This is the doodahs section.',
});
my $shop_category3 = $shop_category1->shop_categories->find_or_create({
    name        => 'Ambidextrous Widgets',
    url_name    => 'ambi-widgets',
    description => 'This is the section for ambidextrous widgets.',
});
my $shop_category4 = $schema->resultset( 'ShopCategory' )->find_or_create({
    name        => 'T-shirts',
    url_name    => 't-shirts',
    description => 'This is the section for T-shirts.',
});


# Create some product types
my $product_type1 = $schema->resultset( 'ShopProductType' )->find_or_create({
    name          => 'Standard',
    template_file => 'standard.tt'
});
my $product_type2 = $schema->resultset( 'ShopProductType' )->find_or_create({
    name          => 'Paypal',
    template_file => 'paypal.tt'
});
my $product_type3 = $schema->resultset( 'ShopProductType' )->find_or_create({
    name          => 'T-shirt',
    template_file => 't-shirt.tt'
});
# Add some product type elements
my $element2_1 = $product_type2->shop_product_type_elements->find_or_create({
    name => 'paypal_button',
    type => 'Long Text',
});
my $element3_1 = $product_type3->shop_product_type_elements->find_or_create({
    name => 'sizes',
    type => 'Short Text',
});
my $element3_2 = $product_type3->shop_product_type_elements->find_or_create({
    name => 'colours',
    type => 'Short Text',
});


# Set up some postage options
my $postage_option1 = $schema->resultset( 'PostageOption' )->find_or_create({
    name  => 'Standard',
    price => '2.22',
});
my $postage_option2 = $schema->resultset( 'PostageOption' )->find_or_create({
    name  => 'Special',
    price => '3.33',
});
my $postage_option3 = $schema->resultset( 'PostageOption' )->find_or_create({
    name  => 'Gold',
    price => '5.55',
});


# Create some shop items
my $shop_item1 = $product_type1->shop_items->find_or_create({
    code        => 'blue-lh-widget',
    name        => 'Blue left-handed widget',
    description => 'A blue widget, suitable for left-handed applications.',
    image       => 'blue-dog.jpg',
    price       => '3.14',
    stock       => '10',
});
$shop_item1->shop_item_elements->find_or_create({
    name    => 'paypal_button',
    type    => 'Long Text',
    content => <<'EOT'
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="8299526">
<input type="image" src="https://www.paypal.com/en_GB/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
EOT
});
$shop_item1->shop_item_postage_options->find_or_create({
    postage => $postage_option1->id,
});
$shop_item1->shop_item_postage_options->find_or_create({
    postage => $postage_option2->id,
});
$shop_item1->shop_item_postage_options->find_or_create({
    postage => $postage_option3->id,
});
my $shop_item2 = $product_type1->shop_items->find_or_create({
    code        => 'red-rh-widget',
    name        => 'Red right-handed widget',
    description => 'A red widget, suitable for right-handed applications.',
    image       => 'redphanatic.jpg',
    price       => '2.72',
});
$shop_item2->shop_item_postage_options->find_or_create({
    postage => $postage_option1->id,
});
$shop_item2->shop_item_postage_options->find_or_create({
    postage => $postage_option2->id,
});
$shop_item2->shop_item_postage_options->find_or_create({
    postage => $postage_option3->id,
});
my $shop_item3 = $product_type2->shop_items->find_or_create({
    code        => 'green-ambi-widget',
    name        => 'Green ambidextrous widget',
    description => 'A green widget; swings both ways. Well tested.',
    image       => 'razer.jpg',
    price       => '1.23',
});
$shop_item3->shop_item_elements->find_or_create({
    name    => 'paypal_button',
    type    => 'Long Text',
    content => <<'EOT'
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="8299566">
<input type="image" src="https://www.paypal.com/en_GB/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
EOT
});
$shop_item3->shop_item_postage_options->find_or_create({
    postage => $postage_option1->id,
});
$shop_item3->shop_item_postage_options->find_or_create({
    postage => $postage_option2->id,
});
$shop_item3->shop_item_postage_options->find_or_create({
    postage => $postage_option3->id,
});
my $shop_item4 = $product_type3->shop_items->find_or_create({
    code        => 'green-t-shirt',
    name        => 'Green T-shirt',
    description => 'T-shirt with green design.',
    image       => 'razer.jpg',
    price       => '5.15',
});
$shop_item4->shop_item_elements->find_or_create({
    name    => 'sizes',
    type    => 'Short Text',
    content => 'Small,Medium,Large',
});
$shop_item4->shop_item_elements->find_or_create({
    name    => 'colours',
    type    => 'Short Text',
    content => 'Black,Blacker,Blackest',
});
$shop_item4->shop_item_postage_options->find_or_create({
    postage => $postage_option2->id,
});
$shop_item4->shop_item_postage_options->find_or_create({
    postage => $postage_option3->id,
});


# Put items in categories
$shop_item1->shop_item_categories->find_or_create({
    category => $shop_category1->id,
});
$shop_item2->shop_item_categories->find_or_create({
    category => $shop_category1->id,
});
$shop_item3->shop_item_categories->find_or_create({
    category => $shop_category1->id,
});
$shop_item3->shop_item_categories->find_or_create({
    category => $shop_category3->id,
});
$shop_item4->shop_item_categories->find_or_create({
    category => $shop_category4->id,
});


# Add a discussion thread to each of the items
my $discussion1 = $schema->resultset( 'Discussion' )->find_or_create({
    resource_id   => $shop_item1->id,
    resource_type => 'ShopItem',
});
$shop_item1->update({ discussion => $discussion1->id });
my $discussion2 = $schema->resultset( 'Discussion' )->find_or_create({
    resource_id   => $shop_item2->id,
    resource_type => 'ShopItem',
});
$shop_item2->update({ discussion => $discussion2->id });
my $discussion3 = $schema->resultset( 'Discussion' )->find_or_create({
    resource_id   => $shop_item3->id,
    resource_type => 'ShopItem',
});
$shop_item3->update({ discussion => $discussion3->id });

# Add a comment
my $comment1 = <<EOT;
Has anyone tested these widgets underwater? I'm not sure whether all widgets 
are waterproof and if so, up (down?) to what depth? Thanks!
EOT
my $next_id = $discussion1->comments->get_column( 'id' )->max;
$next_id++;
$discussion1->comments->find_or_create({
    id          => $next_id,
    author      => 1, # Default admin user
    author_type => 'Site User',
    body        => $comment1,
});


# Add an anonymous like to one of the items
$shop_item3->shop_items_like->create({
    ip_address => '10.10.10.10'
});

# Add some tags
my $tagset3 = $schema->resultset( 'Tagset' )->find_or_create({
    resource_id   => $shop_item3->id,
    resource_type => 'ShopItem',
});
$tagset3->tags->find_or_create({
    tag => 'green',
});
$tagset3->tags->find_or_create({
    tag => 'ambidextrous',
});
$tagset3->tags->find_or_create({
    tag => 'demo',
});
my $tagset4 = $schema->resultset( 'Tagset' )->find_or_create({
    resource_id   => $shop_item4->id,
    resource_type => 'ShopItem',
});
$tagset4->tags->find_or_create({
    tag => 'green',
});
$tagset4->tags->find_or_create({
    tag => 'clothing',
});