From 6bce59dba97887275186f64bff466bc34ba8057b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Mon, 4 Nov 2024 00:59:40 +0000 Subject: [PATCH] feat: speaker seeds --- .../schedule_live/form_component.ex | 5 ++- .../live/backoffice/schedule_live/index.ex | 9 +---- mix.exs | 1 + mix.lock | 1 + priv/repo/seeds/activities.exs | 40 ++++++++++++++++++- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lib/safira_web/live/backoffice/schedule_live/form_component.ex b/lib/safira_web/live/backoffice/schedule_live/form_component.ex index 9342e074..0a86d0be 100644 --- a/lib/safira_web/live/backoffice/schedule_live/form_component.ex +++ b/lib/safira_web/live/backoffice/schedule_live/form_component.ex @@ -173,7 +173,10 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.FormComponent do defp save_activity(socket, :new, activity_params) do case Activities.create_activity(activity_params) do {:ok, activity} -> - case Activities.upsert_activity_speakers(activity, activity_params["speakers"]) do + case Activities.upsert_activity_speakers( + Map.put(activity, :speakers, []), + activity_params["speakers"] + ) do {:ok, _activity} -> {:noreply, socket diff --git a/lib/safira_web/live/backoffice/schedule_live/index.ex b/lib/safira_web/live/backoffice/schedule_live/index.ex index f0c2040b..a2ca9d90 100644 --- a/lib/safira_web/live/backoffice/schedule_live/index.ex +++ b/lib/safira_web/live/backoffice/schedule_live/index.ex @@ -48,7 +48,7 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.Index do defp apply_action(socket, :new, _params) do socket |> assign(:page_title, "New Activity") - |> assign(:activity, %Activity{}) + |> assign(:activity, %Activity{speakers: []}) |> assign(:categories, Activities.list_activity_categories()) end @@ -77,12 +77,7 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.Index do defp apply_action(socket, :speakers_edit, %{"id" => id}) do socket |> assign(:page_title, "Edit Speaker") - |> assign( - :speaker, - Map.update!(Activities.get_speaker!(id), :speakers, fn speakers -> - speakers |> Enum.map(&{&1.name, &1.id}) - end) - ) + |> assign(:speaker, Activities.get_speaker!(id)) end defp apply_action(socket, :speakers_new, _params) do diff --git a/mix.exs b/mix.exs index 62431884..4a5c8a67 100644 --- a/mix.exs +++ b/mix.exs @@ -59,6 +59,7 @@ defmodule Safira.MixProject do {:qrcode_ex, "~> 0.1.1"}, {:cachex, "~> 3.6"}, {:credo, "~> 1.6", only: [:dev, :test], runtime: false}, + {:faker, "~> 0.18.0"}, # frontend {:tailwind, "~> 0.2", runtime: Mix.env() == :dev}, diff --git a/mix.lock b/mix.lock index 17e7a7bc..9229f2c6 100644 --- a/mix.lock +++ b/mix.lock @@ -17,6 +17,7 @@ "esbuild": {:hex, :esbuild, "0.8.1", "0cbf919f0eccb136d2eeef0df49c4acf55336de864e63594adcea3814f3edf41", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "25fc876a67c13cb0a776e7b5d7974851556baeda2085296c14ab48555ea7560f"}, "eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"}, "expo": {:hex, :expo, "0.5.2", "beba786aab8e3c5431813d7a44b828e7b922bfa431d6bfbada0904535342efe2", [:mix], [], "hexpm", "8c9bfa06ca017c9cb4020fabe980bc7fdb1aaec059fd004c2ab3bff03b1c599c"}, + "faker": {:hex, :faker, "0.18.0", "943e479319a22ea4e8e39e8e076b81c02827d9302f3d32726c5bf82f430e6e14", [:mix], [], "hexpm", "bfbdd83958d78e2788e99ec9317c4816e651ad05e24cfd1196ce5db5b3e81797"}, "file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"}, "finch": {:hex, :finch, "0.18.0", "944ac7d34d0bd2ac8998f79f7a811b21d87d911e77a786bc5810adb75632ada4", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "69f5045b042e531e53edc2574f15e25e735b522c37e2ddb766e15b979e03aa65"}, "floki": {:hex, :floki, "0.36.2", "a7da0193538c93f937714a6704369711998a51a6164a222d710ebd54020aa7a3", [:mix], [], "hexpm", "a8766c0bc92f074e5cb36c4f9961982eda84c5d2b8e979ca67f5c268ec8ed580"}, diff --git a/priv/repo/seeds/activities.exs b/priv/repo/seeds/activities.exs index 560ecd6d..49d8de7e 100644 --- a/priv/repo/seeds/activities.exs +++ b/priv/repo/seeds/activities.exs @@ -2,7 +2,7 @@ defmodule Safira.Repo.Seeds.Activities do alias Safira.Repo alias Safira.Activities - alias Safira.Activities.ActivityCategory + alias Safira.Activities.{ActivityCategory, Speaker} def run do case Activities.list_activity_categories() do @@ -12,6 +12,13 @@ defmodule Safira.Repo.Seeds.Activities do Mix.shell().error("Found categories, aborting seeding categories.") end + case Activities.list_speakers() do + [] -> + seed_speakers() + _ -> + Mix.shell().error("Found speakers, aborting seeding speakers.") + end + case Activities.list_activities() do [] -> seed_activities() @@ -48,6 +55,37 @@ defmodule Safira.Repo.Seeds.Activities do end end + def seed_speakers do + for i <- 1..30 do + first_name = Faker.Person.first_name() + last_name = Faker.Person.last_name() + handle = "#{first_name}#{last_name}" |> String.downcase() + + speaker = %{ + name: "#{first_name} #{last_name}", + title: Faker.Person.title(), + company: Faker.Company.name(), + biography: Faker.Lorem.paragraph(3), + highlighted: i > 24, + socials: %{ + x: handle |> String.slice(0..14), + linkedin: handle, + github: handle, + website: Faker.Internet.url() + } + } + + changeset = Speaker.changeset(%Speaker{}, speaker) + + case Repo.insert(changeset) do + {:ok, _} -> :ok + {:error, changeset} -> + Mix.shell().error("Failed to insert speaker: #{speaker.name}") + Mix.shell().error(Kernel.inspect(changeset.errors)) + end + end + end + def seed_activities do # TODO: Add activity seeds end