diff --git a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex index 7909a67..d15d56d 100644 --- a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex +++ b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex @@ -10,7 +10,7 @@ defmodule DrainCloudCoreWeb.AuthController do req = Request.from_request(conn) Log.debug("new register request: #{req}") - model = Request.to_model req + model = Request.to_model(req) try do Repo.insert(model) @@ -23,23 +23,29 @@ defmodule DrainCloudCoreWeb.AuthController do end defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do - @derive [Poison.Encoder,Jason.Encoder] + @derive [Poison.Encoder, Jason.Encoder] defstruct login: "", password: "" alias __MODULE__, as: Request alias DrainCloudCore.Auth.Users, as: User alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException + def from_request(conn) do res = Plug.Conn.read_body(conn) :logger.warning(Kernel.inspect(res)) + case res do - {:error, _} -> raise "failed to read request body: #{:error}" + {:error, _} -> + raise "failed to read request body: #{:error}" + {_, data, _} -> req = Poison.decode!(data, as: %Request{}) validate_request(req) req - _ -> raise "failed to read request body. unexpected result #{Kernel.inspect(res)}" + + _ -> + raise "failed to read request body. unexpected result #{Kernel.inspect(res)}" end end @@ -52,12 +58,19 @@ defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do defp validate_request(req = %Request{}) do if String.length(req.login) < 3 do - raise InvalidArgumentException, %{message: "login must be 3 symbols at minimum", args: ["login"]} - else if String.length(req.password) < 8 do - raise InvalidArgumentException, %{message: "password must be 8 symbols at minimum", args: ["password"]} + raise InvalidArgumentException, %{ + message: "login must be 3 symbols at minimum", + args: ["login"] + } + else + if String.length(req.password) < 8 do + raise InvalidArgumentException, %{ + message: "password must be 8 symbols at minimum", + args: ["password"] + } + end end end - end defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do def to_string(req) do diff --git a/priv/repo/migrations/20240908091209_add_users.exs b/priv/repo/migrations/20240908091209_add_users.exs index 11df9b1..cd4a94b 100644 --- a/priv/repo/migrations/20240908091209_add_users.exs +++ b/priv/repo/migrations/20240908091209_add_users.exs @@ -3,11 +3,11 @@ defmodule DrainCloudCore.Repo.Migrations.AddUsers do def change do create table(:users) do - add :login, :string, [size: 120] + add :login, :string, size: 120 add :password, :string - add :created_at, :utc_datetime, [null: false, default: fragment("CURRENT_TIMESTAMP")] - add :updated_at, :utc_datetime, [null: false, default: fragment("CURRENT_TIMESTAMP")] - add :deleted_at, :utc_datetime, [null: true, default: nil] + add :created_at, :utc_datetime, null: false, default: fragment("CURRENT_TIMESTAMP") + add :updated_at, :utc_datetime, null: false, default: fragment("CURRENT_TIMESTAMP") + add :deleted_at, :utc_datetime, null: true, default: nil end create unique_index(:users, :login)