Created
May 2, 2019 23:22
-
-
Save mazz/ed535fba7bf738116d2e15ab705f78b9 to your computer and use it in GitHub Desktop.
This file contains 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
# ** (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