I'm doing my first Phoenix application, and trying to do new/2 controller. The code I wrote is
def new(conn, %{"fbid" => fbid, "latitude" => lat, "longitude" => lng, "content" => content}) do
{fbid, _} = Integer.parse(fbid);
{lat, _} = Float.parse(lat);
{lng, _} = Float.parse(lng);
chats = %Chat{:fbid => fbid, :latitude => lat, :longitude => lng, :content => content}
|> Repo.insert
|> List.wrap
|> Poison.encode!
render conn, chats: chats
end
But it looks horribly redundant and I can't find any better way to do this. I've read that there is no way to convert Map to Struct, and since the params differ in type it wouldn't work anyway.
So can anybody show me some magic way to map it?