summaryrefslogtreecommitdiff
path: root/web/lib/api/v4/projects.rb
blob: 2cbb7fbf3d836d76e481b81a6fbfdb9612c0f466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# web/lib/api/v4/projects.rb
class Taginfo < Sinatra::Base

    api(4, 'projects/all', {
        :description => 'Get list of all projects using OSM tags known to taginfo.',
        :parameters => { :status => 'Only show projects with given status (default is "OK")', :query => 'Only show projects matching this query (substring match, optional).' },
        :paging => :optional,
        :sort => %w( name ),
        :result => paging_results([
            [:id,          :STRING, 'Project id'],
            [:name,        :STRING, 'Project name'],
            [:url,         :STRING, 'Project URL'],
            [:description, :STRING, 'Project description']
        ]),
        :example => { :page => 1, :rp => 10, :sortname => 'name', :sortorder => 'asc' },
        :ui => '/projects'
    }) do
        if params[:status]
            status = params[:status]
        else
            status = 'OK'
        end

        q = like_contains(params[:query])
        total = @db.count('projects.projects').
            condition("fetch_result=?", status).
            condition_if("name LIKE ? ESCAPE '@' OR description LIKE ? ESCAPE '@'", q, q).
            get_first_value().to_i

        res = @db.select('SELECT * FROM projects.projects').
            condition("fetch_result=?", status).
            condition_if("name LIKE ? ESCAPE '@' OR description LIKE ? ESCAPE '@'", q, q).
            order_by(@ap.sortname, @ap.sortorder) { |o|
                o.name 'lower(name)'
            }.
            paging(@ap).
            execute()

        return JSON.generate({
            :page  => @ap.page,
            :rp    => @ap.results_per_page,
            :total => total,
            :url   => request.url,
            :data  => res.map{ |row| {
                :id          => row['id'],
                :name        => row['name'],
                :url         => row['project_url'],
                :description => row['description'],
            }}
        }, json_opts(params[:format]))
    end

end