-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
434 additions
and
0 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,104 @@ | ||
defmodule Auth.Ctx do | ||
@moduledoc """ | ||
The Ctx context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Auth.Repo | ||
|
||
alias Auth.Ctx.Role | ||
|
||
@doc """ | ||
Returns the list of roles. | ||
## Examples | ||
iex> list_roles() | ||
[%Role{}, ...] | ||
""" | ||
def list_roles do | ||
Repo.all(Role) | ||
end | ||
|
||
@doc """ | ||
Gets a single role. | ||
Raises `Ecto.NoResultsError` if the Role does not exist. | ||
## Examples | ||
iex> get_role!(123) | ||
%Role{} | ||
iex> get_role!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_role!(id), do: Repo.get!(Role, id) | ||
|
||
@doc """ | ||
Creates a role. | ||
## Examples | ||
iex> create_role(%{field: value}) | ||
{:ok, %Role{}} | ||
iex> create_role(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_role(attrs \\ %{}) do | ||
%Role{} | ||
|> Role.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a role. | ||
## Examples | ||
iex> update_role(role, %{field: new_value}) | ||
{:ok, %Role{}} | ||
iex> update_role(role, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_role(%Role{} = role, attrs) do | ||
role | ||
|> Role.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a role. | ||
## Examples | ||
iex> delete_role(role) | ||
{:ok, %Role{}} | ||
iex> delete_role(role) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_role(%Role{} = role) do | ||
Repo.delete(role) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking role changes. | ||
## Examples | ||
iex> change_role(role) | ||
%Ecto.Changeset{data: %Role{}} | ||
""" | ||
def change_role(%Role{} = role, attrs \\ %{}) do | ||
Role.changeset(role, attrs) | ||
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Auth.Ctx.Role do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "roles" do | ||
field :desc, :string | ||
field :name, :string | ||
field :person_id, :id | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(role, attrs) do | ||
role | ||
|> cast(attrs, [:name, :desc]) | ||
|> validate_required([:name, :desc]) | ||
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule AuthWeb.RoleController do | ||
use AuthWeb, :controller | ||
|
||
alias Auth.Ctx | ||
alias Auth.Ctx.Role | ||
|
||
def index(conn, _params) do | ||
roles = Ctx.list_roles() | ||
render(conn, "index.html", roles: roles) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Ctx.change_role(%Role{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"role" => role_params}) do | ||
case Ctx.create_role(role_params) do | ||
{:ok, role} -> | ||
conn | ||
|> put_flash(:info, "Role created successfully.") | ||
|> redirect(to: Routes.role_path(conn, :show, role)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
role = Ctx.get_role!(id) | ||
render(conn, "show.html", role: role) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
role = Ctx.get_role!(id) | ||
changeset = Ctx.change_role(role) | ||
render(conn, "edit.html", role: role, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "role" => role_params}) do | ||
role = Ctx.get_role!(id) | ||
|
||
case Ctx.update_role(role, role_params) do | ||
{:ok, role} -> | ||
conn | ||
|> put_flash(:info, "Role updated successfully.") | ||
|> redirect(to: Routes.role_path(conn, :show, role)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "edit.html", role: role, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
role = Ctx.get_role!(id) | ||
{:ok, _role} = Ctx.delete_role(role) | ||
|
||
conn | ||
|> put_flash(:info, "Role deleted successfully.") | ||
|> redirect(to: Routes.role_path(conn, :index)) | ||
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,5 @@ | ||
<h1>Edit Role</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.role_path(@conn, :update, @role)) %> | ||
|
||
<span><%= link "Back", to: Routes.role_path(@conn, :index) %></span> |
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,19 @@ | ||
<%= form_for @changeset, @action, fn f -> %> | ||
<%= if @changeset.action do %> | ||
<div class="alert alert-danger"> | ||
<p>Oops, something went wrong! Please check the errors below.</p> | ||
</div> | ||
<% end %> | ||
|
||
<%= label f, :name %> | ||
<%= text_input f, :name %> | ||
<%= error_tag f, :name %> | ||
|
||
<%= label f, :desc %> | ||
<%= text_input f, :desc %> | ||
<%= error_tag f, :desc %> | ||
|
||
<div> | ||
<%= submit "Save" %> | ||
</div> | ||
<% 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,28 @@ | ||
<h1>Listing Roles</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Desc</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for role <- @roles do %> | ||
<tr> | ||
<td><%= role.name %></td> | ||
<td><%= role.desc %></td> | ||
|
||
<td> | ||
<span><%= link "Show", to: Routes.role_path(@conn, :show, role) %></span> | ||
<span><%= link "Edit", to: Routes.role_path(@conn, :edit, role) %></span> | ||
<span><%= link "Delete", to: Routes.role_path(@conn, :delete, role), method: :delete, data: [confirm: "Are you sure?"] %></span> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<span><%= link "New Role", to: Routes.role_path(@conn, :new) %></span> |
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,5 @@ | ||
<h1>New Role</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.role_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.role_path(@conn, :index) %></span> |
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,18 @@ | ||
<h1>Show Role</h1> | ||
|
||
<ul> | ||
|
||
<li> | ||
<strong>Name:</strong> | ||
<%= @role.name %> | ||
</li> | ||
|
||
<li> | ||
<strong>Desc:</strong> | ||
<%= @role.desc %> | ||
</li> | ||
|
||
</ul> | ||
|
||
<span><%= link "Edit", to: Routes.role_path(@conn, :edit, @role) %></span> | ||
<span><%= link "Back", to: Routes.role_path(@conn, :index) %></span> |
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 AuthWeb.RoleView do | ||
use AuthWeb, :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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Auth.Repo.Migrations.CreateRoles do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:roles) do | ||
add :name, :string | ||
add :desc, :string | ||
add :person_id, references(:people, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:roles, [:person_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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
defmodule Auth.CtxTest do | ||
use Auth.DataCase | ||
|
||
alias Auth.Ctx | ||
|
||
describe "roles" do | ||
alias Auth.Ctx.Role | ||
|
||
@valid_attrs %{desc: "some desc", name: "some name"} | ||
@update_attrs %{desc: "some updated desc", name: "some updated name"} | ||
@invalid_attrs %{desc: nil, name: nil} | ||
|
||
def role_fixture(attrs \\ %{}) do | ||
{:ok, role} = | ||
attrs | ||
|> Enum.into(@valid_attrs) | ||
|> Ctx.create_role() | ||
|
||
role | ||
end | ||
|
||
test "list_roles/0 returns all roles" do | ||
role = role_fixture() | ||
assert Ctx.list_roles() == [role] | ||
end | ||
|
||
test "get_role!/1 returns the role with given id" do | ||
role = role_fixture() | ||
assert Ctx.get_role!(role.id) == role | ||
end | ||
|
||
test "create_role/1 with valid data creates a role" do | ||
assert {:ok, %Role{} = role} = Ctx.create_role(@valid_attrs) | ||
assert role.desc == "some desc" | ||
assert role.name == "some name" | ||
end | ||
|
||
test "create_role/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Ctx.create_role(@invalid_attrs) | ||
end | ||
|
||
test "update_role/2 with valid data updates the role" do | ||
role = role_fixture() | ||
assert {:ok, %Role{} = role} = Ctx.update_role(role, @update_attrs) | ||
assert role.desc == "some updated desc" | ||
assert role.name == "some updated name" | ||
end | ||
|
||
test "update_role/2 with invalid data returns error changeset" do | ||
role = role_fixture() | ||
assert {:error, %Ecto.Changeset{}} = Ctx.update_role(role, @invalid_attrs) | ||
assert role == Ctx.get_role!(role.id) | ||
end | ||
|
||
test "delete_role/1 deletes the role" do | ||
role = role_fixture() | ||
assert {:ok, %Role{}} = Ctx.delete_role(role) | ||
assert_raise Ecto.NoResultsError, fn -> Ctx.get_role!(role.id) end | ||
end | ||
|
||
test "change_role/1 returns a role changeset" do | ||
role = role_fixture() | ||
assert %Ecto.Changeset{} = Ctx.change_role(role) | ||
end | ||
end | ||
end |
Oops, something went wrong.