summaryrefslogtreecommitdiff
path: root/lib/aggiedit_web/live/post_live/index.ex
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-06 12:55:12 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-06 12:55:12 -0600
commit4067339e8cf9dbd624a8fa0183d7a29c73b2e762 (patch)
tree4f995e706c10bf5b9a58c078cfb843c76e31c8a0 /lib/aggiedit_web/live/post_live/index.ex
parent66d871e5461814dad58872eb832a58f2c3c5111b (diff)
downloadaggiedit-4067339e8cf9dbd624a8fa0183d7a29c73b2e762.tar.gz
aggiedit-4067339e8cf9dbd624a8fa0183d7a29c73b2e762.zip
Models for upload and post; generated liveview controller for posts
Diffstat (limited to 'lib/aggiedit_web/live/post_live/index.ex')
-rw-r--r--lib/aggiedit_web/live/post_live/index.ex46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/aggiedit_web/live/post_live/index.ex b/lib/aggiedit_web/live/post_live/index.ex
new file mode 100644
index 0000000..0b12ed0
--- /dev/null
+++ b/lib/aggiedit_web/live/post_live/index.ex
@@ -0,0 +1,46 @@
+defmodule AggieditWeb.PostLive.Index do
+ use AggieditWeb, :live_view
+
+ alias Aggiedit.Rooms
+ alias Aggiedit.Rooms.Post
+
+ @impl true
+ def mount(_params, _session, socket) do
+ {:ok, assign(socket, :posts, list_posts())}
+ end
+
+ @impl true
+ def handle_params(params, _url, socket) do
+ {:noreply, apply_action(socket, socket.assigns.live_action, params)}
+ end
+
+ defp apply_action(socket, :edit, %{"id" => id}) do
+ socket
+ |> assign(:page_title, "Edit Post")
+ |> assign(:post, Rooms.get_post!(id))
+ end
+
+ defp apply_action(socket, :new, _params) do
+ socket
+ |> assign(:page_title, "New Post")
+ |> assign(:post, %Post{})
+ end
+
+ defp apply_action(socket, :index, _params) do
+ socket
+ |> assign(:page_title, "Listing Posts")
+ |> assign(:post, nil)
+ end
+
+ @impl true
+ def handle_event("delete", %{"id" => id}, socket) do
+ post = Rooms.get_post!(id)
+ {:ok, _} = Rooms.delete_post(post)
+
+ {:noreply, assign(socket, :posts, list_posts())}
+ end
+
+ defp list_posts do
+ Rooms.list_posts()
+ end
+end