File Coverage

blib/lib/AproJo/Command/setup.pm
Criterion Covered Total %
statement 23 34 67.6
branch 3 8 37.5
condition 1 2 50.0
subroutine 3 4 75.0
pod 1 2 50.0
total 31 50 62.0


line stmt bran cond sub pod time code
1             package AproJo::Command::setup;
2 1     1   711 use Mojo::Base 'Mojolicious::Command';
  1         2  
  1         13  
3              
4 1     1   730 use Term::Prompt qw/prompt/;
  1         18897  
  1         684  
5              
6             has description => "Create the database for your AproJo application.\n";
7             has usage => "usage: $0 setup\n";
8              
9             sub run {
10 0     0 1 0 my ($self) = @_;
11              
12 0         0 my $user = prompt('x', 'Admin Username: ', '', '');
13 0         0 my $full = prompt('x', 'Admin Full Name: ', '', '');
14 0         0 my $pass1 = prompt('p', 'Admin Password: ', '', '');
15 0         0 print "\n";
16              
17             #TODO check for acceptable password
18              
19 0         0 my $pass2 = prompt('p', 'Repeat Admin Password: ', '', '');
20 0         0 print "\n";
21              
22 0 0       0 unless ($pass1 eq $pass2) {
23 0         0 die "Passwords do not match";
24             }
25              
26 0         0 $self->inject_sample_data($user, $pass1, $full);
27              
28 0         0 print "Database created! Run 'aprojo daemon' to start the server.\n";
29             }
30              
31             sub inject_sample_data {
32 1     1 0 66969 my $self = shift;
33 1 50       2 my $schema = eval { $_[-1]->isa('AproJo::DB::Schema') } ? pop() : $self->app->schema;
  1         13  
34              
35 1 50       3 my $user = shift or die "Must provide an administrative username";
36 1 50       4 my $pass = shift or die "Must provide a password for $user";
37 1   50     5 my $alias = shift || "Administrator";
38              
39 1         8 $schema->deploy({ add_drop_table => 1});
40            
41 1         699864 my $units = [
42             {'description_short' => 'pc', 'description_long' => 'pieces'},
43             {'description_short' => 'h', 'description_long' => 'hours'},
44             ];
45            
46 1         3 for my $unit (@$units) {
47 2         3993 $schema->resultset('Unit')->create($unit);
48             }
49            
50 1         1536 my $statuses = [
51             {'status' => 'ordered'},
52             {'status' => 'work'},
53             {'status' => 'proven'},
54             {'status' => 'invoiced'},
55             ];
56            
57 1         3 for my $status (@$statuses) {
58 4         4106 $schema->resultset('Status')->create($status);
59             }
60              
61 1         1704 my $role = $schema->resultset('Role')->create({
62             'name' => 'admin',
63             });
64              
65 1         1594 my $admin = $schema->resultset('User')->create({
66             name => $user,
67             alias => $alias,
68             password => $pass,
69             });
70            
71 1         1797 my $admin_role = $schema->resultset('UserRole')->create({
72             user_id => $admin->user_id,
73             role_id => $role->role_id,
74             });
75            
76             #my $group = $admin->add_to_groups({
77             # 'name' => 'admin',
78             #});
79              
80 1         1528 return $schema;
81             }
82              
83             1;