Skip to content

Commit

Permalink
Merge pull request #191 from avantifellows/feature/session-filters
Browse files Browse the repository at this point in the history
add: session filters
  • Loading branch information
Bahugunajii authored Jul 11, 2024
2 parents 01243a6 + 1115ae4 commit f982dce
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/dbservice_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,23 @@ defmodule DbserviceWeb.SessionController do
apply_is_quiz_filter(value, 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 Down

0 comments on commit f982dce

Please sign in to comment.