| Akron | b5809f4 | 2017-05-03 01:26:08 +0200 | [diff] [blame] | 1 | package Krawfish::Controller::Corpus; |
| 2 | use Mojo::Base 'Mojolicious::Controller'; |
| 3 | use Mojo::ByteStream 'b'; |
| 4 | |
| 5 | use Krawfish::Koral::Corpus::Builder; |
| 6 | use Krawfish::Koral::Meta; |
| 7 | |
| 8 | use strict; |
| 9 | use warnings; |
| 10 | |
| 11 | sub corpus { |
| 12 | my $c = shift; |
| 13 | my $v = $c->validation; |
| 14 | $v->optional('fields'); |
| 15 | $v->optional('count'); |
| 16 | $v->optional('page'); |
| 17 | $v->optional('sortBy'); |
| 18 | |
| 19 | my $corpus_id = $c->stash('corpus_id'); |
| 20 | |
| 21 | my $koral = Krawfish::Koral->new; |
| 22 | |
| 23 | # set corpus |
| 24 | $koral->corpus( |
| 25 | $koral->corpus_builder->string('corpus_id' => $corpus_id) |
| 26 | ); |
| 27 | |
| 28 | my $meta = $koral->meta_builder; |
| 29 | $meta->items_per_page($v->param('count')); |
| 30 | $meta->start_index($v->param('page')); # TODO! |
| 31 | # if ($v->param('sortBy')) { |
| 32 | # $meta->field_sort() |
| 33 | # }; |
| 34 | # etc. |
| 35 | |
| 36 | my $fields = b($v->param('fields'))->split(',')->uniq->to_array; |
| 37 | if ($fields->[0]) { |
| 38 | $meta->fields($fields); |
| 39 | }; |
| 40 | |
| 41 | # Set meta |
| 42 | $koral->meta($meta); |
| 43 | |
| 44 | # Get segment index |
| 45 | my $index = $c->index->segment; |
| 46 | |
| 47 | # Prepare query on index |
| 48 | $c->render(json => $koral->to_result($index)); |
| 49 | }; |
| 50 | |
| Akron | 61e8bce | 2017-05-24 15:55:27 +0200 | [diff] [blame] | 51 | |
| 52 | # Get information per text |
| 53 | sub text { |
| 54 | my $self = shift; |
| 55 | |
| 56 | my $koral = Krawfish::Koral->new; |
| 57 | my $meta = $koral->meta_builder; |
| 58 | |
| 59 | my $v = $c->validation; |
| 60 | $v->optional('fields'); |
| 61 | |
| 62 | |
| 63 | # Get the text sigle from the stash |
| 64 | my $corpus_id = $c->stash('corpus_id'); |
| 65 | my $doc_id = $c->stash('doc_id'); |
| 66 | my $text_id = $c->stash('text_id'); |
| 67 | |
| 68 | my $sigle = join('/', $corpus_id, $doc_id, $text_id); |
| 69 | |
| 70 | # Set corpus |
| 71 | $koral->corpus( |
| 72 | $koral->corpus_builder->string('text_sigle' => $text_sigle) |
| 73 | ); |
| 74 | |
| 75 | # Get the field information |
| 76 | my $fields = b($v->param('fields'))->split(',')->uniq->to_array; |
| 77 | if ($fields->[0]) { |
| 78 | $meta->fields($fields); |
| 79 | }; |
| 80 | |
| 81 | # Limit to a single match |
| 82 | $meta->limit(1); |
| 83 | |
| 84 | # Set meta |
| 85 | $koral->meta($meta); |
| 86 | |
| 87 | # Get segment index |
| 88 | my $index = $c->index->segment; |
| 89 | |
| 90 | # Prepare query on index |
| 91 | $c->render(json => $koral->to_result($index)); |
| 92 | }; |
| 93 | |
| 94 | |
| Akron | b5809f4 | 2017-05-03 01:26:08 +0200 | [diff] [blame] | 95 | 1; |