Skip to content

Commit

Permalink
Merge branch 'revert-851-cleanup_route_ajax' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Mar 4, 2020
2 parents 88c7975 + 02389e4 commit e8b060d
Showing 1 changed file with 27 additions and 75 deletions.
102 changes: 27 additions & 75 deletions lib/LibreCat/App/Search/Route/ajax.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@ LibreCat::App::Search::Route::ajax - handles routes for asynchronous requests
=cut

use Catmandu::Sane;
use Catmandu::Util qw(join_path :is);
use Catmandu::Util qw(join_path);
use Dancer qw/:syntax/;
use Dancer::Plugin::Ajax;
use HTML::Entities;
use LibreCat::App::Helper;
use LibreCat qw(searcher);

=head2 AJAX /search_publication
=head2 AJA /search_publication
Ajax route for autocomplete feature in forms.
=cut

ajax '/search_publication' => sub {
my $limit = length(params->{term}) ? 10 : 1000;

my $term = get_term();

return to_json([]) unless defined($term);

my $limit = h->config->{default_page_size} // 10;

my @terms = split_term( $term );
my @terms = split(' ', params->{term});
$terms[-1] .= "*" if @terms;
my @cql_parts = map {"(basic all \"$_\")"} @terms;

Expand All @@ -45,9 +40,9 @@ ajax '/search_publication' => sub {

my $hits = h->publication->search(%search_params);

h->log->debug($hits->total . " hits");
h->log->debug($hits->{total} . " hits");

if ($hits->total) {
if ($hits->{total}) {
my @map = map {
my $author = $_->{author}->[0]->{full_name}
// $_->{editor}->[0]->{full_name}
Expand All @@ -71,17 +66,10 @@ ajax '/search_publication' => sub {
=cut

ajax '/search_researcher' => sub {

my $term = get_term();

return to_json([]) unless defined($term);

my $cql;
push @$cql, $term;
push @$cql, params->{'term'};

my $limit = h->config->{default_page_size} // 10;

my %search_params = (cql => $cql, limit => $limit,
my %search_params = (cql => $cql, limit => 100,
sort => h->config->{default_person_sort});
h->log->debug("executing user->search: " . to_dumper(\%search_params));

Expand All @@ -95,7 +83,7 @@ ajax '/search_researcher' => sub {
=cut

ajax '/authority_user/:id' => sub {
my $person = h->get_person(params("route")->{id}) || {error => "No user found."};
my $person = h->get_person(params->{id}) || {error => "No user found."};
to_json $person;
};

Expand All @@ -104,34 +92,25 @@ ajax '/authority_user/:id' => sub {
=cut

ajax '/get_alias/:id/:alias' => sub {
my $rparams = params("route");
my $term = $rparams->{'alias'};
my $id = $rparams->{'id'};
my $term = params->{'alias'} || "";
my $id = params->{'id'};

my %search_params = (
cql_query => "alias=$term AND id<>$id",
limit => 0
);
my %search_params = (cql => ["alias=$term", "id<>$id"]);
h->log->debug("executing user->search: " . to_dumper(\%search_params));

my $hits = h->user()->search(%search_params);
my $hits = searcher->search('user', \%search_params);

return to_json {ok => $hits->total ? 0 : 1};
return to_json {ok => $hits->{total} ? 0 : 1};
};

=head2 AJAX /get_project
=cut

ajax '/get_project' => sub {
my $limit = length(params->{term}) ? 100 : 1000;

my $term = get_term();

return to_json([]) unless defined($term);

my $limit = h->config->{default_page_size} // 10;

my @terms = split_term( $term );
my @terms = split(' ', params->{term});
$terms[-1] .= "*" if @terms;
my @cql_parts = map {"(basic all \"$_\")"} @terms;

Expand All @@ -144,9 +123,9 @@ ajax '/get_project' => sub {

my $hits = h->project->search(%search_params);

h->log->debug($hits->total . " hits");
h->log->debug($hits->{total} . " hits");

if ($hits->total) {
if ($hits->{total}) {
my $map;
@$map
= map {{id => $_->{_id}, label => $_->{name}};} @{$hits->{hits}};
Expand All @@ -162,13 +141,10 @@ ajax '/get_project' => sub {
=cut

ajax '/get_department' => sub {
my $term = get_term();
my $term = params->{term} // '';
my $limit = length($term) ? 100 : 1000;

return to_json([]) unless defined($term);

my $limit = h->config->{default_page_size} // 10;

my @terms = split_term( $term );
my @terms = split('\s', $term);
$terms[-1] .= "*" if @terms;
my @cql_parts = map {"(name_lookup all \"$_\")"} @terms;

Expand All @@ -185,9 +161,9 @@ ajax '/get_department' => sub {

my $hits = h->department->search(%search_params);

h->log->debug($hits->total . " hits");
h->log->debug($hits->{total} . " hits");

if ($hits->total) {
if ($hits->{total}) {
my $map;
@$map = map {{id => $_->{_id}, label => $_->{display}};}
@{$hits->{hits}};
Expand All @@ -203,14 +179,9 @@ ajax '/get_department' => sub {
=cut

ajax '/get_research_group' => sub {
my $limit = length(params->{term}) ? 100 : 1000;

my $term = get_term();

return to_json([]) unless defined($term);

my $limit = h->config->{default_page_size} // 10;

my @terms = split_term( $term );
my @terms = split(' ', params->{term});
$terms[-1] .= "*" if @terms;
my @cql_parts = map {"(name_lookup all \"$_\")"} @terms;

Expand All @@ -224,9 +195,9 @@ ajax '/get_research_group' => sub {

my $hits = h->research_group->search(%search_params);

h->log->debug($hits->total . " hits");
h->log->debug($hits->{total} . " hits");

if ($hits->total) {
if ($hits->{total}) {
my $map;
@$map
= map {{id => $_->{_id}, label => $_->{name}};} @{$hits->{hits}};
Expand All @@ -237,23 +208,4 @@ ajax '/get_research_group' => sub {
}
};

sub get_term {

my $qparams = params("query");

my $term = is_string( $qparams->{term} ) ?
$qparams->{term} :
is_array_ref( $qparams->{term} ) ? $qparams->{term}->[0] : "";

$term =~ s/^\s+//o;
$term =~ s/\s+$//o;

length($term) ? $term : undef;

}

sub split_term {
split( /\s+/o, $_[0] );
}

1;

0 comments on commit e8b060d

Please sign in to comment.