2017-02-03 8 views
0

私は、電子メールアドレスを取り込んでユーザーをユーザーのリストから探す簡単なサービスを作成しています。エリクシールのリストから値を取得する

ここでは、ユーザーのリストを含む簡略化されたバージョンです。私は自分の電子メールアドレスに基づいてユーザーを抽出したいと思います。

def endpoint do 
    [%{email: "[email protected]", account_type: "full"}, 
    %{email: "[email protected]", account_type: "standard"}, 
    %{email: "[email protected]", account_type: "full"}] 
end 

def get_by_email(user, email) do 
    user |> Map.get(:email) 
end 

def dev_endpoint(email) do 
    endpoint 
    |> Enum.map(&get_by_email(email)/1) 
end 

def show(conn, %{"id" => email}) do 
    response = dev_endpoint(email) 
    json(conn, %{"email" => response}) 
end 

ので、本質的に:

dev_endpoint("[email protected]") 

は返す必要があります。

%{email: "[email protected]", account_type: "full"} 

私は私のキャプチャ構文に何か問題があると知っているが、私はそれの様々な異なる反復を試してみました運がない。

+1

は、たぶん私は完全にあなたがしたいが、 'エンドポイントではないが何であるかを理解することはできません。

def endpoint do [%{email: "[email protected]", account_type: "full"}, %{email: "[email protected]", account_type: "standard"}, %{email: "[email protected]", account_type: "full"}] end def find_by_email(email) do Enum.find(endpoint, fn u -> u.email == email end) end 

は今、あなたはちょうどこれを使用することができます。これは、私はそれを使用する方法でありますemail == Map.get(&1、:email))) '十分ですか? – ymonad

答えて

関連する問題