   end
 
   def create(conn, %{"series" => series_params}) do
-    params = series_params |> populate |> to_map
-    changeset = Series.changeset(%Series{}, params)
+    params = populate(series_params)
+    episodes = Episode.new(params.episode_count)
+    changeset =
+      %Series{}
+      |> Series.changeset(to_map(params))
+      |> Ecto.Changeset.put_assoc(:episodes, episodes)
 
     case Repo.insert(changeset) do
       {:ok, series} ->
+        series =
+          series
+          |> Repo.preload(:episodes)
+          |> Repo.preload(:franchise)
+
         conn
         |> put_status(:created)
         |> put_resp_header("location", series_path(conn, :show, series))

  end

  def show(conn, %{"id" => id}) do
    series = Series |> Repo.get!(id) |> Repo.preload(:episodes)
    render(conn, "show.json", series: series)
  end

  def update(conn, %{"id" => id, "series" => series_params}) do
    series = Series |> Repo.get!(id) |> Repo.preload(:episodes)
    params = series_params |> populate |> to_map
    changeset = Series.changeset(series, params)

    case Repo.update(changeset) do
      {:ok, series} ->
        render(conn, "show.json", series: series)
      {:error, changeset} ->
        conn

  import Animu.ModelHelper
  import Animu.SeriesPopulator

  alias Animu.{Series, Episode}

  plug Guardian.Plug.EnsureAuthenticated, handler: Animu.SessionController

