fourletters_controller.ex 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. defmodule FourlettersWeb.FourlettersController do
  2. use FourlettersWeb, :controller
  3. require Logger
  4. # import Fourletters.Troll
  5. # import ABCD.Fourletters
  6. def fourletters(conn, %{"fourletters" => fourletters}) do
  7. if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) do
  8. pid = case Supervisor.start_child(
  9. Fourletters.Troll,
  10. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  11. {:ok, pid} -> pid
  12. {:error, {:already_started, pid}} -> pid
  13. end
  14. messages = ABCD.Fourletters.get(pid)
  15. render(conn, "four.html", fourletters: fourletters, messages: messages)
  16. else
  17. redirect(conn, to: "/")
  18. end
  19. end
  20. def getmessages(conn, %{"fourletters" => fourletters}) do
  21. if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) do
  22. pid = case Supervisor.start_child(
  23. Fourletters.Troll,
  24. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  25. {:ok, pid} -> pid
  26. {:error, {:already_started, pid}} -> pid
  27. end
  28. messages = ABCD.Fourletters.get(pid)
  29. json(conn, %{fourletters: fourletters, messages: messages})
  30. else
  31. json(conn, %{welcome: "this is fourletters"})
  32. end
  33. end
  34. def addletters(conn, _params) do
  35. %{params: params} = conn
  36. %{"fourletters" => fourletters, "message" => message} = params
  37. if String.length(fourletters) == 4 and String.length(message) < 101 and fourletters =~ ~r(^[a-z!]*$) do
  38. Logger.info params
  39. pid = case Supervisor.start_child(
  40. Fourletters.Troll,
  41. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  42. {:ok, pid} -> pid
  43. {:error, {:already_started, pid}} -> pid
  44. end
  45. _ = ABCD.Fourletters.put(pid, message)
  46. # conn
  47. # |> put_status(:ok)
  48. # |> json(%{fourletters: fourletters, messages: messages})
  49. # |> put_status(:ok)
  50. # |> redirect(to: "/#{fourletters}")
  51. redirect(conn, to: "/#{fourletters}")
  52. # |> render("four.html", fourletters: fourletters, messages: messages)
  53. else
  54. redirect(conn, to: "/#{fourletters}")
  55. end
  56. end
  57. def apiaddletters(conn, _params) do
  58. %{params: params} = conn
  59. %{"fourletters" => fourletters, "message" => message} = params
  60. if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) and String.length(message) < 101 do
  61. Logger.info fn ->
  62. "#{DateTime.utc_now} #{inspect(params)}"
  63. end
  64. pid = case Supervisor.start_child(
  65. Fourletters.Troll,
  66. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  67. {:ok, pid} -> pid
  68. {:error, {:already_started, pid}} -> pid
  69. end
  70. _ = ABCD.Fourletters.put(pid, message)
  71. messages = ABCD.Fourletters.get(pid)
  72. conn
  73. |> put_status(:created)
  74. |> json(%{fourletters: fourletters, messages: messages})
  75. # |> put_status(:ok)
  76. |> redirect(to: "/#{fourletters}")
  77. # redirect(conn, to: "/#{fourletters}")
  78. # |> render("four.html", fourletters: fourletters, messages: messages)
  79. else
  80. json(conn, %{error: message})
  81. end
  82. end
  83. def apiclearletters(conn, _params) do
  84. %{params: params} = conn
  85. %{"fourletters" => fourletters} = params
  86. if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) 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. # |> json(%{fourletters: fourletters, clear: true})
  97. # |> redirect(to: "/#{fourletters}")
  98. # redirect(conn, to: "/redirect_test")
  99. redirect(conn, to: "/#{fourletters}")
  100. end
  101. end
  102. def clearletters(conn, _params) do
  103. %{params: params} = conn
  104. %{"fourletters" => fourletters} = params
  105. if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) do
  106. pid = case Supervisor.start_child(
  107. Fourletters.Troll,
  108. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  109. {:ok, pid} -> pid
  110. {:error, {:already_started, pid}} -> pid
  111. end
  112. ABCD.Fourletters.clear(pid)
  113. # conn
  114. # |> put_status(:ok)
  115. # redirect(conn, to: "/redirect_test")
  116. redirect(conn, to: "/#{fourletters}")
  117. end
  118. end
  119. def nothing(conn, %{"fourletters" => fourletters}) do
  120. if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) do
  121. redirect(conn, to: "/#{fourletters}")
  122. else
  123. render(conn, "nothing.html")
  124. end
  125. end
  126. def nothing(conn, %{}) do
  127. render(conn, "nothing.html")
  128. end
  129. end