feat(pastes): add new page to show owned pastes

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2024-05-09 19:29:13 +05:30
parent f0efa78b89
commit ebd9f7f52c
No known key found for this signature in database
GPG Key ID: A61360EFA2AEE871
5 changed files with 28 additions and 0 deletions

View File

@ -21,6 +21,10 @@ defmodule Ketbin.Pastes do
Repo.all(Paste) Repo.all(Paste)
end end
def list_pastes_by_user(user_id) do
Repo.all(from(p in Paste, where: p.belongs_to == ^user_id))
end
@doc """ @doc """
Gets a single paste. Gets a single paste.

View File

@ -134,4 +134,9 @@ defmodule KetbinWeb.PageController do
render(conn, "edit.html", paste: paste, changeset: changeset) render(conn, "edit.html", paste: paste, changeset: changeset)
end end
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 end

View File

@ -18,6 +18,13 @@ defmodule KetbinWeb.Router do
plug :fetch_current_user plug :fetch_current_user
end end
# scope to ensure user is authenticated
scope "/", KetbinWeb do
pipe_through [:browser, :require_authenticated_user]
get "/pastes", PageController, :pastes
end
scope "/", KetbinWeb do scope "/", KetbinWeb do
pipe_through :browser pipe_through :browser

View File

@ -20,6 +20,7 @@ function toggleDropdown() {
<div id="dropdown" class="hidden right-0 z-50 bg-[#313131] px-4 py-2"> <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 "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 "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>
</div> </div>
<% else %> <% else %>

View File

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