From ebd9f7f52c54c0a8487002e5bc8c73fce8fe63fb Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Thu, 9 May 2024 19:29:13 +0530 Subject: [PATCH] feat(pastes): add new page to show owned pastes Signed-off-by: SphericalKat --- lib/ketbin/pastes.ex | 4 ++++ lib/ketbin_web/controllers/page_controller.ex | 5 +++++ lib/ketbin_web/router.ex | 7 +++++++ lib/ketbin_web/templates/layout/_user_menu.html.heex | 1 + lib/ketbin_web/templates/page/pastes.html.heex | 11 +++++++++++ 5 files changed, 28 insertions(+) create mode 100644 lib/ketbin_web/templates/page/pastes.html.heex diff --git a/lib/ketbin/pastes.ex b/lib/ketbin/pastes.ex index 27f738b..d717f46 100644 --- a/lib/ketbin/pastes.ex +++ b/lib/ketbin/pastes.ex @@ -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. diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index 315cce6..98b90a5 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -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 diff --git a/lib/ketbin_web/router.ex b/lib/ketbin_web/router.ex index 7ed284d..5a74a75 100644 --- a/lib/ketbin_web/router.ex +++ b/lib/ketbin_web/router.ex @@ -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 diff --git a/lib/ketbin_web/templates/layout/_user_menu.html.heex b/lib/ketbin_web/templates/layout/_user_menu.html.heex index c22cc62..ed13bf6 100644 --- a/lib/ketbin_web/templates/layout/_user_menu.html.heex +++ b/lib/ketbin_web/templates/layout/_user_menu.html.heex @@ -20,6 +20,7 @@ function toggleDropdown() { <% else %> diff --git a/lib/ketbin_web/templates/page/pastes.html.heex b/lib/ketbin_web/templates/page/pastes.html.heex new file mode 100644 index 0000000..808f5e5 --- /dev/null +++ b/lib/ketbin_web/templates/page/pastes.html.heex @@ -0,0 +1,11 @@ +
+ +