diff --git a/calls.api b/calls.api new file mode 100644 index 0000000..1b34a2d --- /dev/null +++ b/calls.api @@ -0,0 +1,11 @@ +{ + "body": { + "content": "", + "file": false, + "fileSize": false + }, + "headers": {}, + "method": "GET", + "title": "New HTTP Request", + "url": "" +} \ No newline at end of file diff --git a/lib/draincloud_core/auth/users.ex b/lib/draincloud_core/auth/users.ex index 0f8229e..de1d1d3 100644 --- a/lib/draincloud_core/auth/users.ex +++ b/lib/draincloud_core/auth/users.ex @@ -4,8 +4,8 @@ defmodule DrainCloudCore.Auth.Users do schema "users" do field :login, :string - field :passwoed, :string, redact: true - field :crated_at, :utc_datetime + field :password, :string, redact: true + field :created_at, :utc_datetime field :updated_at, :utc_datetime field :deleted_at, :utc_datetime end 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 dcb0b24..7909a67 100644 --- a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex +++ b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex @@ -1,29 +1,70 @@ defmodule DrainCloudCoreWeb.AuthController do use DrainCloudCoreWeb, :controller + alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: Request + alias :logger, as: Log + alias DrainCloudCore.Repo, as: Repo - - def register(conn, _params) do + def register(conn = %Plug.Conn{}, _params) do req = Request.from_request(conn) - :logger.warning(req) + Log.debug("new register request: #{req}") + + model = Request.to_model req + + try do + Repo.insert(model) + rescue + e in RuntimeError -> Log.error("failed to insert new user: #{e}") + end + json(conn, req) end end defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do - @derive [Poison.Encoder, Jason.Encoder] + @derive [Poison.Encoder,Jason.Encoder] + defstruct login: "", password: "" - alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: RegisterRequest + 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}" {_, data, _} -> - Poison.decode!(data, as: %RegisterRequest{}) - _ -> raise "failed to read request body. uexpected result: #{res}" + req = Poison.decode!(data, as: %Request{}) + validate_request(req) + req + _ -> raise "failed to read request body. unexpected result #{Kernel.inspect(res)}" + end + end + + def to_model(req = %Request{}) do + %User{ + login: req.login, + password: req.password + } + end + + 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"]} + end + end + end + + defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do + def to_string(req) do + case Jason.encode(req) do + {:ok, str} -> str + {:error, msg} -> raise "failed to encode register request to string: #{msg}" + end end end end diff --git a/lib/draincloud_core_web/controllers/errors/errors.ex b/lib/draincloud_core_web/controllers/errors/errors.ex new file mode 100644 index 0000000..480901b --- /dev/null +++ b/lib/draincloud_core_web/controllers/errors/errors.ex @@ -0,0 +1,13 @@ +defmodule DrainCloudCoreWeb.Errors.InvalidArgumentException do + alias __MODULE__, as: InvalidArgumentException + defexception [:message, :args] + + @impl true + def exception(term) do + case term do + [] -> %InvalidArgumentException{} + {message, args} -> %InvalidArgumentException{message: message, args: args} + _ -> %InvalidArgumentException{message: "Error: " <> term} + end + end +end