some exceptions and validation logic implemented
This commit is contained in:
parent
6ac974f1e3
commit
709fb2ce30
11
calls.api
Normal file
11
calls.api
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"body": {
|
||||
"content": "",
|
||||
"file": false,
|
||||
"fileSize": false
|
||||
},
|
||||
"headers": {},
|
||||
"method": "GET",
|
||||
"title": "New HTTP Request",
|
||||
"url": ""
|
||||
}
|
@ -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
|
||||
|
@ -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]
|
||||
|
||||
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
|
||||
|
13
lib/draincloud_core_web/controllers/errors/errors.ex
Normal file
13
lib/draincloud_core_web/controllers/errors/errors.ex
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user