diff --git a/apps/cf_http/lib/cf_http/sessions.ex b/apps/cf_http/lib/cf_http/sessions.ex new file mode 100644 index 000000000..ae2e9ce8e --- /dev/null +++ b/apps/cf_http/lib/cf_http/sessions.ex @@ -0,0 +1,105 @@ +defmodule CfHttp.Sessions do + @moduledoc """ + The Sessions context. + """ + + import Ecto.Query, warn: false + alias CfHttp.Repo + + alias CfHttp.Sessions.Session + + @doc """ + Returns the list of sessions. + + ## Examples + + iex> list_sessions() + [%Session{}, ...] + + """ + def list_sessions do + Repo.all(Session) + end + + @doc """ + Gets a single session. + + Raises `Ecto.NoResultsError` if the Session does not exist. + + ## Examples + + iex> get_session!(123) + %Session{} + + iex> get_session!(456) + ** (Ecto.NoResultsError) + + """ + def get_session!(id), do: Repo.get!(Session, id) + + @doc """ + Creates a session. + + ## Examples + + iex> create_session(%{field: value}) + {:ok, %Session{}} + + iex> create_session(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_session(attrs \\ %{}) do + %Session{} + |> Session.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a session. + + ## Examples + + iex> update_session(session, %{field: new_value}) + {:ok, %Session{}} + + iex> update_session(session, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_session(%Session{} = session, attrs) do + session + |> Session.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a session. + + ## Examples + + iex> delete_session(session) + {:ok, %Session{}} + + iex> delete_session(session) + {:error, %Ecto.Changeset{}} + + """ + def delete_session(%Session{} = session) do + Repo.delete(session) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking session changes. + + ## Examples + + iex> change_session(session) + %Ecto.Changeset{source: %Session{}} + + """ + def change_session(%Session{} = session) do + Session.changeset(session, %{}) + end +end + diff --git a/apps/cf_http/lib/cf_http/sessions/session.ex b/apps/cf_http/lib/cf_http/sessions/session.ex new file mode 100644 index 000000000..30b1a7842 --- /dev/null +++ b/apps/cf_http/lib/cf_http/sessions/session.ex @@ -0,0 +1,21 @@ +defmodule CfHttp.Sessions.Session do + @moduledoc """ + Represents a Session + """ + + use Ecto.Schema + import Ecto.Changeset + + schema "sessions" do + field :user_id, :id + + timestamps() + end + + @doc false + def changeset(session, attrs \\ %{}) do + session + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/apps/cf_http/lib/cf_http/users.ex b/apps/cf_http/lib/cf_http/users.ex new file mode 100644 index 000000000..102f806dc --- /dev/null +++ b/apps/cf_http/lib/cf_http/users.ex @@ -0,0 +1,106 @@ +defmodule CfHttp.Users do + @moduledoc """ + The Users context. + """ + + import Ecto.Query, warn: false + alias CfHttp.Repo + + alias CfHttp.Users.User + + @doc """ + Returns the list of users. + + ## Examples + + iex> list_users() + [%User{}, ...] + + """ + def list_users do + Repo.all(User) + end + + @doc """ + Gets a single user. + + Raises `Ecto.NoResultsError` if the User does not exist. + + ## Examples + + iex> get_user!(123) + %User{} + + iex> get_user!(456) + ** (Ecto.NoResultsError) + + """ + def get_user!(id), do: Repo.get!(User, id) + + @doc """ + Creates a user. + + ## Examples + + iex> create_user(%{field: value}) + {:ok, %User{}} + + iex> create_user(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_user(attrs \\ %{}) do + %User{} + |> User.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a user. + + ## Examples + + iex> update_user(user, %{field: new_value}) + {:ok, %User{}} + + iex> update_user(user, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_user(%User{} = user, attrs) do + user + |> User.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a user. + + ## Examples + + iex> delete_user(user) + {:ok, %User{}} + + iex> delete_user(user) + {:error, %Ecto.Changeset{}} + + """ + def delete_user(%User{} = user) do + Repo.delete(user) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking user changes. + + ## Examples + + iex> change_user(user) + %Ecto.Changeset{source: %User{}} + + """ + def change_user(%User{} = user) do + User.changeset(user, %{}) + end +end + + diff --git a/apps/cf_http/lib/cf_http/user.ex b/apps/cf_http/lib/cf_http/users/user.ex similarity index 94% rename from apps/cf_http/lib/cf_http/user.ex rename to apps/cf_http/lib/cf_http/users/user.ex index 16fdb6011..5b34bc16d 100644 --- a/apps/cf_http/lib/cf_http/user.ex +++ b/apps/cf_http/lib/cf_http/users/user.ex @@ -1,4 +1,4 @@ -defmodule CfHttp.User do +defmodule CfHttp.Users.User do @moduledoc """ Represents a User I guess """ diff --git a/apps/cf_http/lib/cf_http_web/templates/session/new.html.eex b/apps/cf_http/lib/cf_http_web/templates/session/new.html.eex new file mode 100644 index 000000000..5428d0e86 --- /dev/null +++ b/apps/cf_http/lib/cf_http_web/templates/session/new.html.eex @@ -0,0 +1,23 @@ +
+
+
+ Sign In +
+ + +
+
+ + +
+ +
+
+ +
+ +
+
diff --git a/apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs b/apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs new file mode 100644 index 000000000..60cca853b --- /dev/null +++ b/apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs @@ -0,0 +1,13 @@ +defmodule CfHttp.Repo.Migrations.CreateSessions do + use Ecto.Migration + + def change do + create table(:sessions) do + add :user_id, references(:users, on_delete: :delete_all) + + timestamps() + end + + create index(:sessions, [:user_id]) + end +end