diff options
author | Jochen Topf <jochen@topf.org> | 2014-09-14 16:02:00 +0200 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2014-09-14 16:02:00 +0200 |
commit | ac488e8fa2797b32b75b7e0879ed29547c60b589 (patch) | |
tree | 4ba1a914a8641b1f60aee762c24ea93759185c90 /web/lib | |
parent | 3d9bda39eab56852a42312e876d02d32de0f55e0 (diff) | |
download | taginfo-ac488e8fa2797b32b75b7e0879ed29547c60b589.tar taginfo-ac488e8fa2797b32b75b7e0879ed29547c60b589.tar.gz |
Add filtering to key/tag project output.
Diffstat (limited to 'web/lib')
-rw-r--r-- | web/lib/api/v4/key.rb | 14 | ||||
-rw-r--r-- | web/lib/api/v4/tag.rb | 14 |
2 files changed, 28 insertions, 0 deletions
diff --git a/web/lib/api/v4/key.rb b/web/lib/api/v4/key.rb index f53376e..83d09fd 100644 --- a/web/lib/api/v4/key.rb +++ b/web/lib/api/v4/key.rb @@ -330,6 +330,12 @@ class Taginfo < Sinatra::Base :query => 'Only show results where the value matches this query (substring match, optional).' }, :paging => :optional, + :filter => { + :all => { :doc => 'No filter.' }, + :nodes => { :doc => 'Only values on tags used on nodes.' }, + :ways => { :doc => 'Only values on tags used on ways.' }, + :relations => { :doc => 'Only values on tags used on relations.' } + }, :sort => %w( project_name tag ), :result => paging_results([ [:project_id, :STRING, 'Project ID'], @@ -350,16 +356,24 @@ class Taginfo < Sinatra::Base }) do key = params[:key] q = like_contains(params[:query]) + filter_type = get_filter() + total = @db.select('SELECT count(*) FROM projects.projects p, projects.project_tags t ON p.id=t.project_id'). condition("status = 'OK'"). condition('key = ?', key). condition_if("value LIKE ? ESCAPE '@' OR name LIKE ? ESCAPE '@'", q, q). + condition_if("on_node = ?", filter_type == 'nodes' ? 1 : ''). + condition_if("on_way = ? OR on_area = 1", filter_type == 'ways' ? 1 : ''). + condition_if("on_relation = ? OR on_area = 1", filter_type == 'relations' ? 1 : ''). get_first_value().to_i res = @db.select('SELECT t.project_id, p.name, p.icon_url AS project_icon_url, t.key, t.value, t.description, t.doc_url, t.icon_url, t.on_node, t.on_way, t.on_relation, t.on_area FROM projects.projects p, projects.project_tags t ON p.id=t.project_id'). condition("status = 'OK'"). condition('key = ?', key). condition_if("value LIKE ? ESCAPE '@' OR name LIKE ? ESCAPE '@'", q, q). + condition_if("on_node = ?", filter_type == 'nodes' ? 1 : ''). + condition_if("on_way = ? OR on_area = 1", filter_type == 'ways' ? 1 : ''). + condition_if("on_relation = ? OR on_area = 1", filter_type == 'relations' ? 1 : ''). order_by(@ap.sortname, @ap.sortorder) { |o| o.project_name 'lower(p.name)' o.project_name :value diff --git a/web/lib/api/v4/tag.rb b/web/lib/api/v4/tag.rb index d46f03c..b6fdbbe 100644 --- a/web/lib/api/v4/tag.rb +++ b/web/lib/api/v4/tag.rb @@ -256,6 +256,12 @@ class Taginfo < Sinatra::Base :query => 'Only show results where the value matches this query (substring match, optional).' }, :paging => :optional, + :filter => { + :all => { :doc => 'No filter.' }, + :nodes => { :doc => 'Only values on tags used on nodes.' }, + :ways => { :doc => 'Only values on tags used on ways.' }, + :relations => { :doc => 'Only values on tags used on relations.' } + }, :sort => %w( project_name tag ), :result => paging_results([ [:project_id, :STRING, 'Project ID'], @@ -276,17 +282,25 @@ class Taginfo < Sinatra::Base }) do key = params[:key] value = params[:value] + filter_type = get_filter() q = like_contains(params[:query]) + total = @db.select('SELECT count(distinct project_id) FROM projects.projects p, projects.project_tags t ON p.id=t.project_id'). condition("status = 'OK'"). condition('key = ?', key). condition('value = ? OR VALUE IS NULL', value). condition_if("value LIKE ? ESCAPE '@' OR name LIKE ? ESCAPE '@'", q, q). + condition_if("on_node = ?", filter_type == 'nodes' ? 1 : ''). + condition_if("on_way = ? OR on_area = 1", filter_type == 'ways' ? 1 : ''). + condition_if("on_relation = ? OR on_area = 1", filter_type == 'relations' ? 1 : ''). get_first_value().to_i res = @db.select("SELECT p.name, p.icon_url AS project_icon_url, t.* FROM (SELECT project_id, key, MAX(value) AS value FROM projects.project_tags WHERE key=? AND (value=? OR value IS NULL) GROUP BY project_id, key) AS s JOIN projects.project_tags t JOIN projects.projects p ON p.id=t.project_id AND t.project_id=s.project_id AND t.key=s.key AND (t.value=s.value OR (t.value IS NULL AND s.value IS NULL))", key, value). condition("p.status = 'OK'"). condition_if("value LIKE ? ESCAPE '@' OR name LIKE ? ESCAPE '@'", q, q). + condition_if("on_node = ?", filter_type == 'nodes' ? 1 : ''). + condition_if("on_way = ? OR on_area = 1", filter_type == 'ways' ? 1 : ''). + condition_if("on_relation = ? OR on_area = 1", filter_type == 'relations' ? 1 : ''). order_by(@ap.sortname, @ap.sortorder) { |o| o.project_name 'lower(p.name)' o.project_name :value |