page_controller.ex 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. defmodule Hello.PageController do
  2. use Hello.Web, :controller
  3. alias Hello.World
  4. alias Hello.Fortune
  5. plug :action
  6. plug :scrub_params, "world" when action in [:create, :update]
  7. def index(conn, _params) do
  8. json conn, %{"TE Benchmarks\n" => "Started"}
  9. end
  10. # avoid namespace collision
  11. def _json(conn, _params) do
  12. json conn, %{message: "Hello, world!"}
  13. end
  14. def db(conn, _params) do
  15. json conn, Repo.get(World, :random.uniform(10000))
  16. end
  17. def queries(conn, _params) do
  18. q = case String.to_integer(_params["queries"]) do
  19. x when x < 1 -> 1
  20. x when x > 500 -> 500
  21. x -> x
  22. end
  23. json conn, Enum.map(1..q, fn _ -> Repo.get(World, :random.uniform(10000)) end)
  24. end
  25. def fortunes(conn, _params) do
  26. fortunes = List.insert_at(Repo.all(Fortune), 0, %Fortune{:id => 0, :message => "Additional fortune added at request time."})
  27. render conn, "fortunes.html", fortunes: Enum.sort(fortunes, fn f1, f2 -> f1.message < f2.message end)
  28. end
  29. def updates(conn, _params) do
  30. q = case String.to_integer(_params["queries"]) do
  31. x when x < 1 -> 1
  32. x when x > 500 -> 500
  33. x -> x
  34. end
  35. json conn, Enum.map(1..q, fn _ ->
  36. w = Repo.get(World, :random.uniform(10000))
  37. changeset = World.changeset(w, %{randomNumber: :random.uniform(10000)})
  38. Repo.update(changeset)
  39. w end)
  40. end
  41. def plaintext(conn, _params) do
  42. text conn, "Hello, world!"
  43. end
  44. end