package Biblesearch; our $VERSION = '0.01'; use Mojo::Base 'Mojolicious'; use DBIx::Custom; has 'dbi'; sub startup { my $self = shift; # DBI my $db_file = $self->home->rel_file('db/bible.db'); my $dbi = DBIx::Custom->connect( dsn => "dbi:SQLite:dbname=$db_file", option => {sqlite_unicode => 1}, connector => 1 ); $self->dbi($dbi); # Models my $models = [ { table => 'book', primary_key => 'id' }, { table => 'section', primary_key => [qw/book_id chapter section/] } ]; $dbi->create_model($_) for @$models; # Route my $r = $self->routes; $r->get('/')->to(template => 'index'); $r->get('/api/book/:id/content')->to(cb => sub { my $self = shift; my $id = $self->param('id'); my $dbi = $self->app->dbi; my $content = $dbi->model('book')->select('content', id => $id)->value; my $data = { content => $content }; $self->render(json => $data); }); $r->get('/api/word-count/:word')->to(cb => sub { my $self = shift; my $word = $self->param('word'); my $dbi = $self->app->dbi; my $word_count_h = {}; for (my $i = 0; $i < 66; $i++) { my $num = sprintf "%02d", $i + 1; my $content = $dbi->select( 'content_no_tag', table => 'book', where => {id => $num} )->value; my $content_length = length $content; my $word_q = quotemeta($word); $content =~ s/$word_q//g; my $content_length_no_word = length $content; # 文字の個数 my $word_count = ($content_length - $content_length_no_word) / length $word; $word_count_h->{$num} = $word_count; } $self->render(json => {word_count => $word_count_h}); }); } 1;