浏览代码

Working As Intended

From a browser perspective, not confirmed api is totally good atm.
jordan 3 年之前
父节点
当前提交
fd99eb227c

+ 4 - 0
lib/abcd.ex

@@ -22,6 +22,10 @@ defmodule ABCD.Fourletters do
     Agent.update(name, fn list -> append(list, value) end)
   end
 
+  def clear(name) do
+    Agent.update(name, fn _ -> [] end)
+  end
+
   defp append(lines, value) when is_list(lines) and length(lines) < 101 do
     lines ++ [value]
   end

+ 78 - 9
lib/fourletters_web/controllers/fourletters_controller.ex

@@ -1,15 +1,14 @@
 defmodule FourlettersWeb.FourlettersController do
   use FourlettersWeb, :controller
-  import Fourletters.Troll
-  import ABCD.Fourletters
-
+  # import Fourletters.Troll
+  # import ABCD.Fourletters
 
   def fourletters(conn, %{"fourletters" => fourletters}) do
     if String.length(fourletters) == 4 do
 
       pid = case Supervisor.start_child(
           Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [["oo"]]}}) do
+          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
         {:ok, pid} -> pid
         {:error, {:already_started, pid}} -> pid
       end
@@ -22,31 +21,101 @@ defmodule FourlettersWeb.FourlettersController do
 
 
   def addletters(conn, _params) do
-    %{params: params, path_params: path_params} = conn
+    %{params: params} = conn
+    %{"fourletters" => fourletters, "message" => message} = params
+    if String.length(fourletters) == 4 and String.length(message) < 101 do
+
+      pid = case Supervisor.start_child(
+          Fourletters.Troll,
+          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
+        {:ok, pid} -> pid
+        {:error, {:already_started, pid}} -> pid
+      end
+      _ = ABCD.Fourletters.put(pid, message)
+      # conn
+      # |> put_status(:ok)
+      # |> json(%{fourletters: fourletters, messages: messages})
+      # |> put_status(:ok)
+      # |> redirect(to: "/#{fourletters}")
+      redirect(conn, to: "/#{fourletters}")
+      # |> render("four.html", fourletters: fourletters, messages: messages)
+    else
+      json(conn, %{error: message})
+    end
+  end
+
+  def apiaddletters(conn, _params) do
+    %{params: params} = conn
     %{"fourletters" => fourletters, "message" => message} = params
     if String.length(fourletters) == 4 and String.length(message) < 101 do
 
       pid = case Supervisor.start_child(
           Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [["oo"]]}}) do
+          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
         {:ok, pid} -> pid
         {:error, {:already_started, pid}} -> pid
       end
-      messages = ABCD.Fourletters.put(pid, message)
+      _ = ABCD.Fourletters.put(pid, message)
       messages = ABCD.Fourletters.get(pid)
       conn
       |> put_status(:created)
-      # |> json(%{fourletters: fourletters, messages: messages})
+      |> json(%{fourletters: fourletters, messages: messages})
       # |> put_status(:ok)
       |> redirect(to: "/#{fourletters}")
+      # redirect(conn, to: "/#{fourletters}")
       # |> render("four.html", fourletters: fourletters, messages: messages)
     else
       json(conn, %{error: message})
     end
   end
 
+  def apiclearletters(conn, _params) do
+    %{params: params} = conn
+    %{"fourletters" => fourletters} = params
+    if String.length(fourletters) == 4 do
+      pid = case Supervisor.start_child(
+          Fourletters.Troll,
+          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
+        {:ok, pid} -> pid
+        {:error, {:already_started, pid}} -> pid
+      end
+      ABCD.Fourletters.clear(pid)
+      conn
+      |> put_status(:ok)
+      |> json(%{fourletters: fourletters, clear: true})
+      |> redirect(to: "/#{fourletters}")
+      # redirect(conn, to: "/redirect_test")
+      # redirect(conn, to: "/#{fourletters}")
+    end
+  end
+
+  def clearletters(conn, _params) do
+    %{params: params} = conn
+    %{"fourletters" => fourletters} = params
+    if String.length(fourletters) == 4 do
+      pid = case Supervisor.start_child(
+          Fourletters.Troll,
+          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
+        {:ok, pid} -> pid
+        {:error, {:already_started, pid}} -> pid
+      end
+      ABCD.Fourletters.clear(pid)
+      # conn
+      # |> put_status(:ok)
+      # redirect(conn, to: "/redirect_test")
+      redirect(conn, to: "/#{fourletters}")
+    end
+  end
+
+  def nothing(conn, %{"fourletters" => fourletters}) do
+    if String.length(fourletters) == 4 do
+      redirect(conn, to: "/#{fourletters}")
+    else
+      render(conn, "nothing.html")
+    end
+  end
 
-  def nothing(conn, _params) do
+  def nothing(conn, %{}) do
     render(conn, "nothing.html")
   end
 end

+ 7 - 0
lib/fourletters_web/router.ex

@@ -25,6 +25,13 @@ defmodule FourlettersWeb.Router do
     pipe_through :api
 
     post "/:fourletters", FourlettersController, :addletters
+
+    post "/api/:fourletters", FourlettersController, :apiaddletters
+
+    delete "/api/:fourletters", FourlettersController, :apiclearletters
+
+    # for stupid html forms that dont do DELETE
+    post "/api/clear/:fourletters", FourlettersController, :clearletters
   end
 
   # Enables LiveDashboard only for development

+ 4 - 0
lib/fourletters_web/templates/fourletters/four.html.eex

@@ -1,10 +1,14 @@
 <h1>hey <%= @fourletters %></h1>
+<a href="/">go home</a><br><br>
 <form action="" method="post">
 	<label for="message">message:</label><br>
 	<input type=”text” id="message" name="message"><br><br>
 	<input type="submit" value="Submit">
 </form>
 
+<form action="/api/clear/<%= @fourletters %>" method="post">
+	<input type="submit" value="Clear">
+</form>
 <ul>
 	<%= for message <- @messages do %>
 	<li>

+ 5 - 0
lib/fourletters_web/templates/fourletters/nothing.html.eex

@@ -1 +1,6 @@
 <h1>4-letters.net</h1>
+<form action="/" method="get">
+	<label for="fourletters">fourletters:</label><br>
+	<input type=”text” id="fourletters" name="fourletters"><br><br>
+	<input type="submit" value="Submit">
+</form>