From 1c3862471285ef366079e9ff3305f5658838136a Mon Sep 17 00:00:00 2001 From: optclblast Date: Sun, 15 Sep 2024 23:56:12 +0300 Subject: [PATCH] some lazy changes --- lib/draincloud_core/{ => repo}/repo.ex | 0 lib/draincloud_core/repo/user_repo.ex | 8 ++ .../auth_controller/auth_controller.ex | 79 ++----------------- .../auth_controller/login_request.ex | 71 +++++++++++++++++ .../auth_controller/register_request.ex | 71 +++++++++++++++++ 5 files changed, 158 insertions(+), 71 deletions(-) rename lib/draincloud_core/{ => repo}/repo.ex (100%) create mode 100644 lib/draincloud_core/repo/user_repo.ex create mode 100644 lib/draincloud_core_web/controllers/auth_controller/login_request.ex create mode 100644 lib/draincloud_core_web/controllers/auth_controller/register_request.ex diff --git a/lib/draincloud_core/repo.ex b/lib/draincloud_core/repo/repo.ex similarity index 100% rename from lib/draincloud_core/repo.ex rename to lib/draincloud_core/repo/repo.ex diff --git a/lib/draincloud_core/repo/user_repo.ex b/lib/draincloud_core/repo/user_repo.ex new file mode 100644 index 0000000..63687a3 --- /dev/null +++ b/lib/draincloud_core/repo/user_repo.ex @@ -0,0 +1,8 @@ +defmodule DrainCloudCore.UsersRepo do + alias DrainCloudCore.Repo, as: Repo + import Ecto.Query + + def password_by_login(login) do + Repo.all(from u in :users, where: u.login = login, select: u.password, limit: 1) + end +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 ca7c9a4..5979750 100644 --- a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex +++ b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex @@ -1,17 +1,18 @@ defmodule DrainCloudCoreWeb.AuthController do use DrainCloudCoreWeb, :controller - alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: Request + alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: RegisterRequest + alias DrainCloudCoreWeb.AuthController.LoginRequest, as: LoginRequest alias :logger, as: Log alias DrainCloudCore.Repo, as: Repo - def register(conn = %Plug.Conn{}, _params) do - req = Request.from_request(conn) + def register(conn, _params) do + req = RegisterRequest.from_request(conn) Log.debug("new register request: #{req}") - model = Request.to_model(req) + model = RegisterRequest.to_model(req) try do Repo.insert(model) @@ -21,75 +22,11 @@ defmodule DrainCloudCoreWeb.AuthController do json(conn, req) end -end -defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do - @derive [Poison.Encoder, Jason.Encoder] + def login(conn, _params) do + req = LoginRequest.from_request(conn) - defstruct login: "", password: "" + Log.debug("new login request: #{req}") - alias __MODULE__, as: Request - alias DrainCloudCore.Auth.Users, as: User - alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException - - def from_request(conn = %Plug.Conn{}) do - :logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}") - - validate_and_build(conn.params) - end - - def to_model(req = %Request{}) do - %User{ - login: req.login, - password: req.password - } - end - - defp validate_and_build(req) do - validate(req) - |> build - end - - defp validate(req) do - validate_login(req) - |> validate_password - end - - defp validate_login(req) do - if String.length(Map.get(req, "login")) >= 3 do - req - else - raise InvalidArgumentException, %{ - message: "login must be 3 symbols at minimum", - args: ["login"] - } - end - end - - defp validate_password(req) do - if String.length(Map.get(req, "password")) >= 8 do - req - else - raise InvalidArgumentException, %{ - message: "password must be 8 symbols at minimum", - args: ["password"] - } - end - end - - defp build(req) do - %Request{ - login: Map.get(req, "login"), - password: Map.get(req, "password") - } - 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/auth_controller/login_request.ex b/lib/draincloud_core_web/controllers/auth_controller/login_request.ex new file mode 100644 index 0000000..32d9b56 --- /dev/null +++ b/lib/draincloud_core_web/controllers/auth_controller/login_request.ex @@ -0,0 +1,71 @@ +defmodule DraincloudCoreWeb.AuthController.LoginRequest do + @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 = %Plug.Conn{}) do + # TODO remove Kernel.inspect calls + :logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}") + + validate_and_build(conn.params) + end + + def to_model(req = %Request{}) do + %User{ + login: req.login, + password: req.password + } + end + + defp validate_and_build(req) do + validate(req) + |> build + end + + defp validate(req) do + validate_login(req) + |> validate_password + end + + defp validate_login(req) do + if String.length(Map.get(req, "login")) >= 3 do + req + else + raise InvalidArgumentException, %{ + message: "login must be 3 symbols at minimum", + args: ["login"] + } + end + end + + defp validate_password(req) do + if String.length(Map.get(req, "password")) >= 8 do + req + else + raise InvalidArgumentException, %{ + message: "password must be 8 symbols at minimum", + args: ["password"] + } + end + end + + defp build(req) do + %Request{ + login: Map.get(req, "login"), + password: Map.get(req, "password") + } + end + + defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.LoginRequest do + def to_string(req) do + case Jason.encode(req) do + {:ok, str} -> str + {:error, msg} -> raise "failed to encode login request to string: #{msg}" + end + end + end +end diff --git a/lib/draincloud_core_web/controllers/auth_controller/register_request.ex b/lib/draincloud_core_web/controllers/auth_controller/register_request.ex new file mode 100644 index 0000000..8392288 --- /dev/null +++ b/lib/draincloud_core_web/controllers/auth_controller/register_request.ex @@ -0,0 +1,71 @@ +defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do + @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 = %Plug.Conn{}) do + # TODO remove Kernel.inspect calls + :logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}") + + validate_and_build(conn.params) + end + + def to_model(req = %Request{}) do + %User{ + login: req.login, + password: req.password + } + end + + defp validate_and_build(req) do + validate(req) + |> build + end + + defp validate(req) do + validate_login(req) + |> validate_password + end + + defp validate_login(req) do + if String.length(Map.get(req, "login")) >= 3 do + req + else + raise InvalidArgumentException, %{ + message: "login must be 3 symbols at minimum", + args: ["login"] + } + end + end + + defp validate_password(req) do + if String.length(Map.get(req, "password")) >= 8 do + req + else + raise InvalidArgumentException, %{ + message: "password must be 8 symbols at minimum", + args: ["password"] + } + end + end + + defp build(req) do + %Request{ + login: Map.get(req, "login"), + password: Map.get(req, "password") + } + 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