add files
|
1 |
package Biblesearch; |
2 | ||
3 |
our $VERSION = '0.01'; |
|
4 | ||
5 |
use Mojo::Base 'Mojolicious'; |
|
6 |
use DBIx::Custom; |
|
7 | ||
8 |
has 'dbi'; |
|
9 | ||
10 |
sub startup { |
|
11 |
my $self = shift; |
|
12 |
|
|
13 |
# DBI |
|
14 |
my $db_file = $self->home->rel_file('db/bible.db'); |
|
15 |
my $dbi = DBIx::Custom->connect( |
|
16 |
dsn => "dbi:SQLite:dbname=$db_file", |
|
17 |
option => {sqlite_unicode => 1}, |
|
18 |
connector => 1 |
|
19 |
); |
|
20 |
$self->dbi($dbi); |
|
21 |
|
|
22 |
# Models |
|
23 |
my $models = [ |
|
24 |
{ |
|
25 |
table => 'book', |
|
26 |
primary_key => 'id' |
|
27 |
}, |
|
28 |
{ |
|
29 |
table => 'section', |
|
30 |
primary_key => [qw/book_id chapter section/] |
|
31 |
} |
|
32 |
]; |
|
33 |
$dbi->create_model($_) for @$models; |
|
34 |
|
|
35 |
# Route |
|
36 |
my $r = $self->routes; |
|
37 |
$r->get('/')->to(template => 'index'); |
|
文字列の置換をjavascript側で行うようにした。
|
38 |
$r->get('/api/book/:id/content')->to(cb => sub { |
39 |
my $self = shift; |
|
40 |
my $id = $self->param('id'); |
|
41 |
|
|
42 |
my $dbi = $self->app->dbi; |
|
43 |
my $content = $dbi->model('book')->select('content', id => $id)->value; |
|
44 |
|
|
45 |
my $data = { |
|
46 |
content => $content |
|
47 |
}; |
|
48 |
|
|
49 |
$self->render(json => $data); |
|
50 |
}); |
|
回数を取得するAPIを追加
|
51 |
|
52 |
$r->get('/api/word-count/:word')->to(cb => sub { |
|
53 |
my $self = shift; |
|
54 |
my $word = $self->param('word'); |
|
55 |
my $dbi = $self->app->dbi; |
|
56 |
|
|
57 |
my $word_count_h = {}; |
|
58 |
for (my $i = 0; $i < 66; $i++) { |
|
59 |
my $num = sprintf "%02d", $i + 1; |
|
60 |
|
|
61 |
my $content = $dbi->select( |
|
62 |
'content_no_tag', |
|
63 |
table => 'book', |
|
64 |
where => {id => $num} |
|
65 |
)->value; |
|
66 |
my $content_length = length $content; |
|
67 |
my $word_q = quotemeta($word); |
|
68 |
$content =~ s/$word_q//g; |
|
69 |
my $content_length_no_word = length $content; |
|
70 |
|
|
71 |
# 文字の個数 |
|
72 |
my $word_count = ($content_length - $content_length_no_word) / length $word; |
|
73 |
$word_count_h->{$num} = $word_count; |
|
74 |
} |
|
75 |
|
|
76 |
$self->render(json => {word_count => $word_count_h}); |
|
77 |
}); |
|
add files
|
78 |
} |
79 | ||
80 |
1; |