feat(paste): add user id if user is logged in

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2021-08-15 03:19:03 +05:30
parent c99df5e8bd
commit f5525d2d20
No known key found for this signature in database
GPG Key ID: ED5C54FBBB920E51
3 changed files with 8 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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)