Skip to content

Commit

Permalink
feat: speaker seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo committed Nov 4, 2024
1 parent a5e5417 commit 6bce59d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions lib/safira_web/live/backoffice/schedule_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
40 changes: 39 additions & 1 deletion priv/repo/seeds/activities.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6bce59d

Please sign in to comment.