add publish unpublish actions

This commit is contained in:
Inhji 2023-05-21 09:53:17 +02:00
parent cf8677cc95
commit d6fb14bb38
3 changed files with 51 additions and 0 deletions

View file

@ -104,6 +104,46 @@ defmodule ChiyaWeb.NoteController do
|> text(raw_note)
end
def publish(conn, %{"id" => id}) do
note_params = %{published_at: NaiveDateTime.local_now()}
note = Notes.get_note_preloaded!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note published successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit,
note: note,
changeset: changeset,
channels: to_channel_options(),
tags: note.tags
)
end
end
def unpublish(conn, %{"id" => id}) do
note_params = %{published_at: nil}
note = Notes.get_note_preloaded!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note un-published successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit,
note: note,
changeset: changeset,
channels: to_channel_options(),
tags: note.tags
)
end
end
def edit_image(conn, %{"image_id" => id}) do
image = Notes.get_note_image!(id)
changeset = Notes.change_note_image(image)

View file

@ -25,6 +25,15 @@ defmodule ChiyaWeb.NoteShowLive do
<.link href={~p"/admin/notes/#{@note}/raw"}>
<.button>Raw</.button>
</.link>
<%= if is_nil(@note.published_at) do %>
<.link href={~p"/admin/notes/#{@note}/publish"}>
<.button>Publish</.button>
</.link>
<% else %>
<.link href={~p"/admin/notes/#{@note}/unpublish"}>
<.button>Un-Publish</.button>
</.link>
<% end %>
</:actions>
</.header>

View file

@ -67,6 +67,8 @@ defmodule ChiyaWeb.Router do
live "/notes/:id", NoteShowLive, :show
get "/notes/:id/raw", NoteController, :raw
get "/notes/:id/publish", NoteController, :publish
get "/notes/:id/unpublish", NoteController, :unpublish
get "/notes/:id/image/:image_id", NoteController, :edit_image
put "/notes/:id/image/:image_id", NoteController, :update_image