Skip to content

Commit

Permalink
Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
starcraft66 committed Nov 12, 2023
1 parent 7c1391b commit 6acc5f8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 82 deletions.
54 changes: 26 additions & 28 deletions lib/lanpartyseating/logic/reservation_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ defmodule Lanpartyseating.ReservationLogic do
{:ok, badge} = BadgesLogic.get_badge(uid)
{:ok, station} = StationLogic.get_station(station_number)

is_available =
case StationLogic.get_station_status(station).status do
:reserved -> false
:occupied -> false
:broken -> false
:available -> true
end

case is_available do
case StationLogic.is_station_available(station) do
true ->
Logger.debug("Station is available")
now = DateTime.truncate(DateTime.utc_now(), :second)
Expand All @@ -39,6 +31,7 @@ defmodule Lanpartyseating.ReservationLogic do
}) do
{:ok, updated} ->
{:ok, stations} = StationLogic.get_all_stations(now)

Phoenix.PubSub.broadcast(
PubSub,
"station_update",
Expand All @@ -63,10 +56,13 @@ defmodule Lanpartyseating.ReservationLogic do

Logger.debug("Created expiration task for reservation #{updated.id}")
{:ok, updated}

{:error, err} ->
{:error, {:reservation_failed, err}}
end
false ->
Logger.debug("Station is not available")
{:error, "Station is not available"}
{:error, :station_unavailable}
end
end

Expand All @@ -88,28 +84,30 @@ defmodule Lanpartyseating.ReservationLogic do
deleted_at: DateTime.truncate(DateTime.utc_now(), :second)
)

case Repo.update(reservation) do
{:ok, reservation} ->
GenServer.cast(:"expire_reservation_#{res.id}", :terminate)
with {:ok, reservation} <- Repo.update(reservation) do
GenServer.cast(:"expire_reservation_#{res.id}", :terminate)

Endpoint.broadcast!(
"desktop:all",
"cancel_reservation",
%{
station_number: reservation.station.station_number,
# reservation: updated
}
)
Endpoint.broadcast!(
"desktop:all",
"cancel_reservation",
%{
station_number: reservation.station.station_number,
# reservation: updated
}
)

{:ok, stations} = StationLogic.get_all_stations()
{:ok, stations} = StationLogic.get_all_stations()

Phoenix.PubSub.broadcast(
PubSub,
"station_update",
{:stations, stations}
)
Phoenix.PubSub.broadcast(
PubSub,
"station_update",
{:stations, stations}
)

reservation
reservation
else
{:error, err} ->
{:error, {:reservation_failed, err}}
end
end)

Expand Down
7 changes: 6 additions & 1 deletion lib/lanpartyseating/logic/settings_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ defmodule Lanpartyseating.SettingsLogic do
vertical_trailing: vertical_trailing
)

Repo.update(settings)
with {:ok, _updated} <- Repo.update(settings) do
:ok
else
{:error, error} ->
{:error, {:save_settings_failed, error}}
end
end
end
42 changes: 27 additions & 15 deletions lib/lanpartyseating/logic/station_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ defmodule Lanpartyseating.StationLogic do
|> Repo.all()

case stations do
[] -> {:error, "No stations found"}
[] ->
{:error, :no_stations}
_ ->
stations_map =
Enum.map(stations, fn station ->
Expand All @@ -63,18 +64,17 @@ defmodule Lanpartyseating.StationLogic do
is_closed: is_broken
)

update = Repo.update(station)

case update do
{:ok, _} ->
{:ok, stations} = StationLogic.get_all_stations()
Phoenix.PubSub.broadcast(
PubSub,
"station_update",
{:stations, stations}
)
update
_ -> update
with {:ok, update} <- Repo.update(station) do
{:ok, stations} = StationLogic.get_all_stations()
Phoenix.PubSub.broadcast(
PubSub,
"station_update",
{:stations, stations}
)
{:ok, update}
else
{:error, _} ->
{:error, :station_not_found}
end
end

Expand Down Expand Up @@ -143,7 +143,7 @@ defmodule Lanpartyseating.StationLogic do
|> Repo.one()

case station do
nil -> {:error, "Station not found"}
nil -> {:error, :station_not_found}
_ -> {:ok, station}
end
end
Expand All @@ -160,7 +160,8 @@ defmodule Lanpartyseating.StationLogic do
end)
end)

{:ok, Repo.insert_all(Station, positions)}
Repo.insert_all(Station, positions)
:ok
end

def get_station_status(station) do
Expand All @@ -181,6 +182,17 @@ defmodule Lanpartyseating.StationLogic do
end
end

def is_station_available(station) do
%{status: status} = StationLogic.get_station_status(station)

case status do
:reserved -> false
:occupied -> false
:broken -> false
:available -> true
end
end

def get_stations_by_range(start_number, end_number) do
from(s in Station,
order_by: [asc: s.station_number],
Expand Down
76 changes: 40 additions & 36 deletions lib/lanpartyseating/logic/tournaments_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,27 @@ defmodule Lanpartyseating.TournamentsLogic do
def create_tournament(name, start_time, duration) do
end_time = DateTime.add(start_time, duration, :hour, Tzdata.TimeZoneDatabase)

case Repo.insert(%Tournament{start_date: start_time, end_date: end_time, name: name}) do
{:ok, tournament} ->
DynamicSupervisor.start_child(
Lanpartyseating.ExpirationTaskSupervisor,
{Lanpartyseating.Tasks.StartTournament, {tournament.start_date, tournament.id}}
)

DynamicSupervisor.start_child(
Lanpartyseating.ExpirationTaskSupervisor,
{Lanpartyseating.Tasks.ExpireTournament, {tournament.end_date, tournament.id}}
)

{:ok, tournaments} = get_upcoming_tournaments()
Phoenix.PubSub.broadcast(
PubSub,
"tournament_update",
{:tournaments, tournaments}
)
{:ok, tournament}
with {:ok, tournament} <- Repo.insert(%Tournament{start_date: start_time, end_date: end_time, name: name}) do
DynamicSupervisor.start_child(
Lanpartyseating.ExpirationTaskSupervisor,
{Lanpartyseating.Tasks.StartTournament, {tournament.start_date, tournament.id}}
)

DynamicSupervisor.start_child(
Lanpartyseating.ExpirationTaskSupervisor,
{Lanpartyseating.Tasks.ExpireTournament, {tournament.end_date, tournament.id}}
)

{:ok, tournaments} = get_upcoming_tournaments()
Phoenix.PubSub.broadcast(
PubSub,
"tournament_update",
{:tournaments, tournaments}
)
{:ok, tournament}
else
{:error, err} ->
{:error, {:create_tournament_failed, err}}
end
end

Expand All @@ -77,23 +79,25 @@ defmodule Lanpartyseating.TournamentsLogic do
deleted_at: DateTime.truncate(DateTime.utc_now(), :second)
)

case updated = Repo.update(tournament) do
{:ok, _} ->
{:ok, stations} = StationLogic.get_all_stations()
{:ok, tournaments} = get_upcoming_tournaments()
GenServer.cast(:"expire_tournament_#{id}", :terminate)
GenServer.cast(:"start_tournament_#{id}", :terminate)
Phoenix.PubSub.broadcast(
PubSub,
"station_update",
{:stations, stations}
)
Phoenix.PubSub.broadcast(
PubSub,
"tournament_update",
{:tournaments, tournaments}
)
updated
with {:ok, _updated} <- Repo.update(tournament) do
{:ok, stations} = StationLogic.get_all_stations()
{:ok, tournaments} = get_upcoming_tournaments()
GenServer.cast(:"expire_tournament_#{id}", :terminate)
GenServer.cast(:"start_tournament_#{id}", :terminate)
Phoenix.PubSub.broadcast(
PubSub,
"station_update",
{:stations, stations}
)
Phoenix.PubSub.broadcast(
PubSub,
"tournament_update",
{:tournaments, tournaments}
)
:ok
else
{:error, err} ->
{:error, {:delete_failed, err}}
end
end)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lanpartyseating_web/live/settings_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ defmodule LanpartyseatingWeb.SettingsLive do

s = socket.assigns

{:ok, _} = Lanpartyseating.StationLogic.save_station_positions(socket.assigns.table)
:ok = Lanpartyseating.StationLogic.save_station_positions(socket.assigns.table)

{:ok, _} = Lanpartyseating.SettingsLogic.save_settings(
s.rows,
Expand Down
2 changes: 1 addition & 1 deletion lib/lanpartyseating_web/live/tournaments_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule LanpartyseatingWeb.TournamentsLive do
) do
id = String.to_integer(tournament_id)

TournamentsLogic.delete_tournament(id)
:ok = TournamentsLogic.delete_tournament(id)

tournaments =
socket.assigns.tournaments
Expand Down

0 comments on commit 6acc5f8

Please sign in to comment.