diff --git a/lib/afc_web/controllers/emotion_controller.ex b/lib/afc_web/controllers/emotion_controller.ex new file mode 100644 index 0000000..2076823 --- /dev/null +++ b/lib/afc_web/controllers/emotion_controller.ex @@ -0,0 +1,42 @@ +defmodule AfcWeb.EmotionController do + use AfcWeb, :controller + alias Afc.{Angry, Happy, Repo} + + def show(conn, %{"id" => emotion}) do + {module, changeset} = get_page_module_and_changeset(emotion) + + render conn, "form.html", changeset: changeset, module: module + end + + def create(conn, params) do + submitted_emotion = + ~w(angry happy) + |> Enum.filter(&(Map.has_key?(params, &1))) + |> hd() + + form_info = Map.get(params, submitted_emotion) + + {module, changeset} = + get_page_module_and_changeset(submitted_emotion, form_info) + + case Repo.insert(changeset) do + {:ok, _captured_emotion} -> + + # The emotion itself has been captured at this point. Next step + # is to insert into the emotion_log table. + render conn, "captured.html" + + {:error, changeset} -> + render conn, "form.html", changeset: changeset, module: module + end + end + + defp get_page_module_and_changeset(page, params \\ %{}) do + case page do + "happy" -> + {Happy, Happy.changeset(%Happy{}, params)} + "angry" -> + {Angry, Angry.changeset(%Angry{}, params)} + end + end +end diff --git a/lib/afc_web/router.ex b/lib/afc_web/router.ex index 9166a1c..b53a832 100644 --- a/lib/afc_web/router.ex +++ b/lib/afc_web/router.ex @@ -17,6 +17,7 @@ defmodule AfcWeb.Router do pipe_through :browser # Use the default browser stack get "/", PageController, :index + resources "/emotion", EmotionController, only: [:show, :create] end # Other scopes may use custom stacks. diff --git a/lib/afc_web/templates/component/emoji_helper.html.eex b/lib/afc_web/templates/component/emoji_helper.html.eex index 6c40e46..bbbd294 100644 --- a/lib/afc_web/templates/component/emoji_helper.html.eex +++ b/lib/afc_web/templates/component/emoji_helper.html.eex @@ -1,6 +1,6 @@
<%= @emotion %>
- + <%= end %>10th Oct 2019 (placeholder)
-10th Oct 2019 (placeholder)
How are you feeling today?