page_controller.ex 1.4 KB

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