Skip to content

Commit

Permalink
create groups schema for #220
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Oct 17, 2022
1 parent 997cc36 commit 7cf2ead
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 19 deletions.
64 changes: 56 additions & 8 deletions BUILDIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ to invite others
to ***collaborate***.

Our reasoning to include **`groups`**
in the **`auth`** App is that
we _already_ store all **`people`** related
in the **`auth`** App
is that it _already_ stores all **`people`** related
(_personally identifiable_) data
in **`auth`** therefore _grouping_
in therefore _grouping_
those **`people`** together makes logical sense.
Therefore this is a **_generalised_ implementation**
This is a **_generalised_ implementation**
that can be used by **_any_ application**
that requires collaboration/teamwork.

## 10.1 Create Schema

First we need to create a new schema for storing the data.

Run the folloiwng
[**`mix phx.gen.schema`**](https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Schema.html)
command to create the `groups` schema
as outlined in
[**`#220`**](https://github.com/dwyl/auth/issues/220)

```sh
mix phx.gen.schema Group groups name:binary desc:binary kind:string
mix phx.gen.schema Group groups name:binary desc:binary kind:integer
```

Both `group.name` and `group.desc` (description)
Expand All @@ -64,7 +66,46 @@ hence using the `binary` type in the `gen.schema` command.
The data for these fields will be encrypted at rest using
[`Fields.Encrypted`](https://github.com/dwyl/fields).

## 10.2 Create `LiveView` for `groups`
The `group.kind` will be the way people _categorise_
their various groups. It will be an `Enum`
and therefore the `integer` will be stored in the DB.


## 10.2 _Test_ Groups Schema

Having created the Groups Schema + Migration in the previous step,
it created a new file:
`lib/auth/group.ex`

If we run the coverage report with the command: `mix c`

We see that there are no tests for the code in the `group.ex` file:

```sh

Randomized with seed 366521
----------------
COV FILE LINES RELEVANT MISSED
100.0% lib/auth.ex 9 0 0
100.0% lib/auth/apikey.ex 105 15 0
100.0% lib/auth/app.ex 158 17 0
100.0% lib/auth/email.ex 41 7 0
0.0% lib/auth/group.ex 19 2 2 <-- This!
100.0% lib/auth/init/init.ex 124 26 0
...
100.0% lib/auth_web/views/layout_view.ex 3 0 0
100.0% lib/auth_web/views/people_view.ex 35 7 0
100.0% lib/auth_web/views/permission_view.ex 3 0 0
100.0% lib/auth_web/views/role_view.ex 10 3 0
[TOTAL] 99.3%
----------------
```

That's what we are fixing now.



## 10.3 Create `LiveView` for `groups`

Create the `lib/auth_web/live` directory
and the controller at `lib/auth_web/live/groups_live.ex`:
Expand Down Expand Up @@ -113,7 +154,7 @@ update the contents of the `<body>` to:
</body>
```

## 1.5 Update `router.ex`
## 10.4 Update `router.ex`

Now that you've created the necessary files,
open the router
Expand All @@ -140,7 +181,7 @@ you should see the following:

![liveveiw-page-with-tailwind-style](https://user-images.githubusercontent.com/194400/176137805-34467c88-add2-487f-9593-931d0314df62.png)

## 1.6 Update Tests
## 10.5 Update Tests

At this point we have made a few changes
that mean our automated test suite will no longer pass ...
Expand Down Expand Up @@ -204,3 +245,10 @@ Finished in 0.1 seconds (0.08s async, 0.1s sync)

Randomized with seed 796477
```




## 10.6 Group _Members_

Now that we have groups
19 changes: 19 additions & 0 deletions lib/auth/group.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Auth.Group do
use Ecto.Schema
import Ecto.Changeset

schema "groups" do
field :desc, :binary
field :kind, :integer
field :name, :binary

timestamps()
end

@doc false
def changeset(group, attrs) do
group
|> cast(attrs, [:name, :desc, :kind])
|> validate_required([:name, :desc, :kind])
end
end
5 changes: 0 additions & 5 deletions lib/auth_web/templates/layout/live.html.heex
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<body>
<header>
<section class="container">
<h1>Your App Name Here</h1>
</section>
</header>
<%= @inner_content %>
</body>
4 changes: 4 additions & 0 deletions lib/auth_web/templates/layout/nav.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<li class="pl5 pl6-m di-l tl pb2-m">
<a href="/roles" class="white link">Roles</a>
</li>

<li class="pl5 pl6-m di-l tl pb2-m">
<a href="/groups" class="white link">Groups</a>
</li>
<% end %>

<%= if Map.has_key?(@conn.assigns, :person) do %>
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ defmodule Auth.Mixfile do
# See the documentation for `Mix` for more info on aliases.
defp aliases do
[
"assets.deploy": ["esbuild default --minify", "phx.digest"],
c: ["coveralls.html"],
"ecto.setup": ["ecto.create --quiet", "ecto.migrate --quiet", "seeds"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
seeds: ["run priv/repo/seeds.exs"],
s: ["phx.server"],
test: ["ecto.reset", "test"],
"assets.deploy": ["esbuild default --minify", "phx.digest"],
s: ["phx.server"]
]
end

Expand Down
8 changes: 4 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mochiweb": {:hex, :mochiweb, "2.22.0", "f104d6747c01a330c38613561977e565b788b9170055c5241ac9dd6e4617cba5", [:rebar3], [], "hexpm", "cbbd1fd315d283c576d1c8a13e0738f6dafb63dc840611249608697502a07655"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"phoenix": {:hex, :phoenix, "1.6.11", "29f3c0fd12fa1fc4d4b05e341578e55bc78d96ea83a022587a7e276884d397e4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1664e34f80c25ea4918fbadd957f491225ef601c0e00b4e644b1a772864bfbc2"},
"phoenix": {:hex, :phoenix, "1.6.14", "57678366dc1d5bad49832a0fc7f12c2830c10d3eacfad681bfe9602cd4445f04", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d48c0da00b3d4cd1aad6055387917491af9f6e1f1e96cedf6c6b7998df9dba26"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"},
"phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.3.3", "3a53772a6118d5679bf50fc1670505a290e32a1d195df9e069d8c53ab040c054", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "766796676e5f558dbae5d1bdb066849673e956005e3730dfd5affd7a6da4abac"},
"phoenix_live_view": {:hex, :phoenix_live_view, "0.17.11", "205f6aa5405648c76f2abcd57716f42fc07d8f21dd8ea7b262dd12b324b50c95", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7177791944b7f90ed18f5935a6a5f07f760b36f7b3bdfb9d28c57440a3c43f99"},
"phoenix_live_view": {:hex, :phoenix_live_view, "0.18.2", "635cf07de947235deb030cd6b776c71a3b790ab04cebf526aa8c879fe17c7784", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6 or ~> 1.7", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "da287a77327e996cc166e4c440c3ad5ab33ccdb151b91c793209b39ebbce5b75"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.1", "ba04e489ef03763bf28a17eb2eaddc2c20c6d217e2150a61e3298b0f4c2012b5", [:mix], [], "hexpm", "81367c6d1eea5878ad726be80808eb5a787a23dee699f96e72b1109c57cdd8d9"},
"phoenix_view": {:hex, :phoenix_view, "1.1.2", "1b82764a065fb41051637872c7bd07ed2fdb6f5c3bd89684d4dca6e10115c95a", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "7ae90ad27b09091266f6adbb61e1d2516a7c3d7062c6789d46a7554ec40f3a56"},
"ping": {:hex, :ping, "1.1.0", "088a2e3356dc2a0f4b4b6b1dd50b08e108f31b0ea8058f20e621e0de27281415", [:mix], [{:plug, "~> 1.12.1", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "e05403a9fc8878e17e4b206fdaa77465c6b7fc4b9cc4dc3179dd3a93f39644b3"},
"plug": {:hex, :plug, "1.12.1", "645678c800601d8d9f27ad1aebba1fdb9ce5b2623ddb961a074da0b96c35187d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d57e799a777bc20494b784966dc5fbda91eb4a09f571f76545b72a634ce0d30b"},
"plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"},
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
"plug_crypto": {:hex, :plug_crypto, "1.2.3", "8f77d13aeb32bfd9e654cb68f0af517b371fb34c56c9f2b58fe3df1235c1251a", [:mix], [], "hexpm", "b5672099c6ad5c202c45f5a403f21a3411247f164e4a8fab056e5cd8a290f4a2"},
"postgrex": {:hex, :postgrex, "0.16.2", "0f83198d0e73a36e8d716b90f45f3bde75b5eebf4ade4f43fa1f88c90a812f74", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "a9ea589754d9d4d076121090662b7afe155b374897a6550eb288f11d755acfa0"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"rbac": {:hex, :rbac, "0.7.2", "3849d8428774e6d082fcb77b5c96c41257a21b03cf658ded582013fa967e44a2", [:mix], [{:auth_plug, "~> 1.4", [hex: :auth_plug, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.8.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.3.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "526bf9e2c8391453d5e93c9a97f47e29715920b7851979663ecf6f138e9b3a74"},
Expand Down
13 changes: 13 additions & 0 deletions priv/repo/migrations/20221017220747_create_groups.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Auth.Repo.Migrations.CreateGroups do
use Ecto.Migration

def change do
create table(:groups) do
add :name, :binary
add :desc, :binary
add :kind, :integer

timestamps()
end
end
end

0 comments on commit 7cf2ead

Please sign in to comment.