From 77a9ef432c0568caeff54916a464e9c467ad046e Mon Sep 17 00:00:00 2001 From: Robert Francis Date: Mon, 15 Oct 2018 13:59:48 +0100 Subject: [PATCH] creates emotion_log table with enum type for emotion #22 --- lib/afc/ecto_enum/ecto_enums.ex | 3 +++ lib/afc/emotion_log.ex | 19 +++++++++++++++++++ mix.exs | 3 ++- mix.lock | 1 + .../20181015111921_create_emotion_logs.exs | 14 ++++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/afc/ecto_enum/ecto_enums.ex create mode 100644 lib/afc/emotion_log.ex create mode 100644 priv/repo/migrations/20181015111921_create_emotion_logs.exs diff --git a/lib/afc/ecto_enum/ecto_enums.ex b/lib/afc/ecto_enum/ecto_enums.ex new file mode 100644 index 0000000..acca2dd --- /dev/null +++ b/lib/afc/ecto_enum/ecto_enums.ex @@ -0,0 +1,3 @@ +import EctoEnum + +defenum Afc.EctoEnum.EmotionEnum, :emotion, [:angry, :happy] diff --git a/lib/afc/emotion_log.ex b/lib/afc/emotion_log.ex new file mode 100644 index 0000000..4877f67 --- /dev/null +++ b/lib/afc/emotion_log.ex @@ -0,0 +1,19 @@ +defmodule Afc.EmotionLog do + use Ecto.Schema + import Ecto.Changeset + alias Afc.EctoEnum.EmotionEnum + + schema "emotion_logs" do + field :emotion, EmotionEnum + field :user_id, :id + + timestamps() + end + + @doc false + def changeset(emotion_log, attrs) do + emotion_log + |> cast(attrs, [:emotion]) + |> validate_required([:emotion]) + end +end diff --git a/mix.exs b/mix.exs index 73b913e..66bb39b 100644 --- a/mix.exs +++ b/mix.exs @@ -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, github: "dwyl/autoform"} + {:autoform, github: "dwyl/autoform"}, + {:ecto_enum, "~> 1.0"} ] end diff --git a/mix.lock b/mix.lock index 0dc376b..066a0c9 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, diff --git a/priv/repo/migrations/20181015111921_create_emotion_logs.exs b/priv/repo/migrations/20181015111921_create_emotion_logs.exs new file mode 100644 index 0000000..d580914 --- /dev/null +++ b/priv/repo/migrations/20181015111921_create_emotion_logs.exs @@ -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