Skip to content

Commit

Permalink
make filter general
Browse files Browse the repository at this point in the history
  • Loading branch information
Drish-xD committed Jul 11, 2024
1 parent ff8119e commit 1115ae4
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions lib/dbservice_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ defmodule DbserviceWeb.SessionController do
:is_quiz ->
apply_is_quiz_filter(value, acc)

:group ->
apply_meta_data_filter(value, key, acc)

:parent_id ->
apply_meta_data_filter(value, key, acc)

:batch_id ->
apply_meta_data_filter(value, key, acc)

atom ->
from(u in acc, where: field(u, ^atom) == ^value)
apply_filter_based_on_schema(atom, key, value, acc)
end
end)

session = Repo.all(query)
render(conn, "index.json", session: session)
end

defp apply_filter_based_on_schema(atom, key, value, acc) do
if atom in Session.__schema__(:fields) do
from(u in acc, where: field(u, ^atom) == ^value)
else
from u in acc,
where: fragment("?->>? = ?", u.meta_data, ^key, ^value)
end
end

defp extract_sort_order(params) do
case params["sort_order"] do
"asc" -> :asc
Expand All @@ -109,11 +109,6 @@ defmodule DbserviceWeb.SessionController do
end
end

defp apply_meta_data_filter(value, field, acc) do
from u in acc,
where: fragment("?->>? = ?", u.meta_data, ^field, ^value)
end

swagger_path :create do
post("/api/session")

Expand Down

0 comments on commit 1115ae4

Please sign in to comment.