Skip to content

Instantly share code, notes, and snippets.

@mazz
Created May 2, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazz/ed535fba7bf738116d2e15ab705f78b9 to your computer and use it in GitHub Desktop.
Save mazz/ed535fba7bf738116d2e15ab705f78b9 to your computer and use it in GitHub Desktop.
# ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size
def search(query_string, offset \\ 0, limit \\ 0,
media_category,
playlist_uuid,
channel_uuid,
published_after
) do
conditions = false
conditions =
if media_category do
dynamic([mi], mi.media_category == ^media_category or ^conditions)
else
conditions
end
conditions =
if published_after do
{:ok, datetime} = DateTime.from_unix(published_after, :second)
erl_datetime = NaiveDateTime.to_erl(datetime)
Logger.info("published_after: #{published_after}")
Logger.info("datetime: #{datetime}")
Logger.info("erl_datetime: #{erl_datetime}")
dynamic([mi], where: mi.updated_at >= ^erl_datetime or ^conditions)
else
conditions
end
Logger.info("media_category: #{media_category}")
query = if conditions do
Ecto.Query.from(mi in MediaItem,
where: ^conditions
)
else
Ecto.Query.from(mi in MediaItem)
end
MediaItemsSearch.run(query, query_string)
|> Repo.paginate(page: offset, page_size: limit)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment