diff --git a/lib/lanpartyseating/logic/reservation_logic.ex b/lib/lanpartyseating/logic/reservation_logic.ex index 2ce20ca..03ece6b 100644 --- a/lib/lanpartyseating/logic/reservation_logic.ex +++ b/lib/lanpartyseating/logic/reservation_logic.ex @@ -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) @@ -39,6 +31,7 @@ defmodule Lanpartyseating.ReservationLogic do }) do {:ok, updated} -> {:ok, stations} = StationLogic.get_all_stations(now) + Phoenix.PubSub.broadcast( PubSub, "station_update", @@ -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 @@ -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) diff --git a/lib/lanpartyseating/logic/settings_logic.ex b/lib/lanpartyseating/logic/settings_logic.ex index 3ab73e4..3763d46 100644 --- a/lib/lanpartyseating/logic/settings_logic.ex +++ b/lib/lanpartyseating/logic/settings_logic.ex @@ -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 diff --git a/lib/lanpartyseating/logic/station_logic.ex b/lib/lanpartyseating/logic/station_logic.ex index e663ebf..8d7e888 100644 --- a/lib/lanpartyseating/logic/station_logic.ex +++ b/lib/lanpartyseating/logic/station_logic.ex @@ -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 -> @@ -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 @@ -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 @@ -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 @@ -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], diff --git a/lib/lanpartyseating/logic/tournaments_logic.ex b/lib/lanpartyseating/logic/tournaments_logic.ex index 5ebbe02..7eeec95 100644 --- a/lib/lanpartyseating/logic/tournaments_logic.ex +++ b/lib/lanpartyseating/logic/tournaments_logic.ex @@ -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 @@ -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 diff --git a/lib/lanpartyseating_web/live/settings_live.ex b/lib/lanpartyseating_web/live/settings_live.ex index 40dd5c4..b327aee 100644 --- a/lib/lanpartyseating_web/live/settings_live.ex +++ b/lib/lanpartyseating_web/live/settings_live.ex @@ -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, diff --git a/lib/lanpartyseating_web/live/tournaments_live.ex b/lib/lanpartyseating_web/live/tournaments_live.ex index fdc9e5f..3b0ff2c 100644 --- a/lib/lanpartyseating_web/live/tournaments_live.ex +++ b/lib/lanpartyseating_web/live/tournaments_live.ex @@ -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