Skip to content

Commit 6f20305

Browse files
committed
feat: Creating generic UserTable module with ETS. #1
1 parent 5cc8933 commit 6f20305

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,8 +1129,76 @@ def deps do
11291129
end
11301130
```
11311131

1132+
With that installed,
1133+
we are going to be creating a module
1134+
that will *manage the users table*.
1135+
We are going to create it on startup,
1136+
and create users/edit them.
11321137

1138+
For that, create a file in `lib/app/users.ex`.
11331139

1140+
```elixir
1141+
defmodule UsersTable do
1142+
1143+
alias ETS.Set
1144+
1145+
@table :users_table
1146+
1147+
def init do
1148+
Set.new(name: @table)
1149+
end
1150+
1151+
def list_users do
1152+
@table
1153+
|> Set.wrap_existing!()
1154+
|> Set.to_list()
1155+
end
1156+
1157+
def create_user(stripe_id) do
1158+
@table
1159+
|> Set.wrap_existing!()
1160+
|> Set.put_new( {stripe_id, false})
1161+
end
1162+
1163+
def update_payment_status(%{:stripe_id => stripe_id, :status => status}) do
1164+
@table
1165+
|> Set.wrap_existing!()
1166+
|> Set.put({stripe_id, status})
1167+
end
1168+
1169+
end
1170+
```
1171+
1172+
Let's go over what we have done.
1173+
We are going to be saving our users
1174+
with a tuple containing the `**stripe_id**`
1175+
and a `**status**` boolean field,
1176+
referring to whether the user has paid or not.
1177+
1178+
All the functions being used are used
1179+
according to the [`ets` wrapper documentation](https://github.com/TheFirstAvenger/ets).
1180+
- the `init/0` function creates the table to store our users.
1181+
- `list_users` returns the list of users.
1182+
- `create_user/1` receives a `stripe_id` and creates a user object.
1183+
By default, the `status` field is created with `false`.
1184+
- `update_payment_status/1` receives a user object
1185+
and updates accordingly.
1186+
1187+
Let's make use of some of these functions.
1188+
We want to setup the `ETS` table on the process startup.
1189+
For this, we are going to initiate the table
1190+
on the `start/1` function inside `lib/app/application.ex`.
1191+
This functions is executed when the process is created,
1192+
so it fits right our needs!
1193+
1194+
Inside this function, add:
1195+
1196+
```elixir
1197+
# Creating ETS user table
1198+
{:ok, _set} = UsersTable.init()
1199+
```
1200+
1201+
Awesome!
11341202

11351203
# Thanks!
11361204

lib/app/application.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ defmodule App.Application do
2222
# {App.Worker, arg}
2323
]
2424

25+
# Creating ETS user table
26+
{:ok, _set} = UsersTable.init()
27+
2528
# See https://hexdocs.pm/elixir/Supervisor.html
2629
# for other strategies and supported options
2730
opts = [strategy: :one_for_one, name: App.Supervisor]

lib/app/users.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule UsersTable do
2+
3+
alias ETS.Set
4+
5+
@table :users_table
6+
7+
def init do
8+
Set.new(name: @table)
9+
end
10+
11+
def list_users do
12+
@table
13+
|> Set.wrap_existing!()
14+
|> Set.to_list()
15+
end
16+
17+
def create_user(stripe_id) do
18+
@table
19+
|> Set.wrap_existing!()
20+
|> Set.put_new( {stripe_id, false})
21+
end
22+
23+
def update_payment_status(%{:stripe_id => stripe_id, :status => status}) do
24+
@table
25+
|> Set.wrap_existing!()
26+
|> Set.put({stripe_id, status})
27+
end
28+
29+
end

0 commit comments

Comments
 (0)