@default_sorting [
desc_nulls_last: :circulating_market_cap,
desc_nulls_last: :fiat_value,
desc_nulls_last: :holder_count,
asc: :name,
asc: :contract_address_hash
]
...
@doc """
Lists the top `t:__MODULE__.t/0`'s'.
"""
@spec list_top(String.t() | nil, [
Chain.paging_options()
| {:sorting, SortingHelper.sorting_params()}
| {:token_type, [String.t()]}
]) :: [Token.t()]
def list_top(filter, options \\ []) do
paging_options = Keyword.get(options, :paging_options, Chain.default_paging_options())
token_type = Keyword.get(options, :token_type, nil)
sorting = Keyword.get(options, :sorting, [])
query = from(t in Token, preload: [:contract_address])
sorted_paginated_query =
query
|> apply_filter(token_type)
|> SortingHelper.apply_sorting(sorting, @default_sorting)
|> SortingHelper.page_with_sorting(paging_options, sorting, @default_sorting)
filtered_query =
case filter && filter !== "" && Search.prepare_search_term(filter) do
{:some, filter_term} ->
sorted_paginated_query
|> where(fragment("to_tsvector('english', symbol || ' ' || name) @@ to_tsquery(?)", ^filter_term))
_ ->
sorted_paginated_query
end
filtered_query
|> Chain.select_repo(options).all()
end