Skip to content

Commit

Permalink
fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
efcasado committed May 26, 2024
1 parent e942767 commit 2c15c5e
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 165 deletions.
35 changes: 29 additions & 6 deletions lib/uof/api/fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ defmodule UOF.API.Fixtures do
all(lang, 0, 1000, filter, map, [])
end

# def changed() do
# end

# def changed_results() do
# end

defp all(lang, start, limit, filter, map, acc) do
case pre_schedule(lang, start, limit) do
[] ->
Expand Down Expand Up @@ -47,4 +41,33 @@ defmodule UOF.API.Fixtures do
error
end
end

# def changed() do
# end

# def changed_results() do
# end

@doc """
Get the details of the given fixture.
"""
def show(id, lang \\ "en") do
# TO-DO: handle codds fixture (eg. codds:competition_group:77739)
case UOF.API.get("/sports/#{lang}/sport_events/#{id}/fixture.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("fixtures_fixture")
|> Map.get("fixture")
|> bubble_up("competitors", "competitor")
|> bubble_up("extra_info", "info")
|> bubble_up("reference_ids", "reference_id")
|> IO.inspect()
|> UOF.API.Fixtures.Fixture.changeset()
|> apply

{:error, _} = error ->
error
end
end
end
21 changes: 21 additions & 0 deletions lib/uof/api/fixtures/extra_info.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule UOF.API.Fixtures.ExtraInfo do
use Ecto.Schema
import Ecto.Changeset
import UOF.API.EctoHelpers

@type t :: %__MODULE__{
key: String.t(),
value: String.t()
}

@primary_key false

embedded_schema do
field :key, :string
field :value, :string
end

def changeset(model \\ %__MODULE__{}, params) do
cast(model, params, [:key, :value])
end
end
67 changes: 67 additions & 0 deletions lib/uof/api/fixtures/fixture.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule UOF.API.Fixtures.Fixture do
@moduledoc false

use Ecto.Schema
import Ecto.Changeset
import UOF.API.EctoHelpers

# attribute(:start_time_confirmed, cast: :boolean)
# attribute(:start_time)
# attribute(:liveodds)
# attribute(:status)
# attribute(:next_live_time)
# attribute(:id)
# attribute(:scheduled)
# attribute(:start_time_tbd, cast: :boolean)
# elements(:competitor, as: :competitors, into: %Competitor{})
# elements(:tv_channel, as: :tv_channels, into: %TVChannel{})
# elements(:info, as: :extra_info, into: %ExtraInfo{})
# elements(:reference_id, as: :references, into: %Reference{})
# element(:tournament_round, into: %TournamentRound{})
# element(:tournament, into: %Tournament{})
# element(:season, into: %Season{})
# element(:venue, into: %Venue{})
# element(:coverage_info, into: %CoverageInfo{})
# element(:product_info, into: %ProductInfo{})

@primary_key false

embedded_schema do
field :start_time_confirmed, :boolean
field :start_time
field :liveodds
field :status
field :next_live_time
field :id
field :scheduled
field :start_time_tbd, :boolean
# field :tv_channels, {:array, :string}
embeds_one :season, UOF.API.Tournaments.Season
embeds_one :tournament, UOF.API.Sports.Tournament
embeds_one :tournament_round, UOF.API.Tournaments.TournamentRound
embeds_many :competitors, UOF.API.Competitors.Competitor
embeds_many :extra_info, UOF.API.Fixtures.ExtraInfo
embeds_many :reference_ids, UOF.API.Fixtures.ReferenceId
end

def changeset(model \\ %__MODULE__{}, params) do
model
|> cast(params, [
:start_time_confirmed,
:start_time,
:liveodds,
:status,
:next_live_time,
:id,
:scheduled,
:start_time_tbd
# :tv_channels
])
|> cast_embed(:season)
|> cast_embed(:tournament)
|> cast_embed(:tournament_round)
|> cast_embed(:competitors)
|> cast_embed(:extra_info)
|> cast_embed(:reference_ids)
end
end
21 changes: 21 additions & 0 deletions lib/uof/api/fixtures/reference_id.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule UOF.API.Fixtures.ReferenceId do
use Ecto.Schema
import Ecto.Changeset
import UOF.API.EctoHelpers

@type t :: %__MODULE__{
name: String.t(),
value: String.t()
}

@primary_key false

embedded_schema do
field :name, :string
field :value, :string
end

def changeset(model \\ %__MODULE__{}, params) do
cast(model, params, [:name, :value])
end
end
32 changes: 0 additions & 32 deletions lib/uof/api/fixtures/venue.ex

This file was deleted.

46 changes: 0 additions & 46 deletions lib/uof/api/sports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,53 +50,7 @@ defmodule UOF.API.Sports do
end
end

## Auxiliary functions
## =========================================================================
def fixtures(filter \\ & &1, map \\ & &1) do
fixtures(0, 1000, filter, map, [])
end

defp fixtures(start, limit, filter, map, acc) do
{:ok, schedule} = pre_schedule(start, limit)

case schedule.events do
[] ->
acc

events ->
# TO-DO: Return subset of fields (eg. only fixture ids)
# events = maybe_filter_fixtures(events, filter)
events = for e <- events, filter.(e), do: map.(e)
fixtures(start + limit, limit, filter, map, events ++ acc)
end
end

# defp maybe_filter_fixtures(fixtures, nil) do
# fixtures
# end
# defp maybe_filter_fixtures(fixtures, filter) do
# Enum.filter(fixtures, filter)
# end

def fixtures_by_sport(sports) when is_list(sports) do
fixtures(&(&1.tournament.sport.name in sports))
end

def fixtures_by_sport(sport) do
fixtures_by_sport([sport])
end

## =========================================================================

@doc """
Get the details of the given fixture.
"""
def fixture(fixture, lang \\ "en") do
# TO-DO: handle codds fixture (eg. codds:competition_group:77739)
endpoint = ["sports", lang, "sport_events", fixture, "fixture.xml"]

HTTP.get(%UOF.API.Mappings.FixturesFixture{}, endpoint)
end

@doc """
Get a list of all the fixtures scheduled to start at the given date (in UTC).
Expand Down
2 changes: 1 addition & 1 deletion lib/uof/api/tournaments/round.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule UOF.API.Tournaments.Round do
field :cup_round_match_number, :integer
end

def changeset(%__MODULE__{} = model, params) do
def changeset(model \\ %__MODULE__{}, params) do
model
|> cast(params, [:type, :name, :number, :cup_round_matches, :cup_round_match_number])
end
Expand Down
3 changes: 2 additions & 1 deletion lib/uof/api/tournaments/season.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ defmodule UOF.API.Tournaments.Season do
field :year, :string
field :id, :string
field :name, :string
field :tournament_id, :string
end

def changeset(%__MODULE__{} = model, params) do
model
|> cast(params, [:id, :name, :start_date, :end_date, :year])
|> cast(params, [:id, :name, :start_date, :end_date, :year, :tournament_id])
end
end
44 changes: 44 additions & 0 deletions lib/uof/api/tournaments/tournament_round.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule UOF.API.Tournaments.TournamentRound do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset

@primary_key false

embedded_schema do
field :betradar_id, :integer
field :betradar_name
field :type, :string
# cup
field :name, :string
field :cup_round_matches, :integer
field :cup_round_match_number, :integer
field :other_match_id
# group
field :number
field :group
field :group_id
field :group_long_name
# qualification
# ???
field :phase
end

def changeset(model \\ %__MODULE__{}, params) do
model
|> cast(params, [
:betradar_id,
:betradar_name,
:type,
:name,
:number,
:cup_round_matches,
:cup_round_match_number,
:other_match_id,
:group,
:group_id,
:group_long_name,
:phase
])
end
end
Loading

0 comments on commit 2c15c5e

Please sign in to comment.