Skip to content

Commit

Permalink
Use handle event to create draft picks & chat
Browse files Browse the repository at this point in the history
* Allow admin to create a chat from championship page
* Update action to create draft picks to use handle event
* Delete old draft order controller
* Closes #1299
  • Loading branch information
axelclark committed Apr 11, 2024
1 parent 33e3c3d commit b5c573c
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 151 deletions.
26 changes: 26 additions & 0 deletions lib/ex338/fantasy_leagues.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Ex338.FantasyLeagues do
import Ecto.Query

alias Ex338.Championships.Championship
alias Ex338.Chats
alias Ex338.DraftPicks
alias Ex338.FantasyLeagues.FantasyLeague
alias Ex338.FantasyLeagues.FantasyLeagueDraft
Expand All @@ -16,6 +17,25 @@ defmodule Ex338.FantasyLeagues do
FantasyLeague.changeset(fantasy_league, attrs)
end

def create_draft_chat_for_championship(
%FantasyLeague{} = fantasy_league,
%Championship{} = championship
) do
room_name = "#{fantasy_league.fantasy_league_name} #{championship.title}"

Repo.transact(fn ->
with {:ok, chat} <- Chats.create_chat(%{room_name: room_name}),
{:ok, fantasy_league_championship} <-
create_fantasy_league_draft(%{
fantasy_league_id: fantasy_league.id,
chat_id: chat.id,
championship_id: championship.id
}) do
{:ok, chat: chat, fantasy_league_championship: fantasy_league_championship}
end
end)
end

def create_future_picks_for_league(league_id, draft_rounds) do
league_id
|> FantasyTeams.list_teams_for_league()
Expand Down Expand Up @@ -102,6 +122,12 @@ defmodule Ex338.FantasyLeagues do
|> Repo.insert!()
end

def create_fantasy_league_draft(attrs) do
%FantasyLeagueDraft{}
|> FantasyLeagueDraft.changeset(attrs)
|> Repo.insert()
end

def get_draft_by_league_and_championship(
%FantasyLeague{} = fantasy_league,
%Championship{} = championship
Expand Down
21 changes: 21 additions & 0 deletions lib/ex338/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,25 @@ defmodule Ex338.Repo do
use Ecto.Repo,
otp_app: :ex338,
adapter: Ecto.Adapters.Postgres

@doc """
A small wrapper around `Repo.transaction/2'.
Commits the transaction if the lambda returns `:ok` or `{:ok, result}`,
rolling it back if the lambda returns `:error` or `{:error, reason}`. In both
cases, the function returns the result of the lambda.
"""
def transact(fun, opts \\ []) do
transaction(
fn ->
case fun.() do
{:ok, value} -> value
:ok -> :transaction_commited
{:error, reason} -> rollback(reason)
:error -> rollback(:transaction_rollback_error)
end
end,
opts
)
end
end
25 changes: 0 additions & 25 deletions lib/ex338_web/controllers/in_season_draft_order_controller.ex

This file was deleted.

2 changes: 1 addition & 1 deletion lib/ex338_web/live/championship_live/chat_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Ex338Web.ChampionshipLive.ChatComponent do
<div class="flex gap-x-3 pt-3 px-4 sm:px-6">
<.user_icon name={@current_user.name} class="!mt-0" />
<.form
id="create-message"
id="create-message-form"
for={@form}
phx-target={@myself}
phx-change="validate"
Expand Down
134 changes: 106 additions & 28 deletions lib/ex338_web/live/championship_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ defmodule Ex338Web.ChampionshipLive.Show do
_ ->
socket
|> assign(:chat, nil)
|> assign(:message, nil)
|> assign(:users, [])
|> stream(:messages, [])
end
Expand Down Expand Up @@ -119,6 +120,63 @@ defmodule Ex338Web.ChampionshipLive.Show do
)
end

@impl true

def handle_event(
"create_draft_picks",
_params,
%{assigns: %{current_user: %{admin: true}}} = socket
) do
%{fantasy_league: fantasy_league, championship: championship} = socket.assigns

case InSeasonDraftPicks.create_picks_for_league(fantasy_league.id, championship.id) do
{:ok, new_picks} ->
{:noreply,
socket
|> put_flash(:info, "#{inspect(Enum.count(new_picks))} picks successfully created.")
|> push_patch(to: show_path(fantasy_league, championship))}

{:error, _, changeset, _} ->
{:noreply,
socket
|> put_flash(:error, "Error when creating draft picks: #{inspect(changeset.errors)}")
|> push_patch(to: show_path(fantasy_league, championship))}
end
end

def handle_event(
"create_draft_chat",
_params,
%{assigns: %{current_user: %{admin: true}}} = socket
) do
%{fantasy_league: fantasy_league, championship: championship} = socket.assigns

case FantasyLeagues.create_draft_chat_for_championship(fantasy_league, championship) do
{:ok, _chat_and_fantasy_league_draft} ->
{:noreply,
socket
|> put_flash(:info, "Successfully created chat for in season draft")
|> push_patch(to: show_path(fantasy_league, championship))}

{:error, changeset} ->
{:noreply,
socket
|> put_flash(
:error,
"Error when creating draft chat: #{inspect(changeset.errors)}"
)
|> push_patch(to: show_path(fantasy_league, championship))}
end
end

def handle_event(event, _params, socket) do
Logger.info(
"Unhandled event: #{inspect(event)} for current user #{socket.assigns.current_user.id || "nil"}"
)

{:noreply, socket}
end

@impl true
def handle_info(:refresh, socket) do
championship = Championships.update_next_in_season_pick(socket.assigns.championship)
Expand Down Expand Up @@ -215,35 +273,47 @@ defmodule Ex338Web.ChampionshipLive.Show do
</div>
</h3>
</div>
<%= if show_create_slots(@current_user, @championship) do %>
<div class="flex-shrink-0 mt-2 ml-4">
<.link
href={
~p"/fantasy_leagues/#{@fantasy_league.id}/championship_slot_admin?#{%{championship_id: @championship.id}}"
}
class="bg-transparent hover:bg-indigo-500 text-indigo-600 text-sm font-medium hover:text-white py-2 px-4 border border-indigo-600 hover:border-transparent rounded"
method="post"
data-confirm="Please confirm to create roster slots"
>
Create Roster Slots
</.link>
</div>
<% end %>
<div class="flex items-center">
<%= if show_create_slots(@current_user, @championship) do %>
<div class="flex-shrink-0 mt-2 ml-4">
<.link
href={
~p"/fantasy_leagues/#{@fantasy_league.id}/championship_slot_admin?#{%{championship_id: @championship.id}}"
}
class="bg-transparent hover:bg-indigo-500 text-indigo-600 text-sm font-medium hover:text-white py-2 px-4 border border-indigo-600 hover:border-transparent rounded"
method="post"
data-confirm="Please confirm to create roster slots"
>
Create Roster Slots
</.link>
</div>
<% end %>
<%= if show_create_picks(@current_user, @championship) do %>
<div class="flex-shrink-0 mt-2 ml-4">
<.link
href={
~p"/fantasy_leagues/#{@fantasy_league.id}/in_season_draft_order?#{%{championship_id: @championship.id}}"
}
class="bg-transparent hover:bg-indigo-500 text-indigo-600 text-sm font-medium hover:text-white py-2 px-4 border border-indigo-600 hover:border-transparent rounded"
method="post"
data-confirm="Please confirm to create draft picks"
>
Create Draft Picks
</.link>
</div>
<% end %>
<%= if show_create_picks(@current_user, @championship) do %>
<div class="flex-shrink-0 mt-2 ml-4">
<button
id="create-draft-picks-button"
type="button"
phx-click="create_draft_picks"
class="bg-transparent hover:bg-indigo-500 text-indigo-600 text-sm font-medium hover:text-white py-2 px-4 border border-indigo-600 hover:border-transparent rounded"
>
Create Draft Picks
</button>
</div>
<% end %>
<%= if show_create_chat(@current_user, @chat) do %>
<div class="flex-shrink-0 mt-2 ml-4">
<button
id="create-chat-button"
type="button"
phx-click="create_draft_chat"
class="bg-transparent hover:bg-indigo-500 text-indigo-600 text-sm font-medium hover:text-white py-2 px-4 border border-indigo-600 hover:border-transparent rounded"
>
Create Chat
</button>
</div>
<% end %>
</div>
</div>
</div>
<div class="px-4 py-5 sm:p-0">
Expand Down Expand Up @@ -769,6 +839,14 @@ defmodule Ex338Web.ChampionshipLive.Show do
"-"
end

defp show_create_chat(%{admin: true}, nil) do
true
end

defp show_create_chat(_user, _chat) do
false
end

defp show_create_slots(%{admin: true}, %{category: "event", championship_slots: []}) do
true
end
Expand Down
1 change: 0 additions & 1 deletion lib/ex338_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ defmodule Ex338Web.Router do

resources "/fantasy_leagues", FantasyLeagueController, only: [] do
resources("/championship_slot_admin", ChampionshipSlotAdminController, only: [:create])
resources("/in_season_draft_order", InSeasonDraftOrderController, only: [:create])
resources("/injured_reserves", InjuredReserveController, only: [:update])
end
end
Expand Down
16 changes: 16 additions & 0 deletions test/ex338/fantasy_leagues_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ defmodule Ex338.FantasyLeaguesTest do
alias Ex338.FantasyLeagues.FantasyLeague
alias Ex338.FantasyLeagues.FantasyLeagueDraft

describe "create_draft_chat_for_championship/2" do
test "creates fantasy league draft and draft chat" do
fantasy_league = insert(:fantasy_league)

championship =
insert(:championship, category: "overall", in_season_draft: true)

{:ok, chat: chat, fantasy_league_championship: fantasy_league_championship} =
FantasyLeagues.create_draft_chat_for_championship(fantasy_league, championship)

assert fantasy_league_championship.fantasy_league_id == fantasy_league.id
assert fantasy_league_championship.championship_id == championship.id
assert fantasy_league_championship.chat_id == chat.id
end
end

test "change_fantasy_league/1 returns a fantasy_league changeset" do
fantasy_league = insert(:fantasy_league)
assert %Ecto.Changeset{} = FantasyLeagues.change_fantasy_league(fantasy_league)
Expand Down

This file was deleted.

Loading

0 comments on commit b5c573c

Please sign in to comment.