Skip to content

Commit

Permalink
allows a user to record their emotions #29 #22
Browse files Browse the repository at this point in the history
  • Loading branch information
RobStallion committed Oct 16, 2018
1 parent 49f339c commit ce14c70
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/afc/emotion_log.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
defmodule Afc.EmotionLog do
use Ecto.Schema
import Ecto.Changeset
alias Afc.EctoEnum.EmotionEnum
alias Afc.{EctoEnum.EmotionEnum, User}

schema "emotion_logs" do
field :emotion, EmotionEnum
field :user_id, :id
field :emotion_id, :id
belongs_to :user, User

timestamps()
end

@doc false
def changeset(emotion_log, attrs) do
emotion_log
|> cast(attrs, [:emotion])
|> validate_required([:emotion])
|> cast(attrs, [:emotion, :emotion_id])
|> validate_required([:emotion, :emotion_id])
end
end
20 changes: 15 additions & 5 deletions lib/afc_web/controllers/emotion_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
defmodule AfcWeb.EmotionController do
use AfcWeb, :controller
alias Afc.{Angry, Happy, Repo}
alias Afc.{Angry, EmotionLog, Happy, Repo}
alias Ecto.Changeset

def show(conn, %{"id" => "captured"}) do
render conn, "captured.html"
end

def show(conn, %{"id" => emotion}) do
{module, changeset} = get_page_module_and_changeset(emotion)
Expand All @@ -20,11 +25,16 @@ defmodule AfcWeb.EmotionController do
get_page_module_and_changeset(submitted_emotion, form_info)

case Repo.insert(changeset) do
{:ok, _captured_emotion} ->
{:ok, captured_emotion} ->
emotion_params =
%{emotion: submitted_emotion, emotion_id: captured_emotion.id}

%EmotionLog{}
|> EmotionLog.changeset(emotion_params)
|> Changeset.put_assoc(:user, conn.assigns.current_user)
|> Repo.insert!()

# The emotion itself has been captured at this point. Next step
# is to insert into the emotion_log table.
render conn, "captured.html"
redirect(conn, to: emotion_path(conn, :show, "captured"))

{:error, changeset} ->
render conn, "form.html", changeset: changeset, module: module
Expand Down
2 changes: 1 addition & 1 deletion lib/afc_web/templates/component/emoji_helper.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<%= link to: emotion_path(@conn, :show, @page), class: "pointer link black" do %>
<%= emoji_p_tag(@emoji) %>
<p class="-mt2"><%= @emotion %></p>
<%= end %>
<% end %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Afc.Repo.Migrations.AddEmotionIdToEmotionLog do
use Ecto.Migration

def change do
alter table(:emotion_logs) do
add :emotion_id, :integer
end
end
end

0 comments on commit ce14c70

Please sign in to comment.