2022-04-06 14:55:12 -04:00
defmodule AggieditWeb.PostLive.Show do
use AggieditWeb , :live_view
alias Aggiedit.Rooms
2022-04-13 14:42:01 -04:00
alias Aggiedit.Roles
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
assign_socket_room_and_user_or_error ( params , session , socket )
2022-04-06 14:55:12 -04:00
end
@impl true
2022-04-13 18:55:02 -04:00
def handle_params ( %{ " id " = > id } , _ , socket ) do
2022-04-13 14:42:01 -04:00
post = Rooms . get_post! ( id )
|> Repo . preload ( :upload )
if Roles . guard? ( socket . assigns . current_user , socket . assigns . live_action , post ) do
{ :noreply ,
socket
|> assign ( :page_title , page_title ( socket . assigns . live_action ) )
|> assign ( :post , post ) }
else
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :error , " You don't have permission to do that. " ) |> redirect ( to : Routes . post_show_path ( socket , :show , socket . assigns . room , post ) ) }
2022-04-13 14:42:01 -04:00
end
2022-04-06 14:55:12 -04:00
end
defp page_title ( :show ) , do : " Show Post "
defp page_title ( :edit ) , do : " Edit Post "
end