Compare commits

...

7 Commits

17 changed files with 502 additions and 32 deletions

View File

@ -37,6 +37,27 @@ defmodule BeetRoundServer.BiddingRounds do
"""
def get_bidding_round!(id), do: Repo.get!(BiddingRound, id)
def get_highest_bidding_round!() do
query =
Ecto.Query.from(bidding_round in BiddingRound,
order_by: [desc: bidding_round.round_number],
limit: 1
)
Repo.one(query)
end
def get_bidding_round_by_number!(round_number) do
query =
Ecto.Query.from(bidding_round in BiddingRound,
where: bidding_round.round_number == ^round_number,
order_by: [desc: bidding_round.inserted_at],
limit: 1
)
Repo.one(query)
end
@doc """
Creates a bidding_round.

View File

@ -6,7 +6,7 @@ defmodule BeetRoundServer.BiddingRounds.BiddingRound do
@foreign_key_type :binary_id
schema "bidding_rounds" do
field :round_number, :integer
field :running, :boolean, default: false
field :stopped, :boolean, default: false
timestamps(type: :utc_datetime)
end
@ -14,7 +14,8 @@ defmodule BeetRoundServer.BiddingRounds.BiddingRound do
@doc false
def changeset(bidding_round, attrs) do
bidding_round
|> cast(attrs, [:round_number, :running])
|> validate_required([:round_number, :running])
|> cast(attrs, [:round_number, :stopped])
|> validate_required([:round_number, :stopped])
|> unique_constraint(:round_number)
end
end

View File

@ -0,0 +1,118 @@
defmodule BeetRoundServer.BiddingRounds.BiddingRoundFacade do
alias BeetRoundServer.BiddingRounds.BiddingRound
alias BeetRoundServer.BiddingRounds
alias BeetRoundServer.BiddingRounds.BiddingRoundServer
def restart_if_necessary() do
last_round = get_highest_bidding_round()
if last_round.stopped == false do
IO.puts("There is a last round, that wasn't stopped. Should be running...")
if !isAlive() do
IO.puts("...but it isn't. Restarting last round...")
restart_hightest_round()
end
end
end
def get_highest_bidding_round() do
last_round = BiddingRounds.get_highest_bidding_round!()
if last_round != nil do
last_round
else
%BiddingRound{round_number: 0, stopped: true, id: "00000000-0000-0000-0000-000000000000"}
end
end
def get_current_round do
restart_if_necessary()
if GenServer.whereis(CurrentRoundServer) == nil do
IO.puts("CurrentRoundServer isn't alive. Returning 0...")
# %BiddingRound{round_number: 0, stopped: true, id: "00000000-0000-0000-0000-000000000000"}
0
else
GenServer.call(CurrentRoundServer, :val)
end
end
def start_new_round() do
if isAlive() do
IO.puts("CurrentRoundServer is alive! Please stop the server before starting a new round")
{:error, "A current round is running! Please stop it, before starting a new round."}
else
IO.puts("CurrentRoundServer isn't alive. Starting instance...")
last_round = BiddingRounds.get_highest_bidding_round!()
cond do
last_round == nil ->
IO.puts("No bidding round found. Starting first round...")
round_number = 1
BiddingRoundServer.start(round_number)
BiddingRounds.create_bidding_round(%{round_number: round_number})
last_round.stopped == false ->
IO.puts("Last bidding round not stopped. Restarting round...")
BiddingRoundServer.start(last_round.round_number)
true ->
IO.puts("Last bidding round has stopped. Starting a new round...")
round_number = last_round.round_number + 1
BiddingRoundServer.start(round_number)
BiddingRounds.create_bidding_round(%{round_number: round_number})
end
end
end
def restart_hightest_round() do
IO.puts("Restarting hightest round...")
if isAlive() do
IO.puts("Server is alive. Nothing to do...")
IO.puts(["Current round: ", GenServer.call(CurrentRoundServer, :val)])
else
IO.puts("Server isn't alive. Trying to restart last round.")
last_round = BiddingRounds.get_highest_bidding_round!()
cond do
last_round == nil ->
IO.puts("No bidding round found! Can't restart round...")
{:error, "No bidding round found! Nothing to restart."}
true ->
IO.puts("Last bidding round found. Restarting...")
BiddingRoundServer.start(last_round.round_number)
BiddingRounds.update_bidding_round(last_round, %{stopped: false})
end
end
end
def stop_current_round() do
IO.puts("Stopping current round...")
if isAlive() do
IO.puts("Server is alive. Shutting down and writing to DB...")
current_round_number = GenServer.call(CurrentRoundServer, :val)
GenServer.stop(CurrentRoundServer)
current_round = BiddingRounds.get_bidding_round_by_number!(current_round_number)
BiddingRounds.update_bidding_round(current_round, %{stopped: true})
else
IO.puts("Server isn't alive. Nothing to shut down.")
end
end
def isAlive() do
GenServer.whereis(CurrentRoundServer) != nil
end
end

View File

@ -0,0 +1,39 @@
defmodule BeetRoundServer.BiddingRounds.BiddingRoundServer do
use GenServer
def inc(pid), do: GenServer.cast(pid, :inc)
def dec(pid), do: GenServer.cast(pid, :dec)
def val(pid) do
GenServer.call(pid, :val)
end
def stop(pid) do
GenServer.stop(pid)
end
def start(initial_val) do
GenServer.start(__MODULE__, initial_val, name: CurrentRoundServer)
end
def init(initial_val) do
{:ok, initial_val}
end
def terminate(_reason, val) do
IO.puts("Stopping bidding round:")
IO.puts(val)
:ok
end
def handle_cast(:inc, val) do
{:noreply, val + 1}
end
def handle_cast(:dec, val) do
{:noreply, val - 1}
end
def handle_call(:val, _from, val) do
{:reply, val, val}
end
end

View File

@ -44,6 +44,10 @@ defmodule BeetRoundServer.Biddings do
Repo.all_by(Bidding, user_id: scope.user.id)
end
def list_biddings() do
Repo.all(Bidding)
end
@doc """
Gets a single bidding.
@ -62,6 +66,26 @@ defmodule BeetRoundServer.Biddings 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.

View File

@ -0,0 +1,59 @@
defmodule BeetRoundServerWeb.BiddingController do
use BeetRoundServerWeb, :controller
alias BeetRoundServer.Biddings
alias BeetRoundServer.BiddingRounds.BiddingRoundFacade
# alias BeetRoundServer.Biddings.Bidding
action_fallback BeetRoundServerWeb.FallbackController
def index(conn, _params) do
biddings = Biddings.list_biddings()
IO.puts("biddings:")
IO.inspect(biddings)
render(conn, :index, biddings: biddings)
end
def biddings_of_round(conn, %{"round_number" => round_number}) do
biddings = Biddings.biddings_of_round(round_number)
render(conn, :index, biddings: biddings)
end
def biddings_of_highest_round(conn, _params) do
round = BiddingRoundFacade.get_highest_bidding_round()
IO.puts("Highest round number:")
IO.puts(round.round_number)
biddings = Biddings.biddings_of_round(round.round_number)
render(conn, :index, biddings: biddings)
end
# def create(conn, %{"bidding" => bidding_params}) do
# with {:ok, %Bidding{} = bidding} <- Biddings.create_bidding(bidding_params) do
# conn
# |> put_status(:created)
# |> put_resp_header("location", ~p"/api/biddings/#{bidding}")
# |> render(:show, bidding: bidding)
# end
# end
# def show(conn, %{"id" => id}) do
# bidding = Biddings.get_bidding!(id)
# render(conn, :show, bidding: bidding)
# end
# def update(conn, %{"id" => id, "bidding" => bidding_params}) do
# bidding = Biddings.get_bidding!(id)
# with {:ok, %Bidding{} = bidding} <- Biddings.update_bidding(bidding, bidding_params) do
# render(conn, :show, bidding: bidding)
# end
# end
# def delete(conn, %{"id" => id}) do
# bidding = Biddings.get_bidding!(id)
# with {:ok, %Bidding{}} <- Biddings.delete_bidding(bidding) do
# send_resp(conn, :no_content, "")
# end
# end
end

View File

@ -0,0 +1,28 @@
defmodule BeetRoundServerWeb.BiddingJSON do
alias BeetRoundServer.Biddings.Bidding
@doc """
Renders a list of biddings.
"""
def index(%{biddings: biddings}) do
%{data: for(bidding <- biddings, do: data(bidding))}
end
@doc """
Renders a single bidding.
"""
def show(%{bidding: bidding}) do
%{data: data(bidding)}
end
defp data(%Bidding{} = bidding) do
%{
user_id: bidding.user_id,
id: bidding.id,
bidding_round: bidding.bidding_round,
amount: bidding.amount,
depot_wish_one: bidding.depot_wish_one,
depot_wish_two: bidding.depot_wish_two
}
end
end

View File

@ -3,22 +3,62 @@ defmodule BeetRoundServerWeb.BiddingRoundController do
alias BeetRoundServer.BiddingRounds
alias BeetRoundServer.BiddingRounds.BiddingRound
alias BeetRoundServer.BiddingRounds.BiddingRoundFacade
action_fallback BeetRoundServerWeb.FallbackController
def get_highest(conn, _params) do
BiddingRoundFacade.restart_if_necessary()
last_round = BiddingRoundFacade.get_highest_bidding_round()
conn
|> render(:show, bidding_round: last_round)
end
def start_new(conn, _params) do
BiddingRoundFacade.start_new_round()
current_round = BiddingRounds.get_highest_bidding_round!()
conn
|> put_status(:created)
|> render(:show, bidding_round: current_round)
end
def restart(conn, _params) do
BiddingRoundFacade.restart_hightest_round()
current_round = BiddingRounds.get_highest_bidding_round!()
conn
|> put_status(:created)
|> render(:show, bidding_round: current_round)
end
def stop(conn, _params) do
BiddingRoundFacade.stop_current_round()
stopped_round = BiddingRounds.get_highest_bidding_round!()
conn
|> render(:show, bidding_round: stopped_round)
end
def index(conn, _params) do
bidding_rounds = BiddingRounds.list_bidding_rounds()
render(conn, :index, bidding_rounds: bidding_rounds)
end
def create(conn, %{"bidding_round" => bidding_round_params}) do
with {:ok, %BiddingRound{} = bidding_round} <- BiddingRounds.create_bidding_round(bidding_round_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/bidding_rounds/#{bidding_round}")
|> render(:show, bidding_round: bidding_round)
end
end
# def create(conn, %{"bidding_round" => bidding_round_params}) do
# with {:ok, %BiddingRound{} = bidding_round} <-
# BiddingRounds.create_bidding_round(bidding_round_params) do
# conn
# |> put_status(:created)
# |> put_resp_header("location", ~p"/api/bidding_rounds/#{bidding_round}")
# |> render(:show, bidding_round: bidding_round)
# end
# end
def show(conn, %{"id" => id}) do
bidding_round = BiddingRounds.get_bidding_round!(id)
@ -28,7 +68,8 @@ defmodule BeetRoundServerWeb.BiddingRoundController do
def update(conn, %{"id" => id, "bidding_round" => bidding_round_params}) do
bidding_round = BiddingRounds.get_bidding_round!(id)
with {:ok, %BiddingRound{} = bidding_round} <- BiddingRounds.update_bidding_round(bidding_round, bidding_round_params) do
with {:ok, %BiddingRound{} = bidding_round} <-
BiddingRounds.update_bidding_round(bidding_round, bidding_round_params) do
render(conn, :show, bidding_round: bidding_round)
end
end

View File

@ -19,7 +19,7 @@ defmodule BeetRoundServerWeb.BiddingRoundJSON do
%{
id: bidding_round.id,
round_number: bidding_round.round_number,
running: bidding_round.running
stopped: bidding_round.stopped
}
end
end

View File

@ -2,6 +2,7 @@ defmodule BeetRoundServerWeb.BiddingLive.Index do
use BeetRoundServerWeb, :live_view
alias BeetRoundServer.Biddings
alias BeetRoundServer.BiddingRounds.BiddingRoundFacade
@impl true
def render(assigns) do
@ -16,6 +17,12 @@ defmodule BeetRoundServerWeb.BiddingLive.Index do
</:actions>
</.header>
<%= if @bidding_round == 0 do %>
<p>Keine Bietrunde aktiv.</p>
<% else %>
<p>Aktive Bietrunde: {@bidding_round}</p>
<% end %>
<.table
id="biddings"
rows={@streams.biddings}
@ -50,9 +57,12 @@ defmodule BeetRoundServerWeb.BiddingLive.Index do
Biddings.subscribe_biddings(socket.assigns.current_scope)
end
current_round = BiddingRoundFacade.get_current_round()
{:ok,
socket
|> assign(:page_title, "Listing Biddings")
|> assign(bidding_round: current_round)
|> stream(:biddings, list_biddings(socket.assigns.current_scope))}
end
@ -67,7 +77,8 @@ defmodule BeetRoundServerWeb.BiddingLive.Index do
@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)}
{:noreply,
stream(socket, :biddings, list_biddings(socket.assigns.current_scope), reset: true)}
end
defp list_biddings(current_scope) do

View File

@ -29,7 +29,14 @@ defmodule BeetRoundServerWeb.Router do
get "/", DefaultApiController, :index
resources "/bidding_rounds", BiddingRoundController, except: [:new, :edit]
get "/bidding_rounds/get_current", BiddingRoundController, :get_highest
get "/bidding_rounds/start_new", BiddingRoundController, :start_new
get "/bidding_rounds/restart", BiddingRoundController, :restart
get "/bidding_rounds/stop", BiddingRoundController, :stop
get "/biddings_of_round/:round_number", BiddingController, :biddings_of_round
get "/biddings_of_highest_round", BiddingController, :biddings_of_highest_round
resources "/users", UserController, except: [:new, :edit]
end

View File

@ -0,0 +1,10 @@
defmodule BeetRoundServer.Repo.Migrations.BiddingRoundStatusStoppedInsteadOfRunning do
use Ecto.Migration
def change do
alter table(:bidding_rounds) do
add :stopped, :boolean, default: false, null: false
remove :running
end
end
end

View File

@ -8,7 +8,7 @@ defmodule BeetRoundServer.BiddingRoundsTest do
import BeetRoundServer.BiddingRoundsFixtures
@invalid_attrs %{running: nil, round_number: nil}
@invalid_attrs %{stopped: nil, round_number: nil}
test "list_bidding_rounds/0 returns all bidding_rounds" do
bidding_round = bidding_round_fixture()
@ -21,10 +21,12 @@ defmodule BeetRoundServer.BiddingRoundsTest do
end
test "create_bidding_round/1 with valid data creates a bidding_round" do
valid_attrs = %{running: true, round_number: 42}
valid_attrs = %{stopped: true, round_number: 42}
assert {:ok, %BiddingRound{} = bidding_round} = BiddingRounds.create_bidding_round(valid_attrs)
assert bidding_round.running == true
assert {:ok, %BiddingRound{} = bidding_round} =
BiddingRounds.create_bidding_round(valid_attrs)
assert bidding_round.stopped == true
assert bidding_round.round_number == 42
end
@ -34,23 +36,31 @@ defmodule BeetRoundServer.BiddingRoundsTest do
test "update_bidding_round/2 with valid data updates the bidding_round" do
bidding_round = bidding_round_fixture()
update_attrs = %{running: false, round_number: 43}
update_attrs = %{stopped: false, round_number: 43}
assert {:ok, %BiddingRound{} = bidding_round} = BiddingRounds.update_bidding_round(bidding_round, update_attrs)
assert bidding_round.running == false
assert {:ok, %BiddingRound{} = bidding_round} =
BiddingRounds.update_bidding_round(bidding_round, update_attrs)
assert bidding_round.stopped == false
assert bidding_round.round_number == 43
end
test "update_bidding_round/2 with invalid data returns error changeset" do
bidding_round = bidding_round_fixture()
assert {:error, %Ecto.Changeset{}} = BiddingRounds.update_bidding_round(bidding_round, @invalid_attrs)
assert {:error, %Ecto.Changeset{}} =
BiddingRounds.update_bidding_round(bidding_round, @invalid_attrs)
assert bidding_round == BiddingRounds.get_bidding_round!(bidding_round.id)
end
test "delete_bidding_round/1 deletes the bidding_round" do
bidding_round = bidding_round_fixture()
assert {:ok, %BiddingRound{}} = BiddingRounds.delete_bidding_round(bidding_round)
assert_raise Ecto.NoResultsError, fn -> BiddingRounds.get_bidding_round!(bidding_round.id) end
assert_raise Ecto.NoResultsError, fn ->
BiddingRounds.get_bidding_round!(bidding_round.id)
end
end
test "change_bidding_round/1 returns a bidding_round changeset" do

View File

@ -0,0 +1,98 @@
defmodule BeetRoundServerWeb.BiddingControllerTest do
use BeetRoundServerWeb.ConnCase
import BeetRoundServer.BiddingsFixtures
alias BeetRoundServer.Biddings.Bidding
import BeetRoundServer.AccountsFixtures, only: [user_scope_fixture: 0]
@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 %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all biddings", %{conn: conn} do
conn = get(conn, ~p"/api/biddings")
assert json_response(conn, 200)["data"] == []
end
end
describe "create bidding" do
test "renders bidding when data is valid", %{conn: conn} do
conn = post(conn, ~p"/api/biddings", bidding: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, ~p"/api/biddings/#{id}")
assert %{
"id" => ^id,
"amount" => 42,
"bidding_round" => 42,
"depot_wish_one" => "some depot_wish_one",
"depot_wish_two" => "some depot_wish_two"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/api/biddings", bidding: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update bidding" do
setup [:create_bidding]
test "renders bidding when data is valid", %{conn: conn, bidding: %Bidding{id: id} = bidding} do
conn = put(conn, ~p"/api/biddings/#{bidding}", bidding: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, ~p"/api/biddings/#{id}")
assert %{
"id" => ^id,
"amount" => 43,
"bidding_round" => 43,
"depot_wish_one" => "some updated depot_wish_one",
"depot_wish_two" => "some updated depot_wish_two"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, bidding: bidding} do
conn = put(conn, ~p"/api/biddings/#{bidding}", bidding: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete bidding" do
setup [:create_bidding]
test "deletes chosen bidding", %{conn: conn, bidding: bidding} do
conn = delete(conn, ~p"/api/biddings/#{bidding}")
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, ~p"/api/biddings/#{bidding}")
end
end
end
defp create_bidding(_) do
scope = user_scope_fixture()
bidding = bidding_fixture(scope)
%{bidding: bidding}
end
end

View File

@ -5,14 +5,14 @@ defmodule BeetRoundServerWeb.BiddingRoundControllerTest do
alias BeetRoundServer.BiddingRounds.BiddingRound
@create_attrs %{
running: true,
stopped: true,
round_number: 42
}
@update_attrs %{
running: false,
stopped: false,
round_number: 43
}
@invalid_attrs %{running: nil, round_number: nil}
@invalid_attrs %{stopped: nil, round_number: nil}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
@ -35,7 +35,7 @@ defmodule BeetRoundServerWeb.BiddingRoundControllerTest do
assert %{
"id" => ^id,
"round_number" => 42,
"running" => true
"stopped" => true
} = json_response(conn, 200)["data"]
end
@ -48,7 +48,10 @@ defmodule BeetRoundServerWeb.BiddingRoundControllerTest do
describe "update bidding_round" do
setup [:create_bidding_round]
test "renders bidding_round when data is valid", %{conn: conn, bidding_round: %BiddingRound{id: id} = bidding_round} do
test "renders bidding_round when data is valid", %{
conn: conn,
bidding_round: %BiddingRound{id: id} = bidding_round
} do
conn = put(conn, ~p"/api/bidding_rounds/#{bidding_round}", bidding_round: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
@ -57,7 +60,7 @@ defmodule BeetRoundServerWeb.BiddingRoundControllerTest do
assert %{
"id" => ^id,
"round_number" => 43,
"running" => false
"stopped" => false
} = json_response(conn, 200)["data"]
end

View File

@ -3,6 +3,6 @@ defmodule BeetRoundServerWeb.PageControllerTest do
test "GET /", %{conn: conn} do
conn = get(conn, ~p"/")
assert html_response(conn, 200) =~ "Peace of mind from prototype to production"
assert html_response(conn, 200) =~ "BeetRound · Das grüne Zebra"
end
end

View File

@ -12,7 +12,7 @@ defmodule BeetRoundServer.BiddingRoundsFixtures do
attrs
|> Enum.into(%{
round_number: 42,
running: true
stopped: false
})
|> BeetRoundServer.BiddingRounds.create_bidding_round()