So I'm writing a quick async username validation check, and following the tutorial on PhoenixFrameworks website, I can do it like so:
def validateUsername(conn, %{"username" => user}) do
IO.inspect(conn)
query = from u in User,
where: u.username == ^user,
select: [u.username]
case Repo.all(query) do
[[username]] ->
conn
|> json(%{ success: false, error: "Username has been taken" })
[] ->
conn
|> json(%{ success: true })
nil ->
conn
|> json(%{ success: true })
_ ->
conn
|> json(%{ success: false, error: "Internal Server Error"})
end
But this doesn't quite make sense to me, as I've never dealt with a functional programming language, and I know the data binding with =
in elixir works differently. But in my head I feel like it should be reversed like:
def validateUsername(conn, %{user => "username"})
Or something like that so my main question is
How does the %{"username" => user})
populate the user
variable with the relevant information?