-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from actnforchildren/log-emotion
Log emotion
- Loading branch information
Showing
18 changed files
with
154 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import EctoEnum | ||
|
||
defenum Afc.EctoEnum.EmotionEnum, :emotion, [:angry, :happy] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule AfcWeb.SessionView do | ||
use AfcWeb, :view | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
priv/repo/migrations/20181015111921_create_emotion_logs.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20181016092544_add_emotion_id_to_emotion_log.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |