2023-12-04 14:15:25 +00:00
|
|
|
defmodule BoardWise.Coaches do
|
|
|
|
@moduledoc """
|
|
|
|
The Coaches context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
|
|
|
alias BoardWise.Repo
|
|
|
|
|
|
|
|
alias BoardWise.Coaches.Coach
|
2023-12-06 23:24:40 +00:00
|
|
|
alias BoardWise.Coaches.QueryParams
|
2023-12-04 14:15:25 +00:00
|
|
|
|
|
|
|
@prefix "coach_scraper"
|
|
|
|
|
2023-12-06 23:24:40 +00:00
|
|
|
defmacrop rating_fragment(field, gte, lte) do
|
|
|
|
quote do
|
|
|
|
fragment(
|
|
|
|
"""
|
|
|
|
CASE
|
|
|
|
WHEN ? IS NULL THEN 0
|
|
|
|
WHEN ? IS NULL THEN 0
|
|
|
|
WHEN ? >= ? AND ? <= ? THEN 5
|
|
|
|
ELSE 0
|
|
|
|
END
|
|
|
|
""",
|
|
|
|
type(unquote(gte), :integer),
|
|
|
|
type(unquote(lte), :integer),
|
|
|
|
unquote(field),
|
|
|
|
type(unquote(gte), :integer),
|
|
|
|
unquote(field),
|
|
|
|
type(unquote(lte), :integer)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-04 14:15:25 +00:00
|
|
|
@doc """
|
2023-12-06 23:24:40 +00:00
|
|
|
Return the list of coaches according to the specified params.
|
2023-12-04 14:15:25 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2023-12-06 23:24:40 +00:00
|
|
|
iex> list_coaches(%QueryParams{...})
|
2023-12-04 14:15:25 +00:00
|
|
|
[%Coach{}, ...]
|
|
|
|
|
|
|
|
"""
|
2023-12-06 23:24:40 +00:00
|
|
|
def list_coaches(%QueryParams{
|
|
|
|
:rapid_gte => rapid_gte,
|
|
|
|
:rapid_lte => rapid_lte,
|
|
|
|
:blitz_gte => blitz_gte,
|
|
|
|
:blitz_lte => blitz_lte,
|
|
|
|
:bullet_gte => bullet_gte,
|
|
|
|
:bullet_lte => bullet_lte,
|
|
|
|
:languages => languages,
|
2023-12-07 12:45:40 +00:00
|
|
|
:titles => titles,
|
2023-12-07 14:44:59 +00:00
|
|
|
:sites => sites,
|
2023-12-06 23:24:40 +00:00
|
|
|
:page_no => page_no,
|
|
|
|
:page_size => page_size
|
|
|
|
}) do
|
2023-12-04 20:35:01 +00:00
|
|
|
Coach
|
2023-12-06 23:24:40 +00:00
|
|
|
|> select([c], c)
|
|
|
|
|> select_merge(
|
|
|
|
[c],
|
|
|
|
%{
|
|
|
|
score:
|
|
|
|
fragment(
|
|
|
|
"""
|
2023-12-06 23:48:53 +00:00
|
|
|
CASE WHEN ? IS NOT NULL THEN 1000 ELSE 0 END +
|
|
|
|
CASE WHEN ? IS NOT NULL THEN 500 ELSE 0 END +
|
|
|
|
? +
|
|
|
|
? +
|
|
|
|
? +
|
2023-12-07 12:45:40 +00:00
|
|
|
(5 * (SELECT COUNT(*) FROM UNNEST(?) WHERE UNNEST = ANY(?))) +
|
2023-12-07 14:44:59 +00:00
|
|
|
CASE WHEN ? = ANY(?) THEN 5 ELSE 0 END +
|
|
|
|
CASE WHEN ? = ANY(?) THEN 30 ELSE 0 END
|
2023-12-06 23:24:40 +00:00
|
|
|
""",
|
2023-12-06 23:48:53 +00:00
|
|
|
c.name,
|
|
|
|
c.image_url,
|
2023-12-06 23:24:40 +00:00
|
|
|
rating_fragment(c.rapid, ^rapid_gte, ^rapid_lte),
|
|
|
|
rating_fragment(c.blitz, ^blitz_gte, ^blitz_lte),
|
2023-12-06 23:48:53 +00:00
|
|
|
rating_fragment(c.bullet, ^bullet_gte, ^bullet_lte),
|
2023-12-07 12:45:40 +00:00
|
|
|
c.languages,
|
2023-12-07 14:44:59 +00:00
|
|
|
type(^languages, {:array, :string}),
|
2023-12-07 12:45:40 +00:00
|
|
|
c.title,
|
2023-12-07 14:44:59 +00:00
|
|
|
type(^titles, {:array, :string}),
|
|
|
|
c.site,
|
|
|
|
type(^sites, {:array, :string})
|
2023-12-06 23:24:40 +00:00
|
|
|
)
|
|
|
|
|> selected_as(:score)
|
|
|
|
}
|
|
|
|
)
|
2023-12-07 15:12:32 +00:00
|
|
|
|> order_by(desc: selected_as(:score), asc: :position, asc: :username, asc: :id)
|
2023-12-06 02:46:27 +00:00
|
|
|
|> limit(^page_size)
|
2023-12-06 23:24:40 +00:00
|
|
|
|> offset(^((page_no - 1) * page_size))
|
2023-12-04 20:35:01 +00:00
|
|
|
|> Repo.all(prefix: @prefix)
|
2023-12-04 14:15:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single coach.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Coach does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_coach!(123)
|
|
|
|
%Coach{}
|
|
|
|
|
|
|
|
iex> get_coach!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_coach!(id), do: Repo.get!(Coach, id, prefix: @prefix)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a coach.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_coach(%{field: value})
|
|
|
|
{:ok, %Coach{}}
|
|
|
|
|
|
|
|
iex> create_coach(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_coach(attrs \\ %{}) do
|
|
|
|
%Coach{}
|
|
|
|
|> Coach.changeset(attrs)
|
|
|
|
|> Repo.insert(prefix: @prefix)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a coach.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> update_coach(coach, %{field: new_value})
|
|
|
|
{:ok, %Coach{}}
|
|
|
|
|
|
|
|
iex> update_coach(coach, %{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_coach(%Coach{} = coach, attrs) do
|
|
|
|
coach
|
|
|
|
|> Coach.changeset(attrs)
|
|
|
|
|> Repo.update(prefix: @prefix)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a coach.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_coach(coach)
|
|
|
|
{:ok, %Coach{}}
|
|
|
|
|
|
|
|
iex> delete_coach(coach)
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_coach(%Coach{} = coach) do
|
|
|
|
Repo.delete(coach, prefix: @prefix)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking coach changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> change_coach(coach)
|
|
|
|
%Ecto.Changeset{data: %Coach{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def change_coach(%Coach{} = coach, attrs \\ %{}) do
|
|
|
|
Coach.changeset(coach, attrs)
|
|
|
|
end
|
|
|
|
end
|