Skip to content

Commit

Permalink
Add link redirects for competitor page
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Sep 5, 2024
1 parent 9ffd912 commit a7feb2f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/wca_live/competitions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ defmodule WcaLive.Competitions do
@spec fetch_person(term()) :: {:ok, %Person{}} | {:error, Ecto.Queryable.t()}
def fetch_person(id), do: Repo.fetch(Person, id)

@doc """
Finds person by competition-scoped registrant id.
"""
@spec get_person_by_registrant_id!(term(), pos_integer()) :: %Person{}
def get_person_by_registrant_id!(competition_id, registrant_id) do
Repo.one!(
from person in Person,
where: person.competition_id == ^competition_id and person.registrant_id == ^registrant_id
)
end

@doc """
Finds person by WCA ID.
"""
@spec get_person_by_wca_id!(term(), String.t()) :: %Person{}
def get_person_by_wca_id!(competition_id, wca_id) do
Repo.one!(
from person in Person,
where: person.competition_id == ^competition_id and person.wca_id == ^wca_id
)
end

@doc """
Updates `competition` with `attrs`.
Expand Down
22 changes: 22 additions & 0 deletions lib/wca_live_web/controllers/link_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@ defmodule WcaLiveWeb.LinkController do

redirect(conn, to: "/competitions/#{competition.id}/rounds/#{round.id}")
end

def competitor(conn, %{"wca_id" => wca_id, "registrant_id_or_wca_id" => registrant_id_or_wca_id}) do
competition = Competitions.get_competition_by_wca_id!(wca_id)

person =
case parse_registrant_id_or_wca_id(registrant_id_or_wca_id) do
{:registrant_id, registrant_id} ->
Competitions.get_person_by_registrant_id!(competition.id, registrant_id)

{:wca_id, wca_id} ->
Competitions.get_person_by_wca_id!(competition.id, wca_id)
end

redirect(conn, to: "/competitions/#{competition.id}/competitors/#{person.id}")
end

defp parse_registrant_id_or_wca_id(id) do
case Integer.parse(id) do
{registrant_id, ""} -> {:registrant_id, registrant_id}
_ -> {:wca_id, id}
end
end
end
1 change: 1 addition & 0 deletions lib/wca_live_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule WcaLiveWeb.Router do
scope "/link", WcaLiveWeb do
get "/competitions/:wca_id", LinkController, :competition
get "/competitions/:wca_id/rounds/:event_id/:round_number", LinkController, :round
get "/competitions/:wca_id/competitors/:registrant_id_or_wca_id", LinkController, :competitor
end

scope "/api", WcaLiveWeb do
Expand Down
73 changes: 73 additions & 0 deletions test/wca_live_web/controllers/link_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
defmodule WcaLiveWeb.LinkControllerTest do
use WcaLiveWeb.ConnCase

import WcaLive.Factory

describe "competition" do
test "redirects by wca id", %{conn: conn} do
competition = insert(:competition)

conn = get(conn, "/link/competitions/#{competition.wca_id}")

assert redirected_to(conn) == "/competitions/#{competition.id}"
end

test "responds with error when no competition is found", %{conn: conn} do
assert_error_sent 404, fn ->
get(conn, "/link/competitions/NONEXISTENT")
end
end
end

describe "round" do
test "redirects by event and round number", %{conn: conn} do
competition = insert(:competition)
competition_event = insert(:competition_event, competition: competition, event_id: "333")
round = insert(:round, competition_event: competition_event, number: 1)

conn = get(conn, "/link/competitions/#{competition.wca_id}/rounds/333/1")

assert redirected_to(conn) == "/competitions/#{competition.id}/rounds/#{round.id}"
end

test "responds with error when no round is found", %{conn: conn} do
competition = insert(:competition)

assert_error_sent 404, fn ->
get(conn, "/link/competitions/#{competition.wca_id}/rounds/333/1")
end
end
end

describe "competitor" do
test "redirects by registrant id", %{conn: conn} do
competition = insert(:competition)
person = insert(:person, competition: competition, registrant_id: 1)

conn = get(conn, "/link/competitions/#{competition.wca_id}/competitors/1")

assert redirected_to(conn) == "/competitions/#{competition.id}/competitors/#{person.id}"
end

test "redirects by wca id", %{conn: conn} do
competition = insert(:competition)
person = insert(:person, competition: competition, wca_id: "2020OTHR01")

conn = get(conn, "/link/competitions/#{competition.wca_id}/competitors/2020OTHR01")

assert redirected_to(conn) == "/competitions/#{competition.id}/competitors/#{person.id}"
end

test "responds with error when no person is found", %{conn: conn} do
competition = insert(:competition)

assert_error_sent 404, fn ->
get(conn, "/link/competitions/#{competition.wca_id}/competitors/1")
end

assert_error_sent 404, fn ->
get(conn, "/link/competitions/#{competition.wca_id}/competitors/2020OTHR01")
end
end
end
end

0 comments on commit a7feb2f

Please sign in to comment.