2022-04-06 14:55:12 -04:00
defmodule AggieditWeb.PostLive.Index do
use AggieditWeb , :live_view
2022-04-13 14:42:01 -04:00
alias Aggiedit.Roles
2022-04-06 14:55:12 -04:00
alias Aggiedit.Rooms
2022-04-13 18:55:02 -04:00
alias Aggiedit.Rooms.Post
2022-04-06 20:17:27 -04:00
alias Aggiedit.Repo
2022-04-06 14:55:12 -04:00
@impl true
2022-04-13 18:55:02 -04:00
def mount ( %{ " room_id " = > _room_id } = params , session , socket ) do
{ :ok , socket } = assign_socket_room_and_user_or_error ( params , session , socket )
2022-04-06 16:41:19 -04:00
case socket . assigns do
2022-04-13 18:55:02 -04:00
%{ :room = > room } -> { :ok , assign ( socket , %{ :posts = > room |> Repo . preload ( :posts ) |> Map . get ( :posts ) } ) }
2022-04-13 16:17:28 -04:00
_ -> { :ok , socket }
2022-04-06 16:41:19 -04:00
end
2022-04-06 14:55:12 -04:00
end
2022-04-13 14:42:01 -04:00
@impl true
def handle_params ( %{ " id " = > id } = params , _url , socket ) do
2022-04-13 16:17:28 -04:00
post = Rooms . get_post! ( id )
if Roles . guard? ( socket . assigns . current_user , socket . assigns . live_action , post ) do
{ :noreply , apply_action ( socket , socket . assigns . live_action , params ) }
2022-04-13 14:42:01 -04:00
else
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :error , " You do not have permission to edit this post. " ) |> redirect ( to : Routes . post_index_path ( socket , :index , socket . assigns . room ) ) }
2022-04-13 14:42:01 -04:00
end
end
2022-04-06 14:55:12 -04:00
@impl true
def handle_params ( params , _url , socket ) do
2022-04-13 14:42:01 -04:00
IO . puts ( inspect ( params ) )
2022-04-06 14:55:12 -04:00
{ :noreply , apply_action ( socket , socket . assigns . live_action , params ) }
end
2022-04-13 18:55:02 -04:00
defp apply_action ( socket , :edit , %{ " id " = > id } ) do
2022-04-06 14:55:12 -04:00
socket
|> assign ( :page_title , " Edit Post " )
2022-04-08 15:01:24 -04:00
|> assign ( :post , Rooms . get_post! ( id ) |> Repo . preload ( :upload ) )
2022-04-06 14:55:12 -04:00
end
defp apply_action ( socket , :new , _params ) do
socket
|> assign ( :page_title , " New Post " )
2022-04-07 17:52:32 -04:00
|> assign ( :post , % Post { } )
2022-04-06 14:55:12 -04:00
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 )
2022-04-13 14:42:01 -04:00
if Roles . guard? ( socket . assigns . current_user , :delete , post ) do
Rooms . delete_post ( post )
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :success , " Post deleted. " ) |> redirect ( to : Routes . post_index_path ( socket , :index , socket . assigns . room ) ) }
2022-04-13 14:42:01 -04:00
else
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :error , " You do not have permission to delete this post. " ) |> redirect ( to : Routes . post_index_path ( socket , :index , socket . assigns . room ) ) }
2022-04-13 14:42:01 -04:00
end
2022-04-06 14:55:12 -04:00
end
end