Added support for meta data endpoint

Change-Id: Id471476f381ea2bdc1234c1cfc0c845ab5d38546
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index 34cfadc..c99f86f 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -185,9 +185,8 @@
   # Match route
   my $corpus = $r->route('/corpus/:corpus_id');
   my $doc    = $corpus->get('/:doc_id');
-  my $text   = $doc->get('/:text_id');
-  my $match  = $text->get('/:match_id');
-  $match->to('search#match_info')->name('match');
+  my $text   = $doc->get('/:text_id')->to('search#text_info')->name('text');
+  my $match  = $doc->get('/:text_id/:match_id')->to('search#match_info')->name('match');
 
   # User Management
   my $user = $r->any('/user')->to(controller => 'User');
diff --git a/lib/Kalamar/API.pm b/lib/Kalamar/API.pm
index 29e8ebf..d48a660 100644
--- a/lib/Kalamar/API.pm
+++ b/lib/Kalamar/API.pm
@@ -7,6 +7,13 @@
 
 # KorAP Search engine for Mojolicious::Plugin::Search
 
+
+# TODO:
+#   This contains a lot of legacy code that is not
+#   necessary anymore. Quite a lot of endpoints are
+#   only proxies and should simply be piped through
+#   the backend (including auth code)
+
 # TODO: Add fixtures
 # TODO: Support search in corpus and virtualcollection
 # TODO: Support caching everywhere!
@@ -230,6 +237,52 @@
 };
 
 
+# Get text info
+sub text {
+  my $self = shift;
+  my $index = shift;
+
+  # Get controller
+  my $c = $index->controller;
+
+  # If there is a callback, do async
+  my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE';
+
+  my %param = @_;
+
+  my $url = Mojo::URL->new($index->api);
+
+  # Use hash slice to create path
+  $url->path(join('/', 'corpus', @param{qw/corpus_id doc_id text_id/}));
+
+  my %query;
+  $query{fields} = $param{fields};
+
+  # Add query
+  $url->query(\%query);
+
+  $c->app->log->debug('Text info: ' . $url);
+
+  # non-blocking
+  if ($cb) {
+    $c->user->auth_request(
+      get =>
+        $url => sub {
+          my $tx = pop;
+          $self->_process_response('text', $index, $tx);
+          weaken $index;
+          return $cb->($index);
+        });
+  }
+
+  # Text info blocking
+  else {
+    my $tx = $c->user->auth_request(get => $url);
+    return $self->_process_response('text', $index, $tx);
+  };
+};
+
+
 # Get resource information
 sub resource {
   my $self = shift;
@@ -335,6 +388,9 @@
     }
     elsif ($type eq 'resource') {
       $self->_process_response_resource($index, $json);
+    }
+    elsif ($type eq 'text') {
+      $self->_process_response_text($index, $json);
     };
 
     return 1 if ref $json ne 'HASH';
@@ -425,6 +481,13 @@
 };
 
 
+# Process query serialization response
+sub _process_response_text {
+  my ($self, $index, $json) = @_;
+  $index->results($json);
+};
+
+
 # Process trace response
 sub _process_response_trace {
   my ($self, $index, $json) = @_;
diff --git a/lib/Kalamar/Controller/Search.pm b/lib/Kalamar/Controller/Search.pm
index 50c0d8b..3045bb5 100644
--- a/lib/Kalamar/Controller/Search.pm
+++ b/lib/Kalamar/Controller/Search.pm
@@ -80,6 +80,46 @@
 };
 
 
+# Get meta data of a text
+sub text_info {
+  my $c = shift;
+
+  my %query = (fields => '*');
+  if ($c->param('fields')) {
+    $query{fields} = $c->param('fields')
+  };
+
+  $c->render_later;
+
+  # Use the API for fetching matching information non-blocking
+  $c->search->text(
+    corpus_id => $c->stash('corpus_id'),
+    doc_id    => $c->stash('doc_id'),
+    text_id   => $c->stash('text_id'),
+    %query,
+
+    # Callback for async search
+    sub {
+      my $index = shift;
+      return $c->respond_to(
+
+        # Render json if requested
+        json => sub {
+          # Add notifications to the matching json
+          # TODO: There should be a special notification engine doing that!
+          my $notes = $c->notifications(json => $index->results->[0]);
+          $c->render(
+            json => $notes,
+            status => $index->status
+          );
+        }
+      );
+    }
+  );
+};
+
+
+
 # Get information about a match
 # Note: This is called 'match_info' as 'match' is reserved
 sub match_info {