diff --git a/lib/kanta/query.ex b/lib/kanta/query.ex index 4e215c5..ad97b5f 100644 --- a/lib/kanta/query.ex +++ b/lib/kanta/query.ex @@ -34,6 +34,10 @@ defmodule Kanta.Query do Repo.get_repo().one(query, opts) end + def all(query \\ base(), opts \\ []) do + Repo.get_repo().all(query, opts) + end + @default_page_size 100 @minimum_per_page 10 @@ -230,6 +234,8 @@ defmodule Kanta.Query do |> maybe_greater_or_equal_than(value, field_name) |> maybe_lower_than(value, field_name) |> maybe_lower_or_equal_than(value, field_name) + |> maybe_is_null(value, field_name) + |> maybe_is_not_null(value, field_name) |> maybe_equality(value, field_name) end @@ -288,13 +294,26 @@ defmodule Kanta.Query do end defp maybe_equality(q, value, field_name) do - if (is_binary(value) && String.match?(value, ~r/(>|>=|<|<=).*/)) || is_list(value) do + if (is_binary(value) && String.match?(value, ~r/(>|>=|<|<=).*/)) || + value in [:is_null, :is_not_null] || is_list(value) do q else from(s in q, where: field(s, ^field_name) == ^value) end end + defp maybe_is_null(q, :is_null, field_name) do + from(s in q, where: is_nil(field(s, ^field_name))) + end + + defp maybe_is_null(q, _value, field_name), do: q + + defp maybe_is_not_null(q, :is_not_null, field_name) do + from(s in q, where: not is_nil(field(s, ^field_name))) + end + + defp maybe_is_not_null(q, _value, field_name), do: q + defp get_field_name(value, current_query, field_name) when is_binary(field_name) do get_field_name(value, current_query, String.to_existing_atom(field_name)) end diff --git a/lib/kanta/translations.ex b/lib/kanta/translations.ex index fd4541d..fdbe8a5 100644 --- a/lib/kanta/translations.ex +++ b/lib/kanta/translations.ex @@ -48,11 +48,12 @@ defmodule Kanta.Translations do defdelegate update_locale(locale, attrs, opts \\ []), to: Locales # TRANSLATIONS - defdelegate list_plural_translations(params), to: PluralTranslations + defdelegate list_plural_translations(params \\ []), to: PluralTranslations defdelegate get_plural_translation(params), to: PluralTranslations defdelegate create_plural_translation(attrs, opts \\ []), to: PluralTranslations defdelegate update_plural_translation(translation, attrs, opts \\ []), to: PluralTranslations + defdelegate list_singular_translations(params \\ []), to: SingularTranslations defdelegate get_singular_translation(params), to: SingularTranslations defdelegate create_singular_translation(attrs, opts \\ []), to: SingularTranslations diff --git a/lib/kanta/translations/plural_translation/finders/list_plural_translations.ex b/lib/kanta/translations/plural_translation/finders/list_plural_translations.ex index 0425697..38b78bb 100644 --- a/lib/kanta/translations/plural_translation/finders/list_plural_translations.ex +++ b/lib/kanta/translations/plural_translation/finders/list_plural_translations.ex @@ -8,10 +8,16 @@ defmodule Kanta.Translations.PluralTranslations.Finders.ListPluralTranslations d binding: :plural_translation def find(params \\ []) do - base() - |> filter_query(params[:filter]) - |> search_query(params[:search]) - |> preload_resources(params[:preloads] || []) - |> paginate(params[:page], params[:per_page]) + query = + base() + |> filter_query(params[:filter]) + |> search_query(params[:search]) + |> preload_resources(params[:preloads] || []) + + if params[:skip_pagination] do + all(query) + else + paginate(query, params[:page], params[:per_page]) + end end end diff --git a/lib/kanta/translations/singular_translation/finders/list_singular_translations.ex b/lib/kanta/translations/singular_translation/finders/list_singular_translations.ex index c5212e4..e53519f 100644 --- a/lib/kanta/translations/singular_translation/finders/list_singular_translations.ex +++ b/lib/kanta/translations/singular_translation/finders/list_singular_translations.ex @@ -8,10 +8,16 @@ defmodule Kanta.Translations.SingularTranslations.Finders.ListSingularTranslatio binding: :singular_translation def find(params \\ []) do - base() - |> filter_query(params[:filter]) - |> search_query(params[:search]) - |> preload_resources(params[:preloads] || []) - |> paginate(params[:page], params[:per_page]) + query = + base() + |> filter_query(params[:filter]) + |> search_query(params[:search]) + |> preload_resources(params[:preloads] || []) + + if params[:skip_pagination] do + all(query) + else + paginate(query, params[:page], params[:per_page]) + end end end diff --git a/lib/kanta/translations/singular_translation/singular_translations.ex b/lib/kanta/translations/singular_translation/singular_translations.ex index 2be6363..d85f3a1 100644 --- a/lib/kanta/translations/singular_translation/singular_translations.ex +++ b/lib/kanta/translations/singular_translation/singular_translations.ex @@ -3,12 +3,19 @@ defmodule Kanta.Translations.SingularTranslations do Singular translations Kanta subcontext """ - alias Kanta.Translations.SingularTranslations.Finders.GetSingularTranslation + alias Kanta.Translations.SingularTranslations.Finders.{ + GetSingularTranslation, + ListSingularTranslations + } alias Kanta.Cache alias Kanta.Repo alias Kanta.Translations.SingularTranslation + def list_singular_translations(params \\ []) do + ListSingularTranslations.find(params) + end + def get_singular_translation(params \\ []) do GetSingularTranslation.find(params) end