chiya/lib/chiya_web/controllers/note_controller.ex

218 lines
5.6 KiB
Elixir
Raw Normal View History

2023-03-05 17:23:16 +01:00
defmodule ChiyaWeb.NoteController do
use ChiyaWeb, :controller
import Plug.Conn, only: [assign: 3]
2023-03-05 17:23:16 +01:00
alias Chiya.Notes
2023-04-07 11:37:55 +02:00
alias Chiya.Notes.{Note, NoteImport}
2023-03-05 17:23:16 +01:00
def new(conn, _params) do
default_channels = get_default_channels(conn)
2023-06-20 20:28:45 +02:00
changeset =
%Note{}
|> Notes.preload_note()
2023-06-20 20:28:45 +02:00
|> Notes.change_note()
2023-04-10 19:18:27 +02:00
render(conn, :new,
changeset: changeset,
channels: to_channel_options(),
selected_channels: default_channels,
2023-07-04 07:13:49 +02:00
tags: [],
page_title: "New Note"
2023-04-10 19:18:27 +02:00
)
2023-03-05 17:23:16 +01:00
end
def create(conn, %{"note" => note_params}) do
note_params = from_channel_ids(note_params)
2023-03-05 17:23:16 +01:00
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note created successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
2023-03-05 17:23:16 +01:00
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new,
changeset: changeset,
channels: to_channel_options(),
selected_channels: nil,
2023-07-04 07:13:49 +02:00
tags: [],
page_title: "New Note"
)
2023-03-05 17:23:16 +01:00
end
end
def edit(conn, %{"id" => id}) do
note = Notes.get_note_preloaded!(id)
2023-03-05 17:23:16 +01:00
changeset = Notes.change_note(note)
selected_channels = Enum.map(note.channels, fn c -> c.id end)
2023-04-10 19:18:27 +02:00
render(conn, :edit,
note: note,
changeset: changeset,
channels: to_channel_options(),
selected_channels: selected_channels,
2023-07-04 07:13:49 +02:00
tags: note.tags,
page_title: "EDIT #{note.name}"
2023-04-10 19:18:27 +02:00
)
2023-03-05 17:23:16 +01:00
end
def update(conn, %{"id" => id, "note" => note_params}) do
note_params = from_channel_ids(note_params)
note = Notes.get_note_preloaded!(id)
2023-03-05 17:23:16 +01:00
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note updated successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
2023-03-05 17:23:16 +01:00
{:error, %Ecto.Changeset{} = changeset} ->
2023-04-10 19:22:28 +02:00
render(conn, :edit,
note: note,
changeset: changeset,
channels: to_channel_options(),
selected_channels: nil,
2023-07-04 07:13:49 +02:00
tags: note.tags,
page_title: "EDIT #{note.name}"
2023-04-10 19:22:28 +02:00
)
2023-03-05 17:23:16 +01:00
end
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
{:ok, _note} = Notes.delete_note(note)
conn
|> put_flash(:info, "Note deleted successfully.")
|> redirect(to: ~p"/admin/notes")
2023-03-05 17:23:16 +01:00
end
2023-03-31 22:20:32 +02:00
def raw(conn, %{"id" => id}) do
raw_note =
id
|> Notes.get_note!()
|> Notes.preload_note()
|> ChiyaWeb.Exim.export_note()
conn
|> text(raw_note)
end
2023-05-21 09:53:17 +02:00
def publish(conn, %{"id" => id}) do
note = Notes.get_note_preloaded!(id)
2023-06-19 22:07:37 +02:00
case Notes.publish_note(note, NaiveDateTime.local_now()) do
2023-05-21 09:53:17 +02:00
{:ok, note} ->
conn
|> put_flash(:info, "Note published successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
end
end
def unpublish(conn, %{"id" => id}) do
note = Notes.get_note_preloaded!(id)
2023-06-19 22:07:37 +02:00
case Notes.publish_note(note, nil) do
2023-05-21 09:53:17 +02:00
{:ok, note} ->
conn
|> put_flash(:info, "Note un-published successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
end
end
def edit_image(conn, %{"image_id" => id}) do
image = Notes.get_note_image!(id)
changeset = Notes.change_note_image(image)
render(conn, :edit_image, image: image, changeset: changeset)
end
def update_image(conn, %{"image_id" => id, "note_image" => image_params}) do
image = Notes.get_note_image!(id)
case Notes.update_note_image(image, image_params) do
{:ok, image} ->
conn
|> put_flash(:info, "Image updated successfully.")
|> redirect(to: ~p"/admin/notes/#{image.note_id}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit_image, image: image, changeset: changeset)
end
end
2023-04-07 11:37:55 +02:00
def import_prepare(conn, _params) do
2023-07-04 07:13:49 +02:00
render(conn, :import,
changeset: NoteImport.change_note_import(%{}),
page_title: "Import Note"
)
2023-04-07 11:37:55 +02:00
end
def import_run(conn, %{
"note_import" => %{
"file" => %{
path: path,
content_type: "text/markdown",
filename: filename
}
}
}) do
case File.read(path) do
{:ok, content} ->
note_params = %{
name: filename,
content: content
}
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note created successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset, channels: to_channel_options())
end
_ ->
2023-07-04 07:13:49 +02:00
render(conn, :import,
changeset: NoteImport.change_note_import(%{}),
page_title: "Import Note"
)
2023-04-07 11:37:55 +02:00
end
end
def import_run(conn, _params) do
conn
|> put_flash(:error, "Error while importing.")
2023-07-04 07:13:49 +02:00
|> render(:import,
changeset: NoteImport.change_note_import(%{}),
page_title: "Import Note"
)
2023-04-07 11:37:55 +02:00
end
2023-09-13 23:09:19 +02:00
defp get_default_channels(%Plug.Conn{} = conn) do
if conn.assigns.settings.default_channel do
[conn.assigns.settings.default_channel.id]
else
[]
end
end
defp from_channel_ids(note_params) do
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)
selected_channels =
Chiya.Channels.list_channels()
|> Enum.filter(fn c -> Enum.member?(selected_ids, c.id) end)
Map.put(note_params, "channels", selected_channels)
end
defp to_channel_options(items \\ nil),
do:
Enum.map(items || Chiya.Channels.list_channels(), fn c ->
{Chiya.Channels.Channel.icon(c) <> " " <> c.name, c.id}
end)
2023-03-05 17:23:16 +01:00
end