Skip to content

Commit

Permalink
feat(pastes): add new page to show owned pastes
Browse files Browse the repository at this point in the history
Signed-off-by: SphericalKat <[email protected]>
  • Loading branch information
SphericalKat committed May 9, 2024
1 parent f0efa78 commit ebd9f7f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ketbin/pastes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ defmodule Ketbin.Pastes do
Repo.all(Paste)
end

def list_pastes_by_user(user_id) do
Repo.all(from(p in Paste, where: p.belongs_to == ^user_id))
end

@doc """
Gets a single paste.
Expand Down
5 changes: 5 additions & 0 deletions lib/ketbin_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ defmodule KetbinWeb.PageController do
render(conn, "edit.html", paste: paste, changeset: changeset)
end
end

def pastes(%{assigns: %{current_user: current_user}} = conn, _params) do
pastes = Pastes.list_pastes_by_user(current_user.id)
render(conn, "pastes.html", pastes: pastes)
end
end
7 changes: 7 additions & 0 deletions lib/ketbin_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ defmodule KetbinWeb.Router do
plug :fetch_current_user
end

# scope to ensure user is authenticated
scope "/", KetbinWeb do
pipe_through [:browser, :require_authenticated_user]

get "/pastes", PageController, :pastes
end

scope "/", KetbinWeb do
pipe_through :browser

Expand Down
1 change: 1 addition & 0 deletions lib/ketbin_web/templates/layout/_user_menu.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function toggleDropdown() {
<div id="dropdown" class="hidden right-0 z-50 bg-[#313131] px-4 py-2">
<li><%= link "Settings", to: Routes.user_settings_path(@conn, :edit) %></li>
<li><%= link "Log out", to: Routes.user_session_path(@conn, :delete), method: :delete %></li>
<li><%= link "My Pastes", to: Routes.page_path(@conn, :pastes) %></li>
</div>
</div>
<% else %>
Expand Down
11 changes: 11 additions & 0 deletions lib/ketbin_web/templates/page/pastes.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="flex relative flex-col w-full h-full">
<ul class="break-word py-4 h-full w-full overflow-y-auto">
<%= for paste <- @pastes do %>
<li class="flex flex-row items-center justify-between">
<a href={ Routes.page_path(@conn, :show, paste) } class="">
https://katb.in/v/<%= paste.id %>
</a>
</li>
<% end %>
</ul>
</div>

0 comments on commit ebd9f7f

Please sign in to comment.