From a599dd8930dc6c63d80e7440e52a3dfc2063f0c8 Mon Sep 17 00:00:00 2001 From: optclblast Date: Mon, 2 Sep 2024 01:39:43 +0300 Subject: [PATCH] trying to implement all with elixir-way --- lib/draincloud_core/auth/auth.ex | 5 ++-- .../auth_controller/auth_controller.ex | 7 +++++ .../controllers/{ => errors}/error_html.ex | 0 .../controllers/{ => errors}/error_json.ex | 0 .../controllers/page_controller.ex | 14 ---------- .../page_controller/page_controller.ex | 26 +++++++++++++++++++ .../{ => page_controller}/page_html.ex | 0 .../page_html/home.html.heex | 0 .../page_html/login.html.heex | 0 .../controllers/request/request_utils.ex | 13 ++++++++++ priv/repo/migrations/20240901221317_users.exs | 7 +++++ 11 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex rename lib/draincloud_core_web/controllers/{ => errors}/error_html.ex (100%) rename lib/draincloud_core_web/controllers/{ => errors}/error_json.ex (100%) delete mode 100644 lib/draincloud_core_web/controllers/page_controller.ex create mode 100644 lib/draincloud_core_web/controllers/page_controller/page_controller.ex rename lib/draincloud_core_web/controllers/{ => page_controller}/page_html.ex (100%) rename lib/draincloud_core_web/controllers/{ => page_controller}/page_html/home.html.heex (100%) rename lib/draincloud_core_web/controllers/{ => page_controller}/page_html/login.html.heex (100%) create mode 100644 lib/draincloud_core_web/controllers/request/request_utils.ex create mode 100644 priv/repo/migrations/20240901221317_users.exs diff --git a/lib/draincloud_core/auth/auth.ex b/lib/draincloud_core/auth/auth.ex index 43acb86..abf9db2 100644 --- a/lib/draincloud_core/auth/auth.ex +++ b/lib/draincloud_core/auth/auth.ex @@ -1,5 +1,6 @@ defmodule DraincloudCore.Auth do - def is_logon(token) :: boolean() do - + def is_logon(_token) do + # TODO here + true 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 new file mode 100644 index 0000000..d2ec239 --- /dev/null +++ b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex @@ -0,0 +1,7 @@ +defmodule DrainCloudCoreWeb.AuthController do + use DrainCloudCoreWeb, :controller + + def logon(conn, _params) do + + end +end diff --git a/lib/draincloud_core_web/controllers/error_html.ex b/lib/draincloud_core_web/controllers/errors/error_html.ex similarity index 100% rename from lib/draincloud_core_web/controllers/error_html.ex rename to lib/draincloud_core_web/controllers/errors/error_html.ex diff --git a/lib/draincloud_core_web/controllers/error_json.ex b/lib/draincloud_core_web/controllers/errors/error_json.ex similarity index 100% rename from lib/draincloud_core_web/controllers/error_json.ex rename to lib/draincloud_core_web/controllers/errors/error_json.ex diff --git a/lib/draincloud_core_web/controllers/page_controller.ex b/lib/draincloud_core_web/controllers/page_controller.ex deleted file mode 100644 index 8921305..0000000 --- a/lib/draincloud_core_web/controllers/page_controller.ex +++ /dev/null @@ -1,14 +0,0 @@ -defmodule DrainCloudCoreWeb.PageController do - use DrainCloudCoreWeb, :controller - def home(conn, _params) when is_logon(conn) do - # The home page is often custom made, - # so skip the default app layout. - - if Map.has_key?(conn.req_headers, 'x-access-token') do - {_, token} = Map.fetch(conn.req_headers, 'x-access-token') - - end - - render(conn, :home, layout: false) - end -end diff --git a/lib/draincloud_core_web/controllers/page_controller/page_controller.ex b/lib/draincloud_core_web/controllers/page_controller/page_controller.ex new file mode 100644 index 0000000..95589a3 --- /dev/null +++ b/lib/draincloud_core_web/controllers/page_controller/page_controller.ex @@ -0,0 +1,26 @@ +defmodule DrainCloudCoreWeb.PageController do + use DrainCloudCoreWeb, :controller + + def home(conn, _params) do + # The home page is often custom made, + # so skip the default app layout. + + # TODO + # 1. check is user is logged in + # 2. if is - fetch the main page contents (files, folders etc) + # 2.1 fetch session by token. If NotFOund -> (3) + # 2.2 fetch content by sessions user_id + # 2.3 render page + # If not: + # 3. Redirect to the login page + + # if Map.has_key?(conn.req_headers, 'x-access-token') do + # {_, token} = Map.fetch(conn.req_headers, 'x-access-token') + + # end + + :logger.debug("[#{__MODULE__}] request: ", conn) + + render(conn, :home, layout: false) + end +end diff --git a/lib/draincloud_core_web/controllers/page_html.ex b/lib/draincloud_core_web/controllers/page_controller/page_html.ex similarity index 100% rename from lib/draincloud_core_web/controllers/page_html.ex rename to lib/draincloud_core_web/controllers/page_controller/page_html.ex diff --git a/lib/draincloud_core_web/controllers/page_html/home.html.heex b/lib/draincloud_core_web/controllers/page_controller/page_html/home.html.heex similarity index 100% rename from lib/draincloud_core_web/controllers/page_html/home.html.heex rename to lib/draincloud_core_web/controllers/page_controller/page_html/home.html.heex diff --git a/lib/draincloud_core_web/controllers/page_html/login.html.heex b/lib/draincloud_core_web/controllers/page_controller/page_html/login.html.heex similarity index 100% rename from lib/draincloud_core_web/controllers/page_html/login.html.heex rename to lib/draincloud_core_web/controllers/page_controller/page_html/login.html.heex diff --git a/lib/draincloud_core_web/controllers/request/request_utils.ex b/lib/draincloud_core_web/controllers/request/request_utils.ex new file mode 100644 index 0000000..2080458 --- /dev/null +++ b/lib/draincloud_core_web/controllers/request/request_utils.ex @@ -0,0 +1,13 @@ +defmodule DraincloudCoreWeb.Request do + # defstruct : + def has_token?(conn) do + # TODO try with pattern-matching please + # case conn do + # {} -> + # end + + if Map.has_key?(conn, "req_headers") do + Map.has_key?(conn.req_headers, 'x-access-token') + end + end +end diff --git a/priv/repo/migrations/20240901221317_users.exs b/priv/repo/migrations/20240901221317_users.exs new file mode 100644 index 0000000..8395c5a --- /dev/null +++ b/priv/repo/migrations/20240901221317_users.exs @@ -0,0 +1,7 @@ +defmodule DrainCloudCore.Repo.Migrations.Users do + use Ecto.Migration + + def change do + # TODO users table migration + end +end