fourletters_controller.ex 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 getmessages(conn, %{"fourletters" => fourletters}) do
  20. if String.length(fourletters) == 4 do
  21. pid = case Supervisor.start_child(
  22. Fourletters.Troll,
  23. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  24. {:ok, pid} -> pid
  25. {:error, {:already_started, pid}} -> pid
  26. end
  27. messages = ABCD.Fourletters.get(pid)
  28. json(conn, %{fourletters: fourletters, messages: messages})
  29. else
  30. json(conn, %{welcome: "this is fourletters"})
  31. end
  32. end
  33. def addletters(conn, _params) do
  34. %{params: params} = conn
  35. %{"fourletters" => fourletters, "message" => message} = params
  36. if String.length(fourletters) == 4 and String.length(message) < 101 do
  37. pid = case Supervisor.start_child(
  38. Fourletters.Troll,
  39. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  40. {:ok, pid} -> pid
  41. {:error, {:already_started, pid}} -> pid
  42. end
  43. _ = ABCD.Fourletters.put(pid, message)
  44. # conn
  45. # |> put_status(:ok)
  46. # |> json(%{fourletters: fourletters, messages: messages})
  47. # |> put_status(:ok)
  48. # |> redirect(to: "/#{fourletters}")
  49. redirect(conn, to: "/#{fourletters}")
  50. # |> render("four.html", fourletters: fourletters, messages: messages)
  51. else
  52. redirect(conn, to: "/#{fourletters}")
  53. end
  54. end
  55. def apiaddletters(conn, _params) do
  56. %{params: params} = conn
  57. %{"fourletters" => fourletters, "message" => message} = params
  58. if String.length(fourletters) == 4 and String.length(message) < 101 do
  59. pid = case Supervisor.start_child(
  60. Fourletters.Troll,
  61. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  62. {:ok, pid} -> pid
  63. {:error, {:already_started, pid}} -> pid
  64. end
  65. _ = ABCD.Fourletters.put(pid, message)
  66. messages = ABCD.Fourletters.get(pid)
  67. conn
  68. |> put_status(:created)
  69. |> json(%{fourletters: fourletters, messages: messages})
  70. # |> put_status(:ok)
  71. |> redirect(to: "/#{fourletters}")
  72. # redirect(conn, to: "/#{fourletters}")
  73. # |> render("four.html", fourletters: fourletters, messages: messages)
  74. else
  75. json(conn, %{error: message})
  76. end
  77. end
  78. def apiclearletters(conn, _params) do
  79. %{params: params} = conn
  80. %{"fourletters" => fourletters} = params
  81. if String.length(fourletters) == 4 do
  82. pid = case Supervisor.start_child(
  83. Fourletters.Troll,
  84. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  85. {:ok, pid} -> pid
  86. {:error, {:already_started, pid}} -> pid
  87. end
  88. ABCD.Fourletters.clear(pid)
  89. conn
  90. |> put_status(:ok)
  91. |> json(%{fourletters: fourletters, clear: true})
  92. |> redirect(to: "/#{fourletters}")
  93. # redirect(conn, to: "/redirect_test")
  94. # redirect(conn, to: "/#{fourletters}")
  95. end
  96. end
  97. def clearletters(conn, _params) do
  98. %{params: params} = conn
  99. %{"fourletters" => fourletters} = params
  100. if String.length(fourletters) == 4 do
  101. pid = case Supervisor.start_child(
  102. Fourletters.Troll,
  103. %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
  104. {:ok, pid} -> pid
  105. {:error, {:already_started, pid}} -> pid
  106. end
  107. ABCD.Fourletters.clear(pid)
  108. # conn
  109. # |> put_status(:ok)
  110. # redirect(conn, to: "/redirect_test")
  111. redirect(conn, to: "/#{fourletters}")
  112. end
  113. end
  114. def nothing(conn, %{"fourletters" => fourletters}) do
  115. if String.length(fourletters) == 4 do
  116. redirect(conn, to: "/#{fourletters}")
  117. else
  118. render(conn, "nothing.html")
  119. end
  120. end
  121. def nothing(conn, %{}) do
  122. render(conn, "nothing.html")
  123. end
  124. end