From f5525d2d209ddbc1d0e7c078320748a7e78002b4 Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Sun, 15 Aug 2021 03:19:03 +0530 Subject: [PATCH] feat(paste): add user id if user is logged in Signed-off-by: SphericalKat --- lib/ketbin/pastes/paste.ex | 2 +- lib/ketbin_web/controllers/page_controller.ex | 8 ++++++-- .../controllers/user_registration_controller.ex | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ketbin/pastes/paste.ex b/lib/ketbin/pastes/paste.ex index 6b73696..b0e36bf 100644 --- a/lib/ketbin/pastes/paste.ex +++ b/lib/ketbin/pastes/paste.ex @@ -13,7 +13,7 @@ defmodule Ketbin.Pastes.Paste do @doc false def changeset(paste, attrs) do paste - |> cast(attrs, [:is_url, :content, :id]) + |> cast(attrs, [:is_url, :content, :id, :belongs_to]) |> validate_required([:is_url, :content]) end end diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index 89f551f..4b94692 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -1,4 +1,6 @@ defmodule KetbinWeb.PageController do + require Logger + use KetbinWeb, :controller alias Ketbin.Pastes @@ -34,17 +36,19 @@ defmodule KetbinWeb.PageController do # generate phonetic key id = Utils.generate_key() - IO.puts(conn.assigns[:current_user]) - # check if content is a url is_url = Map.get(paste_params, "content") |> Utils.is_url?() + # pull off current user if exists + current_user = conn.assigns.current_user + # put id and is_url values into changeset paste_params = Map.put(paste_params, "id", id) |> Map.put("is_url", is_url) + |> Map.put("belongs_to", (if current_user, do: current_user.id, else: nil)) # attempt to create a paste case Pastes.create_paste(paste_params) do diff --git a/lib/ketbin_web/controllers/user_registration_controller.ex b/lib/ketbin_web/controllers/user_registration_controller.ex index 6ed2ff2..5dc8cf4 100644 --- a/lib/ketbin_web/controllers/user_registration_controller.ex +++ b/lib/ketbin_web/controllers/user_registration_controller.ex @@ -22,6 +22,7 @@ defmodule KetbinWeb.UserRegistrationController do conn |> put_flash(:info, "User created successfully.") |> UserAuth.log_in_user(user) + |> redirect(to: "/users/confirm") {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset)