Skip to content

Commit

Permalink
feat: Expand Kanta.Query module (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-swiatek authored Oct 9, 2024
1 parent 2749f0e commit a340d64
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
21 changes: 20 additions & 1 deletion lib/kanta/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/kanta/translations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a340d64

Please sign in to comment.