biblesearch / lib / Biblesearch.pm /
28e7ba9 10 years ago
1 contributor
53 lines | 0.993kb
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);
  });
}

1;