41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/query-interface-server/resource.rb', line 41
def query
default_data = {"conditions" => [], "with" => [], "order" => [] }
if query_data = params[:query_data]
unless query_data.is_a?(Hash)
query_data = JSON.parse(query_data)
end
query_data = default_data.merge(query_data)
@instances = query_model.filter.select_all(query_model.table_name)
query_data["conditions"].each do |key, value|
@instances = query_send("filter_#{key}", value)
end
query_data["with"].each do |field|
@instances = query_send("with_#{field}")
end
order = query_data['order'] << "#{query_model.table_name}__id"
order_query(order)
case query_data["mode"].to_sym
when :evaluate
@result = @instances
when :count
@result = {count: @instances.count}
when :ids
id_selector = "#{query_model.table_name}__id".to_sym
@result = @instances.select(id_selector).map(&:id)
when :paginate
page = query_data["page"].to_i
per_page = query_data["per_page"].to_i
@result = {total: @instances.count, objects: @instances.paginate(page, per_page)}
when :first
@result = @instances.first
when :last
@result = @instances.last
else
return head :unprocessable_entity
end
if @result
respond_with(@result)
else
head :not_found
end
else
head :unprocessable_entity
end
end
|