After "mix phx.gen.live Biddings Bidding biddings bidding_round:integer amount:integer depot_wish_one:string depot_wish_two:string".
This commit is contained in:
147
lib/beet_round_server/biddings.ex
Normal file
147
lib/beet_round_server/biddings.ex
Normal file
@ -0,0 +1,147 @@
|
||||
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
|
||||
|
||||
@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
|
||||
|
||||
@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
|
||||
24
lib/beet_round_server/biddings/bidding.ex
Normal file
24
lib/beet_round_server/biddings/bidding.ex
Normal file
@ -0,0 +1,24 @@
|
||||
defmodule BeetRoundServer.Biddings.Bidding do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "biddings" do
|
||||
field :bidding_round, :integer
|
||||
field :amount, :integer
|
||||
field :depot_wish_one, :string
|
||||
field :depot_wish_two, :string
|
||||
field :user_id, :binary_id
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(bidding, attrs, user_scope) do
|
||||
bidding
|
||||
|> cast(attrs, [:bidding_round, :amount, :depot_wish_one, :depot_wish_two])
|
||||
|> validate_required([:bidding_round, :amount, :depot_wish_one, :depot_wish_two])
|
||||
|> put_change(:user_id, user_scope.user.id)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user