[day-01] setup imc calc and route
This commit is contained in:
parent
8658384030
commit
fc73f30dd0
32
lib/wabanex/imc.ex
Normal file
32
lib/wabanex/imc.ex
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
defmodule Wabanex.IMC do
|
||||||
|
def calculate(%{"filename" => filename}) do
|
||||||
|
filename
|
||||||
|
|> File.read()
|
||||||
|
|> handle_file()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_file({:ok, content}) do
|
||||||
|
data =
|
||||||
|
content
|
||||||
|
|> String.trim()
|
||||||
|
|> String.split("\n")
|
||||||
|
|> Enum.map(&parse_line/1)
|
||||||
|
|> Enum.into(%{})
|
||||||
|
|
||||||
|
{:ok, data}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_file({:error, _reason}) do
|
||||||
|
{:error, "Error while opening the file"}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_line(line) do
|
||||||
|
line
|
||||||
|
|> String.split(",")
|
||||||
|
|> List.update_at(1, &String.to_float/1)
|
||||||
|
|> List.update_at(2, &String.to_float/1)
|
||||||
|
|> calculate_imc()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp calculate_imc([name, height, weight]), do: {name, weight / :math.pow(height / 100, 2)}
|
||||||
|
end
|
20
lib/wabanex_web/controllers/imc_controller.ex
Normal file
20
lib/wabanex_web/controllers/imc_controller.ex
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
defmodule WabanexWeb.IMCController do
|
||||||
|
use WabanexWeb, :controller
|
||||||
|
|
||||||
|
alias Wabanex.IMC
|
||||||
|
|
||||||
|
def index(conn, params) do
|
||||||
|
params
|
||||||
|
|> IMC.calculate()
|
||||||
|
|> handle_response(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_response({:ok, result}, conn), do: render_response(conn, :ok, result)
|
||||||
|
def handle_response({:error, result}, conn), do: render_response(conn, :bad_request, result)
|
||||||
|
|
||||||
|
defp render_response(conn, status, result) do
|
||||||
|
conn
|
||||||
|
|> put_status(status)
|
||||||
|
|> json(%{result: result})
|
||||||
|
end
|
||||||
|
end
|
@ -7,6 +7,8 @@ defmodule WabanexWeb.Router do
|
|||||||
|
|
||||||
scope "/api", WabanexWeb do
|
scope "/api", WabanexWeb do
|
||||||
pipe_through :api
|
pipe_through :api
|
||||||
|
|
||||||
|
get "/", IMCController, :index
|
||||||
end
|
end
|
||||||
|
|
||||||
# Enables LiveDashboard only for development
|
# Enables LiveDashboard only for development
|
||||||
|
5
students.csv
Normal file
5
students.csv
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
João,168.0,90.0
|
||||||
|
Claudio,172.0,90.0
|
||||||
|
Americo,169.0,70.0
|
||||||
|
Ana,165.0,58.0
|
||||||
|
Luiz,180.0,90.0
|
|
Loading…
x
Reference in New Issue
Block a user