Last active
August 3, 2018 01:39
-
-
Save elbow-jason/9ed7b0a628a081c9e9c2db56235f6f95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Bmgr.Transaction.SearchQuery do | |
import Ecto.Query | |
alias Bmgr.{Transaction} | |
@filters [:account, :status] | |
def build(filter_params) do | |
do_filter(Transaction, @filters, filter_params) | |
end | |
defp do_filter(query, filters, params) when is_list(filters) do | |
Enum.reduce(filters, query, fn (filter, acc) -> | |
do_filter(acc, filter, params) | |
end) | |
end | |
defp do_filter(query, :account, %{"account" => value}) do | |
from(t in query, preload: [entries: from(e in Entry, where: e.account_id == ^value)]) | |
end | |
defp do_filter(query, :status, %{"status" => value}) do | |
from(t in query, where: t.status == ^value) | |
end | |
defp do_filter(query, _, _) do | |
query | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment