feat(paste): add user id if user is logged in
Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
parent
c99df5e8bd
commit
f5525d2d20
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user