Skip to content

Commit

Permalink
Extended some query options
Browse files Browse the repository at this point in the history
  • Loading branch information
Teifion committed Aug 10, 2024
1 parent dc2a960 commit 6fc5cb5
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions lib/teiserver/game/queries/match_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ defmodule Teiserver.Game.MatchQueries do
)
end

def _where(query, :name_like, name) do
uname = "%" <> name <> "%"

from(users in query,
where: ilike(users.name, ^uname)
)
end

@spec do_order_by(Ecto.Query.t(), list | nil) :: Ecto.Query.t()
defp do_order_by(query, nil), do: query

Expand Down Expand Up @@ -152,31 +160,63 @@ defmodule Teiserver.Game.MatchQueries do
end

@spec _preload(Ecto.Query.t(), any) :: Ecto.Query.t()
def _preload(query, :host) do
from(matches in query,
left_join: hosts in assoc(matches, :host),
preload: [host: hosts]
)
end

def _preload(query, :type) do
from(match in query,
left_join: types in assoc(match, :type),
from(matches in query,
left_join: types in assoc(matches, :type),
preload: [type: types]
)
end

def _preload(query, :members) do
from(match in query,
left_join: members in assoc(match, :members),
from(matches in query,
left_join: members in assoc(matches, :members),
preload: [members: members]
)
end

def _preload(query, :members_with_users) do
from(matches in query,
left_join: memberships in assoc(matches, :members),
left_join: users in assoc(memberships, :user),
preload: [members: {memberships, user: users}]
)
end

def _preload(query, :settings) do
from(match in query,
left_join: settings in assoc(match, :settings),
from(matches in query,
left_join: settings in assoc(matches, :settings),
preload: [settings: settings]
)
end

def _preload(query, :settings_with_types) do
from(matches in query,
left_join: settings in assoc(matches, :settings),
left_join: types in assoc(settings, :type),
preload: [settings: {settings, type: types}]
)
end

def _preload(query, :choices) do
from(match in query,
left_join: choices in assoc(match, :choices),
from(matches in query,
left_join: choices in assoc(matches, :choices),
preload: [choices: choices]
)
end

def _preload(query, :choices_with_users_and_types) do
from(matches in query,
left_join: choices in assoc(matches, :choices),
left_join: types in assoc(choices, :type),
left_join: users in assoc(choices, :user),
preload: [choices: {choices, type: types, user: users}]
)
end
end

0 comments on commit 6fc5cb5

Please sign in to comment.