Skip to content

Commit

Permalink
move Role related helper functions out of Ctx #86
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 25, 2020
1 parent 5ec8568 commit 8f192b1
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 131 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
env:
- MIX_ENV=test
before_script:
# create .env file on Travis-CI:
- echo "export MIX_ENV=test" > .env
- mix ecto.setup
script:
Expand Down
96 changes: 0 additions & 96 deletions lib/auth/ctx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,6 @@ defmodule Auth.Ctx do
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

alias Auth.Ctx.Permission

@doc """
Expand Down
19 changes: 0 additions & 19 deletions lib/auth/ctx/role.ex

This file was deleted.

119 changes: 119 additions & 0 deletions lib/auth/role.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
defmodule Auth.Role do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, warn: false
alias Auth.Repo
# https://stackoverflow.com/a/47501059/1148249
alias __MODULE__

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


@doc """
Returns the list of roles.
## Examples
iex> list_roles()
[%Role{}, ...]
"""
def list_roles do
Repo.all(__MODULE__)
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!(__MODULE__, 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
23 changes: 11 additions & 12 deletions lib/auth_web/controllers/role_controller.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
defmodule AuthWeb.RoleController do
use AuthWeb, :controller

alias Auth.Ctx
alias Auth.Ctx.Role
alias Auth.Role

def index(conn, _params) do
roles = Ctx.list_roles()
roles = Role.list_roles()
render(conn, "index.html", roles: roles)
end

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

def create(conn, %{"role" => role_params}) do
case Ctx.create_role(role_params) do
case Role.create_role(role_params) do
{:ok, role} ->
conn
|> put_flash(:info, "Role created successfully.")
Expand All @@ -27,20 +26,20 @@ defmodule AuthWeb.RoleController do
end

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

case Ctx.update_role(role, role_params) do
case Role.update_role(role, role_params) do
{:ok, role} ->
conn
|> put_flash(:info, "Role updated successfully.")
Expand All @@ -52,8 +51,8 @@ defmodule AuthWeb.RoleController do
end

def delete(conn, %{"id" => id}) do
role = Ctx.get_role!(id)
{:ok, _role} = Ctx.delete_role(role)
role = Role.get_role!(id)
{:ok, _role} = Role.delete_role(role)

conn
|> put_flash(:info, "Role deleted successfully.")
Expand Down
4 changes: 4 additions & 0 deletions priv/repo/create_default_roles.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# scripts for creating default roles and permissions
defmodule Setup.CreateDefaultRoles do

end
4 changes: 2 additions & 2 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ defmodule Auth.Seeds do
|> Auth.Apikey.create_apikey()

api_key = key.client_id <> "/" <> key.client_secret
# set the AUTH_API_KEY during test run:
# set the AUTH_API_KEY environment variable during test run:
write_env("AUTH_API_KEY", api_key)
end

# write the key:value pair to project .env file
def write_env(key, value) do
IO.inspect(File.cwd!)
IO.inspect(File.cwd!, label: "CWD")
path = File.cwd! <> "/.env"
IO.inspect(path, label: "path")
{:ok, data} = File.read(path)
Expand Down
4 changes: 2 additions & 2 deletions test/auth_web/controllers/role_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule AuthWeb.RoleControllerTest do
use AuthWeb.ConnCase

alias Auth.Ctx
alias Auth.Role

@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(:role) do
{:ok, role} = Ctx.create_role(@create_attrs)
{:ok, role} = Role.create_role(@create_attrs)
role
end

Expand Down

0 comments on commit 8f192b1

Please sign in to comment.