fourletters_controller.ex 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. defmodule FourlettersWeb.FourlettersController do
  2. use FourlettersWeb, :controller
  3. # import Fourletters.Troll
  4. # import ABCD.Fourletters
  5. def fourletters(conn, %{"fourletters" => fourletters}) do
  6. if String.length(fourletters) == 4 do
  7. pid = case Supervisor.start_child(
  8. Fourletters.Troll,
  9. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  10. {:ok, pid} -> pid
  11. {:error, {:already_started, pid}} -> pid
  12. end
  13. messages = ABCD.Fourletters.get(pid)
  14. render(conn, "four.html", fourletters: fourletters, messages: messages)
  15. else
  16. redirect(conn, to: "/")
  17. end
  18. end
  19. def addletters(conn, _params) do
  20. %{params: params} = conn
  21. %{"fourletters" => fourletters, "message" => message} = params
  22. if String.length(fourletters) == 4 and String.length(message) < 101 do
  23. pid = case Supervisor.start_child(
  24. Fourletters.Troll,
  25. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  26. {:ok, pid} -> pid
  27. {:error, {:already_started, pid}} -> pid
  28. end
  29. _ = ABCD.Fourletters.put(pid, message)
  30. # conn
  31. # |> put_status(:ok)
  32. # |> json(%{fourletters: fourletters, messages: messages})
  33. # |> put_status(:ok)
  34. # |> redirect(to: "/#{fourletters}")
  35. redirect(conn, to: "/#{fourletters}")
  36. # |> render("four.html", fourletters: fourletters, messages: messages)
  37. else
  38. json(conn, %{error: message})
  39. end
  40. end
  41. def apiaddletters(conn, _params) do
  42. %{params: params} = conn
  43. %{"fourletters" => fourletters, "message" => message} = params
  44. if String.length(fourletters) == 4 and String.length(message) < 101 do
  45. pid = case Supervisor.start_child(
  46. Fourletters.Troll,
  47. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  48. {:ok, pid} -> pid
  49. {:error, {:already_started, pid}} -> pid
  50. end
  51. _ = ABCD.Fourletters.put(pid, message)
  52. messages = ABCD.Fourletters.get(pid)
  53. conn
  54. |> put_status(:created)
  55. |> json(%{fourletters: fourletters, messages: messages})
  56. # |> put_status(:ok)
  57. |> redirect(to: "/#{fourletters}")
  58. # redirect(conn, to: "/#{fourletters}")
  59. # |> render("four.html", fourletters: fourletters, messages: messages)
  60. else
  61. json(conn, %{error: message})
  62. end
  63. end
  64. def apiclearletters(conn, _params) do
  65. %{params: params} = conn
  66. %{"fourletters" => fourletters} = params
  67. if String.length(fourletters) == 4 do
  68. pid = case Supervisor.start_child(
  69. Fourletters.Troll,
  70. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  71. {:ok, pid} -> pid
  72. {:error, {:already_started, pid}} -> pid
  73. end
  74. ABCD.Fourletters.clear(pid)
  75. conn
  76. |> put_status(:ok)
  77. |> json(%{fourletters: fourletters, clear: true})
  78. |> redirect(to: "/#{fourletters}")
  79. # redirect(conn, to: "/redirect_test")
  80. # redirect(conn, to: "/#{fourletters}")
  81. end
  82. end
  83. def clearletters(conn, _params) do
  84. %{params: params} = conn
  85. %{"fourletters" => fourletters} = params
  86. if String.length(fourletters) == 4 do
  87. pid = case Supervisor.start_child(
  88. Fourletters.Troll,
  89. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  90. {:ok, pid} -> pid
  91. {:error, {:already_started, pid}} -> pid
  92. end
  93. ABCD.Fourletters.clear(pid)
  94. # conn
  95. # |> put_status(:ok)
  96. # redirect(conn, to: "/redirect_test")
  97. redirect(conn, to: "/#{fourletters}")
  98. end
  99. end
  100. def nothing(conn, %{"fourletters" => fourletters}) do
  101. if String.length(fourletters) == 4 do
  102. redirect(conn, to: "/#{fourletters}")
  103. else
  104. render(conn, "nothing.html")
  105. end
  106. end
  107. def nothing(conn, %{}) do
  108. render(conn, "nothing.html")
  109. end
  110. end