Skip to content

Commit

Permalink
Merge pull request #31 from actnforchildren/log-emotion
Browse files Browse the repository at this point in the history
Log emotion
  • Loading branch information
Danwhy authored Oct 16, 2018
2 parents 880a98e + 4270bc4 commit 349c8e8
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lib/afc/ecto_enum/ecto_enums.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import EctoEnum

defenum Afc.EctoEnum.EmotionEnum, :emotion, [:angry, :happy]
20 changes: 20 additions & 0 deletions lib/afc/emotion_log.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Afc.EmotionLog do
use Ecto.Schema
import Ecto.Changeset
alias Afc.{EctoEnum.EmotionEnum, User}

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

timestamps()
end

@doc false
def changeset(emotion_log, attrs) do
emotion_log
|> cast(attrs, [:emotion, :emotion_id])
|> validate_required([:emotion, :emotion_id])
end
end
2 changes: 1 addition & 1 deletion lib/afc/trusted_adult.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Afc.Trusted_adult do
defmodule Afc.TrustedAdult do
use Ecto.Schema
import Ecto.Changeset

Expand Down
34 changes: 34 additions & 0 deletions lib/afc_web/controllers/auth.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule AfcWeb.Auth do
import Plug.Conn
import Phoenix.Controller
alias Afc.{Repo, User}
alias AfcWeb.Router.Helpers

def init(opts), do: opts

def call(conn, _opts) do
user_id = get_session(conn, :user_id)
user = user_id && Repo.get(User, user_id)
assign(conn, :current_user, user)
end

def login(conn, user) do
conn
|> assign(:current_user, user)
|> put_session(:user_id, user.id)
|> configure_session(renew: true)
end

def logout(conn), do: configure_session(conn, drop: true)

def authenticate_user(conn, _opts) do
if conn.assigns.current_user do
conn
else
conn
|> put_flash(:error, "You must be logged in to access that page")
|> redirect(to: Helpers.session_path(conn, :new))
|> halt()
end
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
27 changes: 27 additions & 0 deletions lib/afc_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule AfcWeb.SessionController do
use AfcWeb, :controller
alias AfcWeb.Auth
alias Afc.{Repo, User}

def new(conn, _params) do
if conn.assigns.current_user do
redirect(conn, to: page_path(conn, :index))
else
render(conn, "new.html")
end
end

def create(conn, _params) do
user = Repo.get_by!(User, username: "test_user")

conn
|> Auth.login(user)
|> redirect(to: page_path(conn, :index))
end

def delete(conn, _params) do
conn
|> Auth.logout()
|> redirect(to: session_path(conn, :new))
end
end
13 changes: 8 additions & 5 deletions lib/afc_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
defmodule AfcWeb.Router do
use AfcWeb, :router
import AfcWeb.Auth

pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug AfcWeb.Auth
end

pipeline :api do
Expand All @@ -16,12 +18,13 @@ defmodule AfcWeb.Router do
scope "/", AfcWeb do
pipe_through :browser # Use the default browser stack

resources "/sessions", SessionController, only: [:new, :create, :delete]
end

scope "/", AfcWeb do
pipe_through [:browser, :authenticate_user]

get "/", PageController, :index
resources "/emotion", EmotionController, only: [:show, :create]
end

# Other scopes may use custom stacks.
# scope "/api", AfcWeb do
# pipe_through :api
# end
end
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>
4 changes: 1 addition & 3 deletions lib/afc_web/templates/component/navbar.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@
<p class="db mv1">Help</p>
</a>

<a class="dib w-25 pointer link pt2 black" href="/">
<p class="db mv1">Logout</p>
</a>
<%= link "Log out", to: session_path(@conn, :delete, @current_user), method: "delete", class: "dib w-25 pointer link pt2 black" %>
</div>
5 changes: 3 additions & 2 deletions lib/afc_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
</head>

<body class="pv3 afc-bg-pink">
<%= component "navbar", [] %>
<%= if @current_user do %>
<%= component "navbar", [conn: @conn, current_user: @current_user] %>
<% end %>
<div>
<main role="main">
<%= render @view_module, @view_template, assigns %>
</main>
</div>
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
</body>

</html>
3 changes: 3 additions & 0 deletions lib/afc_web/templates/session/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= form_for @conn, session_path(@conn, :create), [as: :session], fn f -> %>
<%= submit "login" %>
<% end %>
1 change: 0 additions & 1 deletion lib/afc_web/views/emotion_view.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule AfcWeb.EmotionView do
use AfcWeb, :view
use Autoform
alias Afc.{Angry, Happy}
end
3 changes: 3 additions & 0 deletions lib/afc_web/views/session_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AfcWeb.SessionView do
use AfcWeb, :view
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule Afc.Mixfile do
{:cowboy, "~> 1.0"},
{:excoveralls, "~> 0.10", only: :test},
{:credo, "~> 0.10.0", only: [:dev, :test], runtime: false},
{:autoform, git: "https:/dwyl/autoform.git", tag: "0.1"}
{:autoform, git: "https:/dwyl/autoform.git", tag: "0.1"},
{:ecto_enum, "~> 1.0"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "2.2.11", "4bb8f11718b72ba97a2696f65d247a379e739a0ecabf6a13ad1face79844791c", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_enum": {:hex, :ecto_enum, "1.1.0", "d44fe2ce6e1c0e907e7c3b6456a69e0f1d662348d8b4e2a662ba312223d8ff62", [:mix], [{:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.10.1", "407d50ac8fc63dfee9175ccb4548e6c5512b5052afa63eedb9cd452a32a91495", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"file_system": {:hex, :file_system, "0.2.6", "fd4dc3af89b9ab1dc8ccbcc214a0e60c41f34be251d9307920748a14bf41f1d3", [:mix], [], "hexpm"},
"gettext": {:hex, :gettext, "0.16.0", "4a7e90408cef5f1bf57c5a39e2db8c372a906031cc9b1466e963101cb927dafc", [:mix], [], "hexpm"},
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/create_dummy_user.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
alias Afc.{Repo, TrustedAdult, User}

adult = Repo.insert!(%TrustedAdult{email: "[email protected]"})

Repo.insert!(%User{
username: "test_user",
pin: 1234,
trusted_adult_id: adult.id
})
14 changes: 14 additions & 0 deletions priv/repo/migrations/20181015111921_create_emotion_logs.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Afc.Repo.Migrations.CreateEmotionLogs do
use Ecto.Migration

def change do
create table(:emotion_logs) do
add :emotion, :string
add :user_id, references(:users, on_delete: :nothing)

timestamps()
end

create index(:emotion_logs, [:user_id])
end
end
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 349c8e8

Please sign in to comment.