Create schemas for JSON API endpoints
This commit is contained in:
parent
1ec18163c1
commit
7c73c2c424
11 changed files with 125 additions and 105 deletions
|
@ -34,11 +34,14 @@ defmodule MatrixServer.Account do
|
|||
end
|
||||
end
|
||||
|
||||
def register(params) do
|
||||
def register(account, params) do
|
||||
Multi.new()
|
||||
|> Multi.insert(:account, changeset(%Account{}, params))
|
||||
|> Multi.insert(:account, changeset(account, params))
|
||||
|> Multi.insert(:device, fn %{account: account} ->
|
||||
device_id = Device.generate_device_id(account)
|
||||
|
||||
Ecto.build_assoc(account, :devices)
|
||||
|> Map.put(:device_id, device_id)
|
||||
|> Device.changeset(params)
|
||||
end)
|
||||
|> Multi.run(:device_with_access_token, &Device.generate_access_token/2)
|
||||
|
|
|
@ -33,4 +33,13 @@ defmodule MatrixServer.Device do
|
|||
|> change(%{access_token: access_token})
|
||||
|> repo.update()
|
||||
end
|
||||
|
||||
def generate_device_id(%Account{localpart: localpart}) do
|
||||
time_string =
|
||||
DateTime.utc_now()
|
||||
|> DateTime.to_unix()
|
||||
|> Integer.to_string()
|
||||
|
||||
"#{localpart}_#{time_string}"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue