-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathquery.ex
More file actions
98 lines (72 loc) · 2.76 KB
/
query.ex
File metadata and controls
98 lines (72 loc) · 2.76 KB
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
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
89
90
91
92
93
94
95
96
97
98
defmodule CodeCorps.Helpers.Query do
import CodeCorps.Helpers.String, only: [coalesce_id_string: 1, coalesce_string: 1]
import Ecto.Query, only: [where: 3, limit: 2, order_by: 2]
def id_filter(query, id_list) do
ids = id_list |> coalesce_id_string
query |> where([object], object.id in ^ids)
end
# skill queries
def limit_filter(query, %{"limit" => count}) do
query |> limit(^count)
end
def limit_filter(query, _), do: query
def title_filter(query, %{"query" => title}) do
query |> where([object], ilike(object.title, ^"%#{title}%"))
end
def title_filter(query, _), do: query
# end skill queries
# task queries
def project_id_with_number_filter(query, %{"id" => number, "project_id" => project_id}) do
query |> where([object], object.number == ^number and object.project_id == ^project_id)
end
def project_id_with_number_filter(query, _), do: query
def task_list_id_with_number_filter(query, %{"id" => number, "task_list_id" => task_list_id}) do
query |> where([object], object.number == ^number and object.task_list_id == ^task_list_id)
end
def task_list_id_with_number_filter(query, _), do: query
def project_filter(query, %{"project_id" => project_id}) do
query |> where([object], object.project_id == ^project_id)
end
def project_filter(query, _), do: query
def task_list_filter(query, %{"task_list_ids" => task_list_ids}) do
task_list_ids = task_list_ids |> coalesce_id_string
query |> where([object], object.task_list_id in ^task_list_ids)
end
def task_list_filter(query, _), do: query
def task_type_filter(query, %{"task_type" => task_type_list}) do
task_types = task_type_list |> coalesce_string
query |> where([object], object.task_type in ^task_types)
end
def task_type_filter(query, _), do: query
def task_status_filter(query, %{"status" => status}) do
query |> where([object], object.status == ^status)
end
def task_status_filter(query, _), do: query
# end task queries
# def comment queries
def task_filter(query, task_id) do
query |> where([object], object.task_id == ^task_id)
end
# end comment queries
# user queries
def user_filter(query, %{"query" => query_string}) do
query
|> where(
[object],
ilike(object.first_name, ^"%#{query_string}%") or
ilike(object.last_name, ^"%#{query_string}%") or
ilike(object.username, ^"%#{query_string}%")
)
end
def user_filter(query, _), do: query
# end user queries
# sorting
def sort_by_newest_first(query), do: query |> order_by([desc: :inserted_at])
def sort_by_order(query), do: query |> order_by([asc: :order])
# end sorting
# finders
def slug_finder(query, slug) do
query |> CodeCorps.Repo.get_by(slug: slug |> String.downcase)
end
# end finders
end