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 {