Added corpusinfo endpoint

Change-Id: I81a24670e437dad5ac449566eac9460a0819ef18
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index 31fcb47..d8e1386 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -229,6 +229,7 @@
   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');
 
+  $r->get('/corpus2')->to('Search2#corpus_info')->name('corpus');
   $r->route('/corpus2/:corpus_id/:doc_id/:text_id/:match_id')->to('search2#match_info')->name('match');
 
   # User Management
diff --git a/lib/Kalamar/Controller/Search2.pm b/lib/Kalamar/Controller/Search2.pm
index 944b7a4..c7e9736 100644
--- a/lib/Kalamar/Controller/Search2.pm
+++ b/lib/Kalamar/Controller/Search2.pm
@@ -12,6 +12,9 @@
 # TODO:
 #   Support server timing API
 
+# TODO:
+#   Add match_info template for HTML
+
 # Query endpoint
 sub query {
   my $c = shift;
@@ -277,8 +280,7 @@
     sub {
       return $c->catch_errors_and_warnings(shift);
     }
-  )
-  ->then(
+  )->then(
     sub {
       my $json = shift;
 
@@ -294,6 +296,79 @@
       return $json;
     }
   )
+
+  # Deal with errors
+  ->catch(
+    sub {
+      return $c->render(
+        json => $c->notifications('json')
+      )
+    }
+  )
+
+  # Start IOLoop
+  ->wait;
+
+  return 1;
+};
+
+
+# Get information about
+# This replaces the collections endpoint
+sub corpus_info {
+  my $c = shift;
+
+  # Input validation
+  my $v = $c->validation;
+  $v->optional('cq');
+
+  # Async
+  $c->render_later;
+
+  my $url = Mojo::URL->new($c->korap->api);
+
+  # Use hash slice to create path
+  $url->path('statistics');
+
+  # Add query
+  $url->query(corpusQuery => $v->param('cq'));
+
+  # Set stash
+  $c->stash('search._resource_cache' => $url->to_string);
+
+  $c->app->log->debug("Statistics info: $url");
+
+  # non-blocking
+  $c->user->auth_request_p(get => $url)->then(
+    sub {
+      return $c->catch_errors_and_warnings(shift);
+    }
+  )->then(
+    sub {
+      my $json = shift;
+
+      # TODO: CACHING!!!
+      # my $user = $c->stash('user') // 'not_logged_in';
+      # $c->stash('search.resource' => $json);
+      # $c->chi->set($user . $c->stash('search._resource_cache') => $json, '24 hours');
+
+      # $self->_process_response('resource', $index, $tx);
+
+      return $json;
+    }
+  )
+
+  # Process response
+  ->then(
+    sub {
+      my $json = shift;
+      return $c->render(
+        json => $c->notifications(json => $json),
+        status => 200
+      );
+    }
+  )
+
   # Deal with errors
   ->catch(
     sub {
diff --git a/t/corpus_info.t b/t/corpus_info.t
new file mode 100644
index 0000000..e245422
--- /dev/null
+++ b/t/corpus_info.t
@@ -0,0 +1,44 @@
+use Mojo::Base -strict;
+use Test::Mojo;
+use Test::More;
+use Mojo::File qw/path/;
+
+
+#####################
+# Start Fake server #
+#####################
+my $mount_point = '/api/';
+$ENV{KALAMAR_API} = $mount_point;
+
+my $t = Test::Mojo->new('Kalamar');
+
+# Mount fake backend
+# Get the fixture path
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $fake_backend = $t->app->plugin(
+  Mount => {
+    $mount_point =>
+      $fixtures_path->child('fake_backend.pl')
+  }
+);
+# Configure fake backend
+$fake_backend->pattern->defaults->{app}->log($t->app->log);
+
+# Query passed
+$t->get_ok('/corpus2')
+  ->status_is(200)
+  ->json_is('/documents', 11)
+  ->json_is('/tokens', 665842)
+  ->json_is('/sentences', 25074)
+  ->json_is('/paragraphs', 772)
+  ;
+
+$t->get_ok('/corpus2?cq=docSigle+%3D+\"GOE/AGA\"')
+  ->json_is('/documents', 5)
+  ->json_is('/tokens', 108557)
+  ->json_is('/sentences', 3835)
+  ->json_is('/paragraphs', 124)
+  ;
+
+done_testing;
+__END__
diff --git a/t/fixtures/fake_backend.pl b/t/fixtures/fake_backend.pl
index a49b05f..89ad3c3 100644
--- a/t/fixtures/fake_backend.pl
+++ b/t/fixtures/fake_backend.pl
@@ -125,6 +125,23 @@
 };
 
 
+# Statistics endpoint
+get '/statistics' => sub {
+  my $c = shift;
+  my $v = $c->validation;
+  $v->optional('corpusQuery');
+
+  my @list = 'corpusinfo';
+  if ($v->param('corpusQuery')) {
+    push @list, $v->param('corpusQuery');
+  };
+  my $slug = slugify(join('_', @list));
+
+  # Get response based on query parameter
+  my $response = $c->load_response($slug);
+  return $c->render(%$response);
+};
+
 ############
 # Auth API #
 ############
diff --git a/t/fixtures/response_corpusinfo.json b/t/fixtures/response_corpusinfo.json
new file mode 100644
index 0000000..1918da5
--- /dev/null
+++ b/t/fixtures/response_corpusinfo.json
@@ -0,0 +1,9 @@
+{
+  "status" : 200,
+  "json" : {
+    "documents":11,
+    "tokens":665842,
+    "sentences":25074,
+    "paragraphs":772
+  }
+}
diff --git a/t/fixtures/response_corpusinfo_docsigle-goeaga.json b/t/fixtures/response_corpusinfo_docsigle-goeaga.json
new file mode 100644
index 0000000..9a65412
--- /dev/null
+++ b/t/fixtures/response_corpusinfo_docsigle-goeaga.json
@@ -0,0 +1,9 @@
+{
+  "status" : 200,
+  "json" : {
+    "documents":5,
+    "tokens":108557,
+    "sentences":3835,
+    "paragraphs":124
+  }
+}