mix format

This commit is contained in:
r8zavetr8v 2024-09-09 01:19:41 +03:00
parent 709fb2ce30
commit 312be30733
2 changed files with 25 additions and 12 deletions

View File

@ -10,7 +10,7 @@ defmodule DrainCloudCoreWeb.AuthController do
req = Request.from_request(conn) req = Request.from_request(conn)
Log.debug("new register request: #{req}") Log.debug("new register request: #{req}")
model = Request.to_model req model = Request.to_model(req)
try do try do
Repo.insert(model) Repo.insert(model)
@ -23,23 +23,29 @@ defmodule DrainCloudCoreWeb.AuthController do
end end
defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do
@derive [Poison.Encoder,Jason.Encoder] @derive [Poison.Encoder, Jason.Encoder]
defstruct login: "", password: "" defstruct login: "", password: ""
alias __MODULE__, as: Request alias __MODULE__, as: Request
alias DrainCloudCore.Auth.Users, as: User alias DrainCloudCore.Auth.Users, as: User
alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException
def from_request(conn) do def from_request(conn) do
res = Plug.Conn.read_body(conn) res = Plug.Conn.read_body(conn)
:logger.warning(Kernel.inspect(res)) :logger.warning(Kernel.inspect(res))
case res do case res do
{:error, _} -> raise "failed to read request body: #{:error}" {:error, _} ->
raise "failed to read request body: #{:error}"
{_, data, _} -> {_, data, _} ->
req = Poison.decode!(data, as: %Request{}) req = Poison.decode!(data, as: %Request{})
validate_request(req) validate_request(req)
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
end end
@ -52,12 +58,19 @@ defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do
defp validate_request(req = %Request{}) do defp validate_request(req = %Request{}) do
if String.length(req.login) < 3 do if String.length(req.login) < 3 do
raise InvalidArgumentException, %{message: "login must be 3 symbols at minimum", args: ["login"]} raise InvalidArgumentException, %{
else if String.length(req.password) < 8 do message: "login must be 3 symbols at minimum",
raise InvalidArgumentException, %{message: "password must be 8 symbols at minimum", args: ["password"]} 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 end
end
defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do
def to_string(req) do def to_string(req) do

View File

@ -3,11 +3,11 @@ defmodule DrainCloudCore.Repo.Migrations.AddUsers do
def change do def change do
create table(:users) do create table(:users) do
add :login, :string, [size: 120] add :login, :string, size: 120
add :password, :string add :password, :string
add :created_at, :utc_datetime, [null: false, default: fragment("CURRENT_TIMESTAMP")] add :created_at, :utc_datetime, null: false, default: fragment("CURRENT_TIMESTAMP")
add :updated_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 :deleted_at, :utc_datetime, null: true, default: nil
end end
create unique_index(:users, :login) create unique_index(:users, :login)