diff --git a/lib/radiator/podcast.ex b/lib/radiator/podcast.ex index d9b779af..f4c60b19 100644 --- a/lib/radiator/podcast.ex +++ b/lib/radiator/podcast.ex @@ -297,31 +297,19 @@ defmodule Radiator.Podcast do """ def create_episode(attrs \\ %{}) do - attrs_with_number = set_number(attrs) - %Episode{} - |> Episode.changeset(attrs_with_number) + |> Episode.changeset(attrs) |> Repo.insert() end - defp set_number(%{number: _number} = attrs), do: attrs - - defp set_number(%{show_id: show_id} = episode_attrs) do - number = get_highest_number(show_id) + 1 - Map.put(episode_attrs, :number, number) - end - - defp set_number(%{} = episode_attrs) do - Map.put(episode_attrs, :number, 0) - end - - defp get_highest_number(show_id) do + def get_next_episode_number(show_id) do query = from e in Episode, select: max(e.number), where: [show_id: ^show_id] - Repo.one(query) || 0 + max_number = Repo.one(query) || 0 + max_number + 1 end @doc """ diff --git a/lib/radiator_web/live/episode_live/index.ex b/lib/radiator_web/live/episode_live/index.ex index 6402c35b..cf8dda68 100644 --- a/lib/radiator_web/live/episode_live/index.ex +++ b/lib/radiator_web/live/episode_live/index.ex @@ -4,6 +4,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do alias Radiator.Outline.{Dispatch, NodeRepository} alias Radiator.Outline.Event.{NodeContentChangedEvent, NodeDeletedEvent, NodeInsertedEvent} alias Radiator.Podcast + alias Radiator.Podcast.Episode @impl true def mount(%{"show" => show_id}, _session, socket) do @@ -14,6 +15,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do # |> assign(:page_description, "") |> assign(:show, show) |> assign(:episodes, show.episodes) + |> assign(action: nil, episode: nil, form: nil) |> reply(:ok) end @@ -75,6 +77,54 @@ defmodule RadiatorWeb.EpisodeLive.Index do |> reply(:noreply) end + def handle_event("new_episode", _params, socket) do + show = socket.assigns.show + number = Podcast.get_next_episode_number(show.id) + + episode = %Podcast.Episode{} + changeset = Episode.changeset(episode, %{number: number}) + + socket + |> assign(:action, :new_episode) + |> assign(:episode, episode) + |> assign(:form, to_form(changeset)) + |> reply(:noreply) + end + + @impl true + def handle_event("validate", %{"episode" => params}, socket) do + changeset = socket.assigns.episode |> Episode.changeset(params) |> Map.put(:action, :validate) + + socket + |> assign(:form, to_form(changeset)) + |> reply(:noreply) + end + + @impl true + def handle_event("save", %{"episode" => params}, socket) do + show_id = socket.assigns.show.id + + params = Map.put(params, "show_id", show_id) + + case Podcast.create_episode(params) do + {:ok, episode} -> + show = Podcast.get_show!(show_id, preload: :episodes) + + socket + |> assign(:action, nil) + |> assign(:episodes, show.episodes) + |> put_flash(:info, "Episode created successfully") + |> push_patch(to: ~p"/admin/podcast/#{show}/#{episode}") + |> reply(:noreply) + + {:error, %Ecto.Changeset{} = changeset} -> + socket + |> assign(:form, to_form(changeset)) + |> put_flash(:info, "Episode could not be created") + |> reply(:noreply) + end + end + @impl true def handle_info( %{node: node, event_id: <<_::binary-size(36)>> <> ":" <> id}, diff --git a/lib/radiator_web/live/episode_live/index.html.heex b/lib/radiator_web/live/episode_live/index.html.heex index 295ae610..0dd0099f 100644 --- a/lib/radiator_web/live/episode_live/index.html.heex +++ b/lib/radiator_web/live/episode_live/index.html.heex @@ -1,8 +1,13 @@