Skip to content

Commit

Permalink
remove ctx from project #87
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 25, 2020
1 parent 7377027 commit ef4261d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 55 deletions.
19 changes: 0 additions & 19 deletions lib/auth/ctx/permission.ex

This file was deleted.

30 changes: 22 additions & 8 deletions lib/auth/ctx.ex → lib/auth/permission.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
defmodule Auth.Ctx do
@moduledoc """
The Ctx context.
"""

defmodule Auth.Permission do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, warn: false
alias Auth.Repo
# alias the Struct so we can use it below
alias Auth.Permission

schema "permissions" do
field :desc, :string
field :name, :string
field :person_id, :id

timestamps()
end

alias Auth.Ctx.Permission
@doc false
def changeset(permission, attrs) do
permission
|> cast(attrs, [:name, :desc])
|> validate_required([:name, :desc])
end

@doc """
Returns the list of permissions.
Expand All @@ -18,7 +31,7 @@ defmodule Auth.Ctx do
"""
def list_permissions do
Repo.all(Permission)
Repo.all(__MODULE__)
end

@doc """
Expand All @@ -35,7 +48,7 @@ defmodule Auth.Ctx do
** (Ecto.NoResultsError)
"""
def get_permission!(id), do: Repo.get!(Permission, id)
def get_permission!(id), do: Repo.get!(__MODULE__, id)

@doc """
Creates a permission.
Expand Down Expand Up @@ -101,4 +114,5 @@ defmodule Auth.Ctx do
def change_permission(%Permission{} = permission, attrs \\ %{}) do
Permission.changeset(permission, attrs)
end

end
23 changes: 11 additions & 12 deletions lib/auth_web/controllers/permission_controller.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
defmodule AuthWeb.PermissionController do
use AuthWeb, :controller

alias Auth.Ctx
alias Auth.Ctx.Permission
alias Auth.Permission

def index(conn, _params) do
permissions = Ctx.list_permissions()
permissions = Permission.list_permissions()
render(conn, "index.html", permissions: permissions)
end

def new(conn, _params) do
changeset = Ctx.change_permission(%Permission{})
changeset = Permission.change_permission(%Permission{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"permission" => permission_params}) do
case Ctx.create_permission(permission_params) do
case Permission.create_permission(permission_params) do
{:ok, permission} ->
conn
|> put_flash(:info, "Permission created successfully.")
Expand All @@ -27,20 +26,20 @@ defmodule AuthWeb.PermissionController do
end

def show(conn, %{"id" => id}) do
permission = Ctx.get_permission!(id)
permission = Permission.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)
permission = Permission.get_permission!(id)
changeset = Permission.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)
permission = Permission.get_permission!(id)

case Ctx.update_permission(permission, permission_params) do
case Permission.update_permission(permission, permission_params) do
{:ok, permission} ->
conn
|> put_flash(:info, "Permission updated successfully.")
Expand All @@ -52,8 +51,8 @@ defmodule AuthWeb.PermissionController do
end

def delete(conn, %{"id" => id}) do
permission = Ctx.get_permission!(id)
{:ok, _permission} = Ctx.delete_permission(permission)
permission = Permission.get_permission!(id)
{:ok, _permission} = Permission.delete_permission(permission)

conn
|> put_flash(:info, "Permission deleted successfully.")
Expand Down
32 changes: 18 additions & 14 deletions test/auth/role_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Auth.RoleTest do
use Auth.DataCase

alias Auth.Ctx

describe "roles" do
alias Auth.Role

Expand Down Expand Up @@ -66,7 +64,7 @@ defmodule Auth.RoleTest do
end

describe "permissions" do
alias Auth.Ctx.Permission
alias Auth.Permission

@valid_attrs %{desc: "some desc", name: "some name"}
@update_attrs %{desc: "some updated desc", name: "some updated name"}
Expand All @@ -76,53 +74,59 @@ defmodule Auth.RoleTest do
{:ok, permission} =
attrs
|> Enum.into(@valid_attrs)
|> Ctx.create_permission()
|> Permission.create_permission()

permission
end

test "list_permissions/0 returns all permissions" do
permission = permission_fixture()
assert Ctx.list_permissions() == [permission]
assert Permission.list_permissions() == [permission]
end

test "get_permission!/1 returns the permission with given id" do
permission = permission_fixture()
assert Ctx.get_permission!(permission.id) == permission
assert Permission.get_permission!(permission.id) == permission
end

test "create_permission/1 with valid data creates a permission" do
assert {:ok, %Permission{} = permission} = Ctx.create_permission(@valid_attrs)
assert {:ok, %Permission{} = permission} =
Permission.create_permission(@valid_attrs)
assert permission.desc == "some desc"
assert permission.name == "some name"
end

test "create_permission/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Ctx.create_permission(@invalid_attrs)
assert {:error, %Ecto.Changeset{}} =
Permission.create_permission(@invalid_attrs)
end

test "update_permission/2 with valid data updates the permission" do
permission = permission_fixture()
assert {:ok, %Permission{} = permission} = Ctx.update_permission(permission, @update_attrs)
assert {:ok, %Permission{} = permission} =
Permission.update_permission(permission, @update_attrs)
assert permission.desc == "some updated desc"
assert permission.name == "some updated name"
end

test "update_permission/2 with invalid data returns error changeset" do
permission = permission_fixture()
assert {:error, %Ecto.Changeset{}} = Ctx.update_permission(permission, @invalid_attrs)
assert permission == Ctx.get_permission!(permission.id)
assert {:error, %Ecto.Changeset{}} =
Permission.update_permission(permission, @invalid_attrs)
assert permission == Permission.get_permission!(permission.id)
end

test "delete_permission/1 deletes the permission" do
permission = permission_fixture()
assert {:ok, %Permission{}} = Ctx.delete_permission(permission)
assert_raise Ecto.NoResultsError, fn -> Ctx.get_permission!(permission.id) end
assert {:ok, %Permission{}} = Permission.delete_permission(permission)
assert_raise Ecto.NoResultsError, fn ->
Permission.get_permission!(permission.id)
end
end

test "change_permission/1 returns a permission changeset" do
permission = permission_fixture()
assert %Ecto.Changeset{} = Ctx.change_permission(permission)
assert %Ecto.Changeset{} = Permission.change_permission(permission)
end
end
end
4 changes: 2 additions & 2 deletions test/auth_web/controllers/permission_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule AuthWeb.PermissionControllerTest do
use AuthWeb.ConnCase

alias Auth.Ctx
alias Auth.Permission

@create_attrs %{desc: "some desc", name: "some name"}
@update_attrs %{desc: "some updated desc", name: "some updated name"}
@invalid_attrs %{desc: nil, name: nil}

def fixture(:permission) do
{:ok, permission} = Ctx.create_permission(@create_attrs)
{:ok, permission} = Permission.create_permission(@create_attrs)
permission
end

Expand Down

0 comments on commit ef4261d

Please sign in to comment.