-
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.
move Role related helper functions out of Ctx #86
- Loading branch information
Showing
8 changed files
with
139 additions
and
131 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
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 was deleted.
Oops, something went wrong.
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,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 |
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,4 @@ | ||
# scripts for creating default roles and permissions | ||
defmodule Setup.CreateDefaultRoles do | ||
|
||
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