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:
2026-02-11 16:14:19 +01:00
parent 7ad08fa91d
commit 2bec68b9ed
9 changed files with 677 additions and 0 deletions

View 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

View 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

View File

@ -0,0 +1,101 @@
defmodule BeetRoundServerWeb.BiddingLive.Form do
use BeetRoundServerWeb, :live_view
alias BeetRoundServer.Biddings
alias BeetRoundServer.Biddings.Bidding
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope}>
<.header>
{@page_title}
<:subtitle>Use this form to manage bidding records in your database.</:subtitle>
</.header>
<.form for={@form} id="bidding-form" phx-change="validate" phx-submit="save">
<.input field={@form[:bidding_round]} type="number" label="Bidding round" />
<.input field={@form[:amount]} type="number" label="Amount" />
<.input field={@form[:depot_wish_one]} type="text" label="Depot wish one" />
<.input field={@form[:depot_wish_two]} type="text" label="Depot wish two" />
<footer>
<.button phx-disable-with="Saving..." variant="primary">Save Bidding</.button>
<.button navigate={return_path(@current_scope, @return_to, @bidding)}>Cancel</.button>
</footer>
</.form>
</Layouts.app>
"""
end
@impl true
def mount(params, _session, socket) do
{:ok,
socket
|> assign(:return_to, return_to(params["return_to"]))
|> apply_action(socket.assigns.live_action, params)}
end
defp return_to("show"), do: "show"
defp return_to(_), do: "index"
defp apply_action(socket, :edit, %{"id" => id}) do
bidding = Biddings.get_bidding!(socket.assigns.current_scope, id)
socket
|> assign(:page_title, "Edit Bidding")
|> assign(:bidding, bidding)
|> assign(:form, to_form(Biddings.change_bidding(socket.assigns.current_scope, bidding)))
end
defp apply_action(socket, :new, _params) do
bidding = %Bidding{user_id: socket.assigns.current_scope.user.id}
socket
|> assign(:page_title, "New Bidding")
|> assign(:bidding, bidding)
|> assign(:form, to_form(Biddings.change_bidding(socket.assigns.current_scope, bidding)))
end
@impl true
def handle_event("validate", %{"bidding" => bidding_params}, socket) do
changeset = Biddings.change_bidding(socket.assigns.current_scope, socket.assigns.bidding, bidding_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end
def handle_event("save", %{"bidding" => bidding_params}, socket) do
save_bidding(socket, socket.assigns.live_action, bidding_params)
end
defp save_bidding(socket, :edit, bidding_params) do
case Biddings.update_bidding(socket.assigns.current_scope, socket.assigns.bidding, bidding_params) do
{:ok, bidding} ->
{:noreply,
socket
|> put_flash(:info, "Bidding updated successfully")
|> push_navigate(
to: return_path(socket.assigns.current_scope, socket.assigns.return_to, bidding)
)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp save_bidding(socket, :new, bidding_params) do
case Biddings.create_bidding(socket.assigns.current_scope, bidding_params) do
{:ok, bidding} ->
{:noreply,
socket
|> put_flash(:info, "Bidding created successfully")
|> push_navigate(
to: return_path(socket.assigns.current_scope, socket.assigns.return_to, bidding)
)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp return_path(_scope, "index", _bidding), do: ~p"/biddings"
defp return_path(_scope, "show", bidding), do: ~p"/biddings/#{bidding}"
end

View File

@ -0,0 +1,76 @@
defmodule BeetRoundServerWeb.BiddingLive.Index do
use BeetRoundServerWeb, :live_view
alias BeetRoundServer.Biddings
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope}>
<.header>
Listing Biddings
<:actions>
<.button variant="primary" navigate={~p"/biddings/new"}>
<.icon name="hero-plus" /> New Bidding
</.button>
</:actions>
</.header>
<.table
id="biddings"
rows={@streams.biddings}
row_click={fn {_id, bidding} -> JS.navigate(~p"/biddings/#{bidding}") end}
>
<:col :let={{_id, bidding}} label="Bidding round">{bidding.bidding_round}</:col>
<:col :let={{_id, bidding}} label="Amount">{bidding.amount}</:col>
<:col :let={{_id, bidding}} label="Depot wish one">{bidding.depot_wish_one}</:col>
<:col :let={{_id, bidding}} label="Depot wish two">{bidding.depot_wish_two}</:col>
<:action :let={{_id, bidding}}>
<div class="sr-only">
<.link navigate={~p"/biddings/#{bidding}"}>Show</.link>
</div>
<.link navigate={~p"/biddings/#{bidding}/edit"}>Edit</.link>
</:action>
<:action :let={{id, bidding}}>
<.link
phx-click={JS.push("delete", value: %{id: bidding.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
</Layouts.app>
"""
end
@impl true
def mount(_params, _session, socket) do
if connected?(socket) do
Biddings.subscribe_biddings(socket.assigns.current_scope)
end
{:ok,
socket
|> assign(:page_title, "Listing Biddings")
|> stream(:biddings, list_biddings(socket.assigns.current_scope))}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
bidding = Biddings.get_bidding!(socket.assigns.current_scope, id)
{:ok, _} = Biddings.delete_bidding(socket.assigns.current_scope, bidding)
{:noreply, stream_delete(socket, :biddings, bidding)}
end
@impl true
def handle_info({type, %BeetRoundServer.Biddings.Bidding{}}, socket)
when type in [:created, :updated, :deleted] do
{:noreply, stream(socket, :biddings, list_biddings(socket.assigns.current_scope), reset: true)}
end
defp list_biddings(current_scope) do
Biddings.list_biddings(current_scope)
end
end

View File

@ -0,0 +1,67 @@
defmodule BeetRoundServerWeb.BiddingLive.Show do
use BeetRoundServerWeb, :live_view
alias BeetRoundServer.Biddings
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope}>
<.header>
Bidding {@bidding.id}
<:subtitle>This is a bidding record from your database.</:subtitle>
<:actions>
<.button navigate={~p"/biddings"}>
<.icon name="hero-arrow-left" />
</.button>
<.button variant="primary" navigate={~p"/biddings/#{@bidding}/edit?return_to=show"}>
<.icon name="hero-pencil-square" /> Edit bidding
</.button>
</:actions>
</.header>
<.list>
<:item title="Bidding round">{@bidding.bidding_round}</:item>
<:item title="Amount">{@bidding.amount}</:item>
<:item title="Depot wish one">{@bidding.depot_wish_one}</:item>
<:item title="Depot wish two">{@bidding.depot_wish_two}</:item>
</.list>
</Layouts.app>
"""
end
@impl true
def mount(%{"id" => id}, _session, socket) do
if connected?(socket) do
Biddings.subscribe_biddings(socket.assigns.current_scope)
end
{:ok,
socket
|> assign(:page_title, "Show Bidding")
|> assign(:bidding, Biddings.get_bidding!(socket.assigns.current_scope, id))}
end
@impl true
def handle_info(
{:updated, %BeetRoundServer.Biddings.Bidding{id: id} = bidding},
%{assigns: %{bidding: %{id: id}}} = socket
) do
{:noreply, assign(socket, :bidding, bidding)}
end
def handle_info(
{:deleted, %BeetRoundServer.Biddings.Bidding{id: id}},
%{assigns: %{bidding: %{id: id}}} = socket
) do
{:noreply,
socket
|> put_flash(:error, "The current bidding was deleted.")
|> push_navigate(to: ~p"/biddings")}
end
def handle_info({type, %BeetRoundServer.Biddings.Bidding{}}, socket)
when type in [:created, :updated, :deleted] do
{:noreply, socket}
end
end

View File

@ -0,0 +1,18 @@
defmodule BeetRoundServer.Repo.Migrations.CreateBiddings do
use Ecto.Migration
def change do
create table(:biddings, primary_key: false) do
add :id, :binary_id, primary_key: true
add :bidding_round, :integer
add :amount, :integer
add :depot_wish_one, :string
add :depot_wish_two, :string
add :user_id, references(:users, type: :binary_id, on_delete: :delete_all)
timestamps(type: :utc_datetime)
end
create index(:biddings, [:user_id])
end
end

View File

@ -0,0 +1,97 @@
defmodule BeetRoundServer.BiddingsTest do
use BeetRoundServer.DataCase
alias BeetRoundServer.Biddings
describe "biddings" do
alias BeetRoundServer.Biddings.Bidding
import BeetRoundServer.AccountsFixtures, only: [user_scope_fixture: 0]
import BeetRoundServer.BiddingsFixtures
@invalid_attrs %{amount: nil, bidding_round: nil, depot_wish_one: nil, depot_wish_two: nil}
test "list_biddings/1 returns all scoped biddings" do
scope = user_scope_fixture()
other_scope = user_scope_fixture()
bidding = bidding_fixture(scope)
other_bidding = bidding_fixture(other_scope)
assert Biddings.list_biddings(scope) == [bidding]
assert Biddings.list_biddings(other_scope) == [other_bidding]
end
test "get_bidding!/2 returns the bidding with given id" do
scope = user_scope_fixture()
bidding = bidding_fixture(scope)
other_scope = user_scope_fixture()
assert Biddings.get_bidding!(scope, bidding.id) == bidding
assert_raise Ecto.NoResultsError, fn -> Biddings.get_bidding!(other_scope, bidding.id) end
end
test "create_bidding/2 with valid data creates a bidding" do
valid_attrs = %{amount: 42, bidding_round: 42, depot_wish_one: "some depot_wish_one", depot_wish_two: "some depot_wish_two"}
scope = user_scope_fixture()
assert {:ok, %Bidding{} = bidding} = Biddings.create_bidding(scope, valid_attrs)
assert bidding.amount == 42
assert bidding.bidding_round == 42
assert bidding.depot_wish_one == "some depot_wish_one"
assert bidding.depot_wish_two == "some depot_wish_two"
assert bidding.user_id == scope.user.id
end
test "create_bidding/2 with invalid data returns error changeset" do
scope = user_scope_fixture()
assert {:error, %Ecto.Changeset{}} = Biddings.create_bidding(scope, @invalid_attrs)
end
test "update_bidding/3 with valid data updates the bidding" do
scope = user_scope_fixture()
bidding = bidding_fixture(scope)
update_attrs = %{amount: 43, bidding_round: 43, depot_wish_one: "some updated depot_wish_one", depot_wish_two: "some updated depot_wish_two"}
assert {:ok, %Bidding{} = bidding} = Biddings.update_bidding(scope, bidding, update_attrs)
assert bidding.amount == 43
assert bidding.bidding_round == 43
assert bidding.depot_wish_one == "some updated depot_wish_one"
assert bidding.depot_wish_two == "some updated depot_wish_two"
end
test "update_bidding/3 with invalid scope raises" do
scope = user_scope_fixture()
other_scope = user_scope_fixture()
bidding = bidding_fixture(scope)
assert_raise MatchError, fn ->
Biddings.update_bidding(other_scope, bidding, %{})
end
end
test "update_bidding/3 with invalid data returns error changeset" do
scope = user_scope_fixture()
bidding = bidding_fixture(scope)
assert {:error, %Ecto.Changeset{}} = Biddings.update_bidding(scope, bidding, @invalid_attrs)
assert bidding == Biddings.get_bidding!(scope, bidding.id)
end
test "delete_bidding/2 deletes the bidding" do
scope = user_scope_fixture()
bidding = bidding_fixture(scope)
assert {:ok, %Bidding{}} = Biddings.delete_bidding(scope, bidding)
assert_raise Ecto.NoResultsError, fn -> Biddings.get_bidding!(scope, bidding.id) end
end
test "delete_bidding/2 with invalid scope raises" do
scope = user_scope_fixture()
other_scope = user_scope_fixture()
bidding = bidding_fixture(scope)
assert_raise MatchError, fn -> Biddings.delete_bidding(other_scope, bidding) end
end
test "change_bidding/2 returns a bidding changeset" do
scope = user_scope_fixture()
bidding = bidding_fixture(scope)
assert %Ecto.Changeset{} = Biddings.change_bidding(scope, bidding)
end
end
end

View File

@ -0,0 +1,125 @@
defmodule BeetRoundServerWeb.BiddingLiveTest do
use BeetRoundServerWeb.ConnCase
import Phoenix.LiveViewTest
import BeetRoundServer.BiddingsFixtures
@create_attrs %{amount: 42, bidding_round: 42, depot_wish_one: "some depot_wish_one", depot_wish_two: "some depot_wish_two"}
@update_attrs %{amount: 43, bidding_round: 43, depot_wish_one: "some updated depot_wish_one", depot_wish_two: "some updated depot_wish_two"}
@invalid_attrs %{amount: nil, bidding_round: nil, depot_wish_one: nil, depot_wish_two: nil}
setup :register_and_log_in_user
defp create_bidding(%{scope: scope}) do
bidding = bidding_fixture(scope)
%{bidding: bidding}
end
describe "Index" do
setup [:create_bidding]
test "lists all biddings", %{conn: conn, bidding: bidding} do
{:ok, _index_live, html} = live(conn, ~p"/biddings")
assert html =~ "Listing Biddings"
assert html =~ bidding.depot_wish_one
end
test "saves new bidding", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/biddings")
assert {:ok, form_live, _} =
index_live
|> element("a", "New Bidding")
|> render_click()
|> follow_redirect(conn, ~p"/biddings/new")
assert render(form_live) =~ "New Bidding"
assert form_live
|> form("#bidding-form", bidding: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert {:ok, index_live, _html} =
form_live
|> form("#bidding-form", bidding: @create_attrs)
|> render_submit()
|> follow_redirect(conn, ~p"/biddings")
html = render(index_live)
assert html =~ "Bidding created successfully"
assert html =~ "some depot_wish_one"
end
test "updates bidding in listing", %{conn: conn, bidding: bidding} do
{:ok, index_live, _html} = live(conn, ~p"/biddings")
assert {:ok, form_live, _html} =
index_live
|> element("#biddings-#{bidding.id} a", "Edit")
|> render_click()
|> follow_redirect(conn, ~p"/biddings/#{bidding}/edit")
assert render(form_live) =~ "Edit Bidding"
assert form_live
|> form("#bidding-form", bidding: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert {:ok, index_live, _html} =
form_live
|> form("#bidding-form", bidding: @update_attrs)
|> render_submit()
|> follow_redirect(conn, ~p"/biddings")
html = render(index_live)
assert html =~ "Bidding updated successfully"
assert html =~ "some updated depot_wish_one"
end
test "deletes bidding in listing", %{conn: conn, bidding: bidding} do
{:ok, index_live, _html} = live(conn, ~p"/biddings")
assert index_live |> element("#biddings-#{bidding.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#biddings-#{bidding.id}")
end
end
describe "Show" do
setup [:create_bidding]
test "displays bidding", %{conn: conn, bidding: bidding} do
{:ok, _show_live, html} = live(conn, ~p"/biddings/#{bidding}")
assert html =~ "Show Bidding"
assert html =~ bidding.depot_wish_one
end
test "updates bidding and returns to show", %{conn: conn, bidding: bidding} do
{:ok, show_live, _html} = live(conn, ~p"/biddings/#{bidding}")
assert {:ok, form_live, _} =
show_live
|> element("a", "Edit")
|> render_click()
|> follow_redirect(conn, ~p"/biddings/#{bidding}/edit?return_to=show")
assert render(form_live) =~ "Edit Bidding"
assert form_live
|> form("#bidding-form", bidding: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert {:ok, show_live, _html} =
form_live
|> form("#bidding-form", bidding: @update_attrs)
|> render_submit()
|> follow_redirect(conn, ~p"/biddings/#{bidding}")
html = render(show_live)
assert html =~ "Bidding updated successfully"
assert html =~ "some updated depot_wish_one"
end
end
end

View File

@ -0,0 +1,22 @@
defmodule BeetRoundServer.BiddingsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `BeetRoundServer.Biddings` context.
"""
@doc """
Generate a bidding.
"""
def bidding_fixture(scope, attrs \\ %{}) do
attrs =
Enum.into(attrs, %{
amount: 42,
bidding_round: 42,
depot_wish_one: "some depot_wish_one",
depot_wish_two: "some depot_wish_two"
})
{:ok, bidding} = BeetRoundServer.Biddings.create_bidding(scope, attrs)
bidding
end
end