Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

大量のスキルパネル作成

create_lots_of_skill_panel.livemd

大量のスキルパネル作成

alias Bright.{Repo, SkillPanels, SkillUnits}

スキルパネルの作成

defmodule TestData do
  def skill_panel(panel_name) do
    %{
      name: "スキルパネル名#{panel_name}",
      skill_classes: [
        %{name: "#{panel_name}-クラス1", class: 1},
        %{name: "#{panel_name}-クラス2", class: 2},
        %{name: "#{panel_name}-クラス3", class: 3}
      ]
    }
  end

  def skill_unit(skill_class, panel_name) do
    %{
      name: "#{panel_name}-スキルユニット(class:#{skill_class.class})",
      skill_categories:
        for x <- 1..3 do
          skill_categories(panel_name, x, skill_class)
        end,
      skill_class_units: [
        %{skill_class_id: skill_class.id, position: 1}
      ]
    }
  end

  def skill_categories(panel_name, no, skill_class) do
    %{
      name: "#{panel_name}-#{no}カテゴリ(class:#{skill_class.class})",
      position: no,
      skills:
        for x <- 1..3 do
          %{name: "#{panel_name}-#{no}-#{x}-スキル(class:#{skill_class.class})", position: x}
        end
    }
  end

  def create_skill_unit(skill_class) do
    for x <- 1..5 do
      {:ok, _skill_unit} = SkillUnits.create_skill_unit(skill_unit(skill_class, x))
    end
  end

  def create_panel(panel_name) do
    {:ok, skill_panel} = SkillPanels.create_skill_panel(skill_panel(panel_name))
    skill_panel.skill_classes |> Enum.each(&amp;create_skill_unit/1)
  end

  def main do
    1..13
    |> Enum.each(&amp;create_panel/1)
  end
end

TestData.main()

Get SkillUnit and preload associations with correct order

[skill_unit | _] =
  SkillUnits.list_skill_units()
  |> Repo.preload(:skill_classes, skill_categories: :skills)

skill_unit.skill_classes