From 950825239f59fbdc16c9635bfeda7293ead59784 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 25 Aug 2019 19:57:34 +0200 Subject: [PATCH] resource_type meta for researcher, provider, repository --- app/controllers/concerns/countable.rb | 24 ++++++++++++++++------ app/controllers/providers_controller.rb | 6 +++++- app/controllers/repositories_controller.rb | 4 +++- app/controllers/researchers_controller.rb | 4 +++- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/controllers/concerns/countable.rb b/app/controllers/concerns/countable.rb index 39bcfdb79..c7f4644e3 100644 --- a/app/controllers/concerns/countable.rb +++ b/app/controllers/concerns/countable.rb @@ -14,12 +14,8 @@ def doi_count(client_id: nil, provider_id: nil, consortium_id: nil, researcher_i else response = Doi.query(nil, page: { number: 1, size: 0 }) end - - if researcher_id - response.results.total > 0 ? facet_by_cumulative_year(response.response.aggregations.created.buckets) : [] - else - response.results.total > 0 ? facet_by_year(response.response.aggregations.created.buckets) : [] - end + + response.results.total > 0 ? facet_by_year(response.response.aggregations.created.buckets) : [] end # cumulative count clients by year @@ -56,5 +52,21 @@ def provider_count(consortium_id: nil) response.results.total > 0 ? facet_by_cumulative_year(response.response.aggregations.cumulative_years.buckets) : [] end end + + def resource_type_count(client_id: nil, provider_id: nil, consortium_id: nil, researcher_id: nil, state: nil) + if client_id + response = Doi.query(nil, client_id: client_id, page: { number: 1, size: 0 }) + elsif provider_id + response = Doi.query(nil, provider_id: provider_id, page: { number: 1, size: 0 }) + elsif consortium_id + response = Doi.query(nil, consortium_id: consortium_id, page: { number: 1, size: 0 }) + elsif researcher_id + response = Doi.query(nil, researcher_id: researcher_id, state: state, page: { number: 1, size: 0 }) + else + response = Doi.query(nil, page: { number: 1, size: 0 }) + end + + response.results.total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.buckets) : [] + end end end diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 4036f96e8..635b354aa 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -156,21 +156,25 @@ def show providers = provider_count(consortium_id: nil) clients = client_count(provider_id: nil) dois = doi_count(provider_id: nil) + resource_types = resource_type_count(provider_id: nil) elsif @provider.member_type == "consortium_member" providers = provider_count(consortium_id: params[:id]) clients = client_count(consortium_id: params[:id]) dois = doi_count(consortium_id: params[:id]) + resource_types = resource_type_count(consortium_id: params[:id]) else providers = nil clients = client_count(provider_id: params[:id]) dois = doi_count(provider_id: params[:id]) + resource_types = resource_type_count(provider_id: params[:id]) end options = {} options[:meta] = { providers: providers, clients: clients, - dois: dois }.compact + dois: dois, + "resourceTypes" => resource_types }.compact options[:include] = @include options[:is_collection] = false options[:params] = { current_ability: current_ability } diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 9640ad2a3..deb342897 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -98,7 +98,9 @@ def show fail ActiveRecord::RecordNotFound unless repository.present? options = {} - options[:meta] = { dois: doi_count(client_id: params[:id]) } + options[:meta] = { + dois: doi_count(client_id: params[:id]), + "resourceTypes" => resource_type_count(client_id: params[:id]) }.compact options[:include] = @include options[:is_collection] = false options[:params] = { current_ability: current_ability } diff --git a/app/controllers/researchers_controller.rb b/app/controllers/researchers_controller.rb index 408235df3..21114c08b 100644 --- a/app/controllers/researchers_controller.rb +++ b/app/controllers/researchers_controller.rb @@ -65,7 +65,9 @@ def show state = current_user.nil? || current_user.role_id == "user" ? "findable" : nil options = {} - options[:meta] = { dois: doi_count(researcher_id: params[:id], state: state) } + options[:meta] = { + dois: doi_count(researcher_id: params[:id], state: state), + "resourceTypes" => resource_type_count(researcher_id: params[:id], state: state) }.compact options[:is_collection] = false render json: ResearcherSerializer.new(@researcher, options).serialized_json, status: :ok end