diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index 4b94692..8687c39 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -15,16 +15,29 @@ defmodule KetbinWeb.PageController do def show(conn, %{"id" => id}) do paste = Pastes.get_paste!(id) # fetch paste from db + # pull off current user if exists + current_user = conn.assigns.current_user + + # show edit if current user matches creator of paste + show_edit = current_user && current_user.id || false + if paste.is_url do # paste is a url, redirect redirect(conn, external: paste.content) else # regular paste, show content - render(conn, "show.html", paste: paste) + render(conn, "show.html", paste: paste, show_edit: show_edit) end end def showlink(conn, %{"id" => id}) do paste = Pastes.get_paste!(id) - render(conn, "show.html", paste: paste) + + # pull off current user if exists + current_user = conn.assigns.current_user + + # show edit if current user matches creator of paste + show_edit = current_user && current_user.id || false + + render(conn, "show.html", paste: paste, show_edit: show_edit) end def raw(conn, %{"id" => id}) do @@ -48,7 +61,7 @@ defmodule KetbinWeb.PageController do 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)) + |> Map.put("belongs_to", current_user && current_user.id) # attempt to create a paste case Pastes.create_paste(paste_params) do @@ -65,4 +78,24 @@ defmodule KetbinWeb.PageController do render(conn, "index.html", changeset: changeset) end end + + def edit(conn, %{"id" => id}) do + paste = Pastes.get_paste!(id) + changeset = Pastes.change_paste(paste) + render(conn, "edit.html", paste: paste, changeset: changeset) + end + + def update(conn, %{"id" => id, "paste" => paste_params}) do + paste = Pastes.get_paste!(id) + + case Pastes.update_paste(paste, paste_params) do + {:ok, paste} -> + conn + |> put_flash(:info, "Paste updated successfully.") + |> redirect(to: Routes.page_path(conn, :show, paste)) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "edit.html", paste: paste, changeset: changeset) + end + end end diff --git a/lib/ketbin_web/controllers/user_auth.ex b/lib/ketbin_web/controllers/user_auth.ex index 7330688..c29acc3 100644 --- a/lib/ketbin_web/controllers/user_auth.ex +++ b/lib/ketbin_web/controllers/user_auth.ex @@ -1,4 +1,5 @@ defmodule KetbinWeb.UserAuth do + require Logger import Plug.Conn import Phoenix.Controller @@ -94,6 +95,11 @@ defmodule KetbinWeb.UserAuth do assign(conn, :current_user, user) end + def owns_paste(%{assigns: %{current_user: user}} = conn, _params) do + Logger.info("USER: #{inspect(user)}") + conn + end + defp ensure_user_token(conn) do if user_token = get_session(conn, :user_token) do {user_token, conn} diff --git a/lib/ketbin_web/router.ex b/lib/ketbin_web/router.ex index c61cd0f..e583d16 100644 --- a/lib/ketbin_web/router.ex +++ b/lib/ketbin_web/router.ex @@ -17,13 +17,16 @@ defmodule KetbinWeb.Router do end scope "/", KetbinWeb do - pipe_through :browser + pipe_through [:browser, :owns_paste] get "/", PageController, :index get "/:id", PageController, :show get "/:id/raw", PageController, :raw get "/v/:id", PageController, :showlink + get "/edit/:id", PageController, :edit post "/", PageController, :create + patch "/:id", PageController, :update + put "/:id", PageController, :update end # Other scopes may use custom stacks. diff --git a/lib/ketbin_web/templates/page/edit.html.leex b/lib/ketbin_web/templates/page/edit.html.leex new file mode 100644 index 0000000..ed47b51 --- /dev/null +++ b/lib/ketbin_web/templates/page/edit.html.leex @@ -0,0 +1 @@ +<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :update, @paste)) %> diff --git a/lib/ketbin_web/templates/page/form.html.eex b/lib/ketbin_web/templates/page/form.html.eex index 56e71f0..9a696ec 100644 --- a/lib/ketbin_web/templates/page/form.html.eex +++ b/lib/ketbin_web/templates/page/form.html.eex @@ -7,15 +7,13 @@
<%= textarea f, :content, [class: "w-full h-full px-6 py-4 outline-none bg-light-grey font-bold resize-none", placeholder: "> Paste, save, share! (Pasting just a URL will shorten it!)"] %> -
+
diff --git a/lib/ketbin_web/templates/page/show.html.eex b/lib/ketbin_web/templates/page/show.html.eex index 91fdc79..3c6d45c 100644 --- a/lib/ketbin_web/templates/page/show.html.eex +++ b/lib/ketbin_web/templates/page/show.html.eex @@ -1,2 +1,12 @@ - +
+
+ <%= if @show_edit do%> + + + + + + <% end %> +
<%= @paste.content %> +