1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#!/usr/bin/env perl
#utf8
# use Acme::PerlTidy;
package DBIxSkinny;
use utf8;
use DBIx::Skinny connect_info => +{
dsn => qq{dbi:SQLite:dbname=$0.db},
connect_options => +{sqlite_unicode => 1,}
};
package DBIxSkinny::Schema;
use utf8;
use DBIx::Skinny::Schema;
install_table motorcycles => schema {
pk 'id';
columns qw/id type brand color/;
};
package main;
use utf8;
use Mojolicious::Lite;
use Mojo::Util qw/md5_sum/;
app->log->level('debug')->path(qq{$0.log});
app->secret(md5_sum $0 )->log->debug(app->secret);
app->helper(model => sub { my $dbh = DBIxSkinny->new });
app->model->do(
'CREATE TABLE IF NOT EXISTS motorcycles (
id INTEGER PRIMARY KEY,
type TEXT NOT NULL,
brand TEXT NOT NULL,
color TEXT)'
);
get '/' => sub {
my $self = shift;
my $model = $self->model;
my $motorbikes =
[$model->search('motorcycles', {}, {order_by => [{type => 'ASC'}]},)];
$self->stash(
motorbikes => $motorbikes,
debug => $self->dumper([$self, $model]),
);
} => 'index';
post '/' => sub {
my $self = shift;
$self->model->create(
'motorcycles',
{ type => $self->param('type'),
brand => $self->param('brand'),
color => $self->param('color')
}
);
$self->redirect_to('/');
};
app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head><title>Motorcycles</title></head>
<body>
<table>
% foreach my $cycle (@{$motorbikes} ) {
<tr>
<td><%= $cycle->id %></td>
<td><%= $cycle->type %></td>
<td><%= $cycle->brand %></td>
<td><%= $cycle->color %></td>
</tr>
% }
</table>
<p>バイクを登録してください。</p>
<p>
<%= form_for '/' => (method => 'post') => begin %>
% foreach (qw/type brand color/) {
<%= uc($_) %>: <%= input_tag $_, 'type' => 'text' %><br />
% }
<%= submit_button 'Submit motorcycle' %>
<% end %>
</p>
<pre><%= $debug %></pre>
</body>
</html>
|