diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb new file mode 100644 index 00000000..5f5c03ef --- /dev/null +++ b/app/controllers/api/v1/api_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class ApiController + end + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/attribute_fields_controller.rb b/app/controllers/api/v1/attribute_fields_controller.rb new file mode 100644 index 00000000..245dec28 --- /dev/null +++ b/app/controllers/api/v1/attribute_fields_controller.rb @@ -0,0 +1,23 @@ +module Api + module V1 + class AttributeFieldsController < ApiController + def suggest + category_label = params[:category] + entity_type = params.fetch(:entity_type, '').downcase + + category_query = AttributeCategory.where(label: category_label) + category_query = category_query.where(entity_type: entity_type) unless entity_type.empty? + + suggestions = AttributeField.where(attribute_category_id: category_query.pluck(:id)) + .group(:label) + .order('count_id DESC') + .limit(50) + .count(:id) + .reject { |k, v| v < 10 } + .keys + + render json: suggestions.to_json + end + end + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 4a7625ea..627bd49a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -177,6 +177,14 @@ end end # API Endpoints + namespace :api do + namespace :v1 do + scope '/fields' do + get '/suggest', to: 'attribute_fields#suggest' + end + end + end + scope '/generate' do # General information