-
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
420 additions
and
1 deletion.
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
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.Permission do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "permissions" do | ||
field :desc, :string | ||
field :name, :string | ||
field :person_id, :id | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(permission, attrs) do | ||
permission | ||
|> 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.PermissionController do | ||
use AuthWeb, :controller | ||
|
||
alias Auth.Ctx | ||
alias Auth.Ctx.Permission | ||
|
||
def index(conn, _params) do | ||
permissions = Ctx.list_permissions() | ||
render(conn, "index.html", permissions: permissions) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Ctx.change_permission(%Permission{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"permission" => permission_params}) do | ||
case Ctx.create_permission(permission_params) do | ||
{:ok, permission} -> | ||
conn | ||
|> put_flash(:info, "Permission created successfully.") | ||
|> redirect(to: Routes.permission_path(conn, :show, permission)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
permission = Ctx.get_permission!(id) | ||
render(conn, "show.html", permission: permission) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
permission = Ctx.get_permission!(id) | ||
changeset = Ctx.change_permission(permission) | ||
render(conn, "edit.html", permission: permission, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "permission" => permission_params}) do | ||
permission = Ctx.get_permission!(id) | ||
|
||
case Ctx.update_permission(permission, permission_params) do | ||
{:ok, permission} -> | ||
conn | ||
|> put_flash(:info, "Permission updated successfully.") | ||
|> redirect(to: Routes.permission_path(conn, :show, permission)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "edit.html", permission: permission, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
permission = Ctx.get_permission!(id) | ||
{:ok, _permission} = Ctx.delete_permission(permission) | ||
|
||
conn | ||
|> put_flash(:info, "Permission deleted successfully.") | ||
|> redirect(to: Routes.permission_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 Permission</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.permission_path(@conn, :update, @permission)) %> | ||
|
||
<span><%= link "Back", to: Routes.permission_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 Permissions</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Desc</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for permission <- @permissions do %> | ||
<tr> | ||
<td><%= permission.name %></td> | ||
<td><%= permission.desc %></td> | ||
|
||
<td> | ||
<span><%= link "Show", to: Routes.permission_path(@conn, :show, permission) %></span> | ||
<span><%= link "Edit", to: Routes.permission_path(@conn, :edit, permission) %></span> | ||
<span><%= link "Delete", to: Routes.permission_path(@conn, :delete, permission), method: :delete, data: [confirm: "Are you sure?"] %></span> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<span><%= link "New Permission", to: Routes.permission_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 Permission</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.permission_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.permission_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 Permission</h1> | ||
|
||
<ul> | ||
|
||
<li> | ||
<strong>Name:</strong> | ||
<%= @permission.name %> | ||
</li> | ||
|
||
<li> | ||
<strong>Desc:</strong> | ||
<%= @permission.desc %> | ||
</li> | ||
|
||
</ul> | ||
|
||
<span><%= link "Edit", to: Routes.permission_path(@conn, :edit, @permission) %></span> | ||
<span><%= link "Back", to: Routes.permission_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.PermissionView do | ||
use AuthWeb, :view | ||
end |
15 changes: 15 additions & 0 deletions
15
priv/repo/migrations/20200722180019_create_permissions.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,15 @@ | ||
defmodule Auth.Repo.Migrations.CreatePermissions do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:permissions) do | ||
add :name, :string | ||
add :desc, :string | ||
add :person_id, references(:people, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:permissions, [: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
Oops, something went wrong.