Files
BeetRoundServer/lib/beet_round_server/biddings.ex

172 lines
3.7 KiB
Elixir

defmodule BeetRoundServer.Biddings do
@moduledoc """
The Biddings context.
"""
import Ecto.Query, warn: false
alias BeetRoundServer.Repo
alias BeetRoundServer.Biddings.Bidding
alias BeetRoundServer.Accounts.Scope
@doc """
Subscribes to scoped notifications about any bidding changes.
The broadcasted messages match the pattern:
* {:created, %Bidding{}}
* {:updated, %Bidding{}}
* {:deleted, %Bidding{}}
"""
def subscribe_biddings(%Scope{} = scope) do
key = scope.user.id
Phoenix.PubSub.subscribe(BeetRoundServer.PubSub, "user:#{key}:biddings")
end
defp broadcast_bidding(%Scope{} = scope, message) do
key = scope.user.id
Phoenix.PubSub.broadcast(BeetRoundServer.PubSub, "user:#{key}:biddings", message)
end
@doc """
Returns the list of biddings.
## Examples
iex> list_biddings(scope)
[%Bidding{}, ...]
"""
def list_biddings(%Scope{} = scope) do
Repo.all_by(Bidding, user_id: scope.user.id)
end
def list_biddings() do
Repo.all(Bidding)
end
@doc """
Gets a single bidding.
Raises `Ecto.NoResultsError` if the Bidding does not exist.
## Examples
iex> get_bidding!(scope, 123)
%Bidding{}
iex> get_bidding!(scope, 456)
** (Ecto.NoResultsError)
"""
def get_bidding!(%Scope{} = scope, id) do
Repo.get_by!(Bidding, id: id, user_id: scope.user.id)
end
def biddings_of_round(round_number) do
Repo.all(
from(bidding in Bidding,
where: bidding.bidding_round == ^round_number,
order_by: [asc: bidding.inserted_at]
)
)
end
def get_most_recent_bidding(%Scope{} = scope) do
query =
Ecto.Query.from(bidding in Bidding,
where: bidding.user_id == ^scope.user.id,
order_by: [desc: bidding.inserted_at],
limit: 1
)
Repo.one(query)
end
@doc """
Creates a bidding.
## Examples
iex> create_bidding(scope, %{field: value})
{:ok, %Bidding{}}
iex> create_bidding(scope, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_bidding(%Scope{} = scope, attrs) do
with {:ok, bidding = %Bidding{}} <-
%Bidding{}
|> Bidding.changeset(attrs, scope)
|> Repo.insert() do
broadcast_bidding(scope, {:created, bidding})
{:ok, bidding}
end
end
@doc """
Updates a bidding.
## Examples
iex> update_bidding(scope, bidding, %{field: new_value})
{:ok, %Bidding{}}
iex> update_bidding(scope, bidding, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_bidding(%Scope{} = scope, %Bidding{} = bidding, attrs) do
true = bidding.user_id == scope.user.id
with {:ok, bidding = %Bidding{}} <-
bidding
|> Bidding.changeset(attrs, scope)
|> Repo.update() do
broadcast_bidding(scope, {:updated, bidding})
{:ok, bidding}
end
end
@doc """
Deletes a bidding.
## Examples
iex> delete_bidding(scope, bidding)
{:ok, %Bidding{}}
iex> delete_bidding(scope, bidding)
{:error, %Ecto.Changeset{}}
"""
def delete_bidding(%Scope{} = scope, %Bidding{} = bidding) do
true = bidding.user_id == scope.user.id
with {:ok, bidding = %Bidding{}} <-
Repo.delete(bidding) do
broadcast_bidding(scope, {:deleted, bidding})
{:ok, bidding}
end
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking bidding changes.
## Examples
iex> change_bidding(scope, bidding)
%Ecto.Changeset{data: %Bidding{}}
"""
def change_bidding(%Scope{} = scope, %Bidding{} = bidding, attrs \\ %{}) do
true = bidding.user_id == scope.user.id
Bidding.changeset(bidding, attrs, scope)
end
end