mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
context: users_roles
This commit is contained in:
parent
0906248922
commit
37ac0e03b7
3 changed files with 171 additions and 0 deletions
|
@ -677,4 +677,100 @@ defmodule Philomena.Users do
|
|||
def change_whitelist(%Whitelist{} = whitelist) do
|
||||
Whitelist.changeset(whitelist, %{})
|
||||
end
|
||||
|
||||
alias Philomena.Users.Role
|
||||
|
||||
@doc """
|
||||
Returns the list of users_roles.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_users_roles()
|
||||
[%Role{}, ...]
|
||||
|
||||
"""
|
||||
def list_users_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{source: %Role{}}
|
||||
|
||||
"""
|
||||
def change_role(%Role{} = role) do
|
||||
Role.changeset(role, %{})
|
||||
end
|
||||
end
|
||||
|
|
18
lib/philomena/users/role.ex
Normal file
18
lib/philomena/users/role.ex
Normal file
|
@ -0,0 +1,18 @@
|
|||
defmodule Philomena.Users.Role do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key false
|
||||
|
||||
schema "users_roles" do
|
||||
belongs_to :user, Philomena.Users.User, primary_key: true
|
||||
belongs_to :role, Philomena.Roles.Role, primary_key: true
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(role, attrs) do
|
||||
role
|
||||
|> cast(attrs, [])
|
||||
|> validate_required([])
|
||||
end
|
||||
end
|
|
@ -344,4 +344,61 @@ defmodule Philomena.UsersTest do
|
|||
assert %Ecto.Changeset{} = Users.change_whitelist(whitelist)
|
||||
end
|
||||
end
|
||||
|
||||
describe "users_roles" do
|
||||
alias Philomena.Users.Role
|
||||
|
||||
@valid_attrs %{}
|
||||
@update_attrs %{}
|
||||
@invalid_attrs %{}
|
||||
|
||||
def role_fixture(attrs \\ %{}) do
|
||||
{:ok, role} =
|
||||
attrs
|
||||
|> Enum.into(@valid_attrs)
|
||||
|> Users.create_role()
|
||||
|
||||
role
|
||||
end
|
||||
|
||||
test "list_users_roles/0 returns all users_roles" do
|
||||
role = role_fixture()
|
||||
assert Users.list_users_roles() == [role]
|
||||
end
|
||||
|
||||
test "get_role!/1 returns the role with given id" do
|
||||
role = role_fixture()
|
||||
assert Users.get_role!(role.id) == role
|
||||
end
|
||||
|
||||
test "create_role/1 with valid data creates a role" do
|
||||
assert {:ok, %Role{} = role} = Users.create_role(@valid_attrs)
|
||||
end
|
||||
|
||||
test "create_role/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Users.create_role(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_role/2 with valid data updates the role" do
|
||||
role = role_fixture()
|
||||
assert {:ok, %Role{} = role} = Users.update_role(role, @update_attrs)
|
||||
end
|
||||
|
||||
test "update_role/2 with invalid data returns error changeset" do
|
||||
role = role_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Users.update_role(role, @invalid_attrs)
|
||||
assert role == Users.get_role!(role.id)
|
||||
end
|
||||
|
||||
test "delete_role/1 deletes the role" do
|
||||
role = role_fixture()
|
||||
assert {:ok, %Role{}} = Users.delete_role(role)
|
||||
assert_raise Ecto.NoResultsError, fn -> Users.get_role!(role.id) end
|
||||
end
|
||||
|
||||
test "change_role/1 returns a role changeset" do
|
||||
role = role_fixture()
|
||||
assert %Ecto.Changeset{} = Users.change_role(role)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue