Skip to content

Commit

Permalink
add put_assoc for linking group_people to people & roles #220
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Oct 31, 2022
1 parent 63b6e82 commit 1da1ce2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/auth/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Auth.Group do
def create(attrs) do
%Group{}
|> changeset(attrs)
|> put_assoc(:app, Auth.App.get_app!(attrs.app_id))
|> Repo.insert()
end
end
4 changes: 4 additions & 0 deletions lib/auth/group_people.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ defmodule Auth.GroupPeople do
def create(attrs) do
%GroupPeople{}
|> changeset(attrs)
|> put_assoc(:granter_id, Auth.Person.get_person_by_id(attrs.granter_id))
# |> put_assoc(:group_id, Auth.Group.(attrs.grantee_id))
|> put_assoc(:person_id, Auth.Person.get_person_by_id(attrs.person_id))
|> put_assoc(:role_id, Auth.Role.get_role!(attrs.role_id))
|> Repo.insert()
end

Expand Down
14 changes: 6 additions & 8 deletions lib/auth_web/live/groups_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,23 @@ defmodule AuthWeb.GroupsLive do
dbg(desc)

# Create the Group
group = Auth.Group.create(%{
{:ok, group} = Auth.Group.create(%{
name: name,
desc: desc,
app_id: app_id,
kind: 1
})

role = Auth.Role.get_role!(2)
{:ok, person_role} =
Auth.PeopleRoles.insert(app_id, person_id, person_id, role.id)

group_person = %{
grantee_id: person_id,
group_id: group.id,
people_role_id: person_role.id
person_id: person_id,
role_id: 2
}

# Insert the GroupPerson Record
{:ok, _inserted_group_person} = Auth.GroupPeople.create(group_person)

{:ok, inserted_group_person} = Auth.GroupPeople.create(group_person)
dbg(inserted_group_person)

end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Auth.Repo.Migrations.CreatePeopleRoles do

def change do
create table(:people_roles) do
add :person_id, references(:people, on_delete: :nothing)
add :person_id, references(:people, on_delete: :delete_all)
add :role_id, references(:roles, on_delete: :nothing)
add :granter_id, references(:people, on_delete: :nothing)
# elixirforum.com/t/difference-between-utc-datetime-and-naive-datetime/12551
Expand Down
2 changes: 1 addition & 1 deletion test/auth/group_people_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ defmodule Auth.GroupPeopleTest do
assert inserted_group_person.group_id == inserted_group.id
assert inserted_group_person.person_id == grantee.id

# Insert the GroupPerson Admin
group_person_admin = %{
granter_id: admin.id,
group_id: inserted_group.id,
person_id: admin.id,
role_id: 2
}

# Insert the GroupPerson Admin
{:ok, inserted_group_admin} = Auth.GroupPeople.create(group_person_admin)
assert inserted_group_admin.group_id == inserted_group.id
assert inserted_group_admin.person_id == admin.id
Expand Down
3 changes: 2 additions & 1 deletion test/auth/group_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule Auth.GroupTest do
group = %{
desc: "My test group",
name: "TestGroup",
kind: 1
kind: 1,
app_id: 1
}
assert {:ok, inserted_group} = Auth.Group.create(group)
assert inserted_group.name == group.name
Expand Down

0 comments on commit 1da1ce2

Please sign in to comment.