Bladeren bron

4-letter char restrictions

Can be better abstracted, more DRY. Will do later (probably).
jordan 3 jaren geleden
bovenliggende
commit
20e5fe28ee
1 gewijzigde bestanden met toevoegingen van 17 en 12 verwijderingen
  1. 17 12
      lib/fourletters_web/controllers/fourletters_controller.ex

+ 17 - 12
lib/fourletters_web/controllers/fourletters_controller.ex

@@ -1,10 +1,11 @@
 defmodule FourlettersWeb.FourlettersController do
   use FourlettersWeb, :controller
+  require Logger
   # import Fourletters.Troll
   # import ABCD.Fourletters
 
   def fourletters(conn, %{"fourletters" => fourletters}) do
-    if String.length(fourletters) == 4 do
+    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) do
 
       pid = case Supervisor.start_child(
           Fourletters.Troll,
@@ -20,7 +21,7 @@ defmodule FourlettersWeb.FourlettersController do
   end
 
   def getmessages(conn, %{"fourletters" => fourletters}) do
-    if String.length(fourletters) == 4 do
+    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
 
       pid = case Supervisor.start_child(
           Fourletters.Troll,
@@ -38,8 +39,9 @@ defmodule FourlettersWeb.FourlettersController do
   def addletters(conn, _params) do
     %{params: params} = conn
     %{"fourletters" => fourletters, "message" => message} = params
-    if String.length(fourletters) == 4 and String.length(message) < 101 do
+    if String.length(fourletters) == 4 and String.length(message) < 101 and fourletters =~ ~r(^[a-z!]*$)  do
 
+      Logger.info params
       pid = case Supervisor.start_child(
           Fourletters.Troll,
           %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
@@ -62,8 +64,11 @@ defmodule FourlettersWeb.FourlettersController do
   def apiaddletters(conn, _params) do
     %{params: params} = conn
     %{"fourletters" => fourletters, "message" => message} = params
-    if String.length(fourletters) == 4 and String.length(message) < 101 do
+    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  and String.length(message) < 101 do
 
+      Logger.info fn ->
+        "#{DateTime.utc_now} #{inspect(params)}"
+      end
       pid = case Supervisor.start_child(
           Fourletters.Troll,
           %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
@@ -87,7 +92,7 @@ defmodule FourlettersWeb.FourlettersController do
   def apiclearletters(conn, _params) do
     %{params: params} = conn
     %{"fourletters" => fourletters} = params
-    if String.length(fourletters) == 4 do
+    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
       pid = case Supervisor.start_child(
           Fourletters.Troll,
           %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
@@ -95,19 +100,19 @@ defmodule FourlettersWeb.FourlettersController do
         {:error, {:already_started, pid}} -> pid
       end
       ABCD.Fourletters.clear(pid)
-      conn
-      |> put_status(:ok)
-      |> json(%{fourletters: fourletters, clear: true})
-      |> redirect(to: "/#{fourletters}")
+      # conn
+      # |> put_status(:ok)
+      # |> json(%{fourletters: fourletters, clear: true})
+      # |> redirect(to: "/#{fourletters}")
       # redirect(conn, to: "/redirect_test")
-      # redirect(conn, to: "/#{fourletters}")
+      redirect(conn, to: "/#{fourletters}")
     end
   end
 
   def clearletters(conn, _params) do
     %{params: params} = conn
     %{"fourletters" => fourletters} = params
-    if String.length(fourletters) == 4 do
+    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
       pid = case Supervisor.start_child(
           Fourletters.Troll,
           %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
@@ -123,7 +128,7 @@ defmodule FourlettersWeb.FourlettersController do
   end
 
   def nothing(conn, %{"fourletters" => fourletters}) do
-    if String.length(fourletters) == 4 do
+    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
       redirect(conn, to: "/#{fourletters}")
     else
       render(conn, "nothing.html")