Skip to content

Commit

Permalink
standardize
Browse files Browse the repository at this point in the history
  • Loading branch information
efcasado committed May 12, 2024
1 parent db39955 commit a89a487
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 225 deletions.
7 changes: 1 addition & 6 deletions lib/uof/api/descriptions/bet_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,10 @@ defmodule UOF.API.Descriptions.BetStop do
end

def changeset(model \\ %__MODULE__{}, params) do
params = prepare(params)
params = sanitize(params)

model
|> cast(params, [:id, :description])
|> apply
end

defp prepare(params) do
params
|> rename_fields
end
end
7 changes: 1 addition & 6 deletions lib/uof/api/descriptions/betting_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,10 @@ defmodule UOF.API.Descriptions.BettingStatus do
end

def changeset(model \\ %__MODULE__{}, params) do
params = prepare(params)
params = sanitize(params)

model
|> cast(params, [:id, :description])
|> apply
end

defp prepare(params) do
params
|> rename_fields
end
end
131 changes: 32 additions & 99 deletions lib/uof/api/descriptions/market.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,121 +51,54 @@ defmodule UOF.API.Descriptions.Market do
end
end

defmodule Outcome do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
import UOF.API.EctoHelpers

@primary_key false

embedded_schema do
field(:id, :integer)
field(:name, :string)
end
@primary_key false

def changeset(model \\ %__MODULE__{}, params) do
cast(model, prepare(params), [:id, :name])
end
embedded_schema do
field :id, :integer
field :name, :string
field :groups, {:array, :string}
# ["player", "competitor", "free_text"]
field :outcome_type, :string
field :includes_outcomes_of_type, :string

defp prepare(params) do
params
|> rename_fields
embeds_many :outcomes, Outcome, primary_key: false do
field :id, :integer
field :name, :string
end
end

defmodule Specifier do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
import UOF.API.EctoHelpers

@primary_key false

embedded_schema do
embeds_many :specifiers, Specifier, primary_key: false do
# ["decimal", "integer", "string", "variable_text"]
field(:type, :string)
field(:name, :string)
end

def changeset(model \\ %__MODULE__{}, params) do
cast(model, prepare(params), [:name, :type])
field :type, :string
field :name, :string
end

defp prepare(params) do
params
|> rename_fields
end
end

@primary_key false

embedded_schema do
field(:id, :integer)
field(:name, :string)
field(:groups, {:array, :string})
# ["player", "competitor", "free_text"]
field(:outcome_type, :string)
field(:includes_outcomes_of_type, :string)
embeds_many(:outcomes, Outcome)
embeds_many(:specifiers, Specifier)
# TO-DO: embeds_many(:mappings, Mapping)
end

def changeset(model \\ %__MODULE__{}, params) do
model
|> cast(prepare(params), [:id, :name, :groups, :outcome_type, :includes_outcomes_of_type])
|> cast_embed(:outcomes)
|> cast_embed(:specifiers)
|> case do
%Ecto.Changeset{valid?: true} = changeset ->
{:ok, apply_changes(changeset)}
def changeset(model \\ %__MODULE__{}, params)

%Ecto.Changeset{} = changeset ->
{:error, {params, traverse_errors(changeset)}}
end
end

defp prepare(params) do
params
|> rename_fields
|> prepare_outcomes
|> prepare_specifiers
|> split_groups
end

defp prepare_outcomes(params) do
outcomes =
def changeset(%__MODULE__{} = model, params) do
params =
params
|> Map.get("outcomes", %{})
|> Map.get("outcome", [])

case outcomes do
outcome when not is_list(outcome) ->
Map.put(params, "outcomes", [outcome])
|> sanitize
|> bubble_up("outcomes", "outcome")
|> bubble_up("specifiers", "specifier")
|> split("groups", "|")

_ ->
Map.put(params, "outcomes", outcomes)
end
model
|> cast(params, [:id, :name, :groups, :outcome_type, :includes_outcomes_of_type])
|> cast_embed(:outcomes, with: &changeset/2)
|> cast_embed(:specifiers, with: &changeset/2)
|> apply
end

defp prepare_specifiers(params) do
specifiers =
params
|> Map.get("specifiers", %{})
|> Map.get("specifier", [])

case specifiers do
specifier when not is_list(specifier) ->
Map.put(params, "specifiers", [specifier])

_ ->
Map.put(params, "specifiers", specifiers)
end
def changeset(%UOF.API.Descriptions.Market.Outcome{} = model, params) do
params = sanitize(params)
cast(model, params, [:id, :name])
end

defp split_groups(params) do
scope = String.split(params["groups"], "|")
Map.put(params, "groups", scope)
def changeset(%UOF.API.Descriptions.Market.Specifier{} = model, params) do
params = sanitize(params)
cast(model, params, [:type, :name])
end
end
26 changes: 10 additions & 16 deletions lib/uof/api/descriptions/match_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,27 @@ defmodule UOF.API.Descriptions.MatchStatus do
end
end

def changeset(model \\ %__MODULE__{}, params) do
params = prepare(params)
def changeset(model \\ %__MODULE__{}, params)

def changeset(%__MODULE__{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:id, :description, :period_number])
|> cast_embed(:sports, with: &sport_changeset/2)
|> cast_embed(:sports, with: &changeset/2)
|> apply
end

defp prepare(params) do
params
|> rename_fields
end

def sport_changeset(model \\ %__MODULE__{}, params) do
params = prepare_sport(params)
def changeset(%UOF.API.Descriptions.MatchStatus.Sports{} = model, params) do
params =
params
|> sanitize
|> prepare_ids

model
|> cast(params, [:all, :ids])
end

defp prepare_sport(params) do
params
|> rename_fields
|> prepare_ids
end

defp prepare_ids(params) do
ids =
params
Expand Down
26 changes: 7 additions & 19 deletions lib/uof/api/descriptions/producers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ defmodule UOF.API.Descriptions.Producer do
end

def changeset(model \\ %__MODULE__{}, params) do
params =
params
|> sanitize
|> split("scope", "|")

model
|> cast(prepare(params), [
|> cast(params, [
:id,
:name,
:description,
Expand All @@ -51,23 +56,6 @@ defmodule UOF.API.Descriptions.Producer do
:scope,
:stateful_recovery_window_in_minutes
])
|> case do
%Ecto.Changeset{valid?: true} = changeset ->
{:ok, apply_changes(changeset)}

%Ecto.Changeset{} = changeset ->
{:error, {params, traverse_errors(changeset)}}
end
end

defp prepare(params) do
params
|> rename_fields
|> prepare_scope
end

defp prepare_scope(params) do
scope = String.split(params["scope"], "|")
Map.put(params, "scope", scope)
|> apply
end
end
86 changes: 21 additions & 65 deletions lib/uof/api/descriptions/variant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,89 +50,45 @@ defmodule UOF.API.Descriptions.Variant do
end
end

def changeset(model \\ %__MODULE__{}, params) do
params = prepare(params)
def changeset(model \\ %__MODULE__{}, params)

def changeset(%__MODULE__{} = model, params) do
params =
params
|> sanitize
|> bubble_up("outcomes", "outcome")
|> bubble_up("mappings", "mapping")

model
|> cast(params, [:id])
|> cast_embed(:outcomes, with: &outcome_changeset/2)
|> cast_embed(:mappings, with: &mapping_changeset/2)
|> cast_embed(:outcomes, with: &changeset/2)
|> cast_embed(:mappings, with: &changeset/2)
|> apply
end

defp prepare(params) do
params
|> rename_fields
|> prepare_outcomes
|> prepare_mappings
end

defp prepare_outcomes(params) do
outcomes =
params
|> Map.get("outcomes", %{})
|> Map.get("outcome", [])

case outcomes do
outcome when not is_list(outcome) ->
Map.put(params, "outcomes", [outcome])

_ ->
Map.put(params, "outcomes", outcomes)
end
end

defp prepare_mappings(params) do
mappings =
params
|> Map.get("mappings", %{})
|> Map.get("mapping", [])

case mappings do
mapping when not is_list(mapping) ->
Map.put(params, "mappings", [mapping])

_ ->
Map.put(params, "mappings", mappings)
end
end

def outcome_changeset(model, params) do
params = prepare_outcome(params)
def changeset(%UOF.API.Descriptions.Variant.Outcome{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:id, :name])
end

def prepare_outcome(params) do
params
|> rename_fields
end

def mapping_changeset(model, params) do
params = prepare_mapping(params)
def changeset(%UOF.API.Descriptions.Variant.Mapping{} = model, params) do
params =
params
|> sanitize
|> split("product_ids", "|")
|> rename("mapping_outcome", "outcome_mappings", [])

model
|> cast(params, [:product_id, :product_ids, :sport_id, :market_id, :product_market_id])
|> cast_embed(:outcome_mappings, with: &outcome_mapping_changeset/2)
end

def prepare_mapping(params) do
params
|> rename_fields
|> split("product_ids", "|")
|> rename("mapping_outcome", "outcome_mappings", [])
|> cast_embed(:outcome_mappings, with: &changeset/2)
end

def outcome_mapping_changeset(model, params) do
params = prepare_outcome_mapping(params)
def changeset(%UOF.API.Descriptions.Variant.Mapping.OutcomeMapping{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:outcome_id, :product_outcome_id, :product_outcome_name])
end

def prepare_outcome_mapping(params) do
params
|> rename_fields
end
end
Loading

0 comments on commit a89a487

Please sign in to comment.