comfycamp/lib/comfycamp_web/controllers/notes_editor_controller.ex

110 lines
2.8 KiB
Elixir
Raw Normal View History

2024-07-29 18:07:44 +05:00
defmodule ComfycampWeb.NotesEditorController do
use ComfycampWeb, :controller
alias Comfycamp.Notes
alias Comfycamp.Notes.Note
def index(conn, _params) do
notes = Notes.list_notes()
conn
|> put_layout(html: :admin)
2024-08-31 17:02:54 +05:00
|> render(:index, page_title: "Заметки", notes: notes)
2024-07-29 18:07:44 +05:00
end
def new(conn, _params) do
changeset = Notes.change_note(%Note{})
conn
|> put_layout(html: :admin)
2024-08-26 22:10:51 +05:00
|> render(:new,
page_title: "Новая заметка",
changeset: changeset,
stylesheets: ["/assets/admin.css"]
)
2024-07-29 18:07:44 +05:00
end
def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id)
changeset = Notes.change_note(note)
conn
|> put_layout(html: :admin)
2024-08-26 22:10:51 +05:00
|> render(:edit,
page_title: "Редактировать заметку",
changeset: changeset,
stylesheets: ["/assets/admin.css"]
)
2024-07-29 18:07:44 +05:00
end
def create(conn, %{"note" => note_params}) do
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Заметка сохранена.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при обновлении заметки.")
|> put_layout(html: :admin)
2024-08-26 22:10:51 +05:00
|> render(:new,
page_title: "Создать заметку",
changeset: changeset,
stylesheets: ["/assets/admin.css"]
)
2024-07-29 18:07:44 +05:00
end
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Заметка обновлена.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при обновлении заметки.")
|> put_layout(html: :admin)
2024-08-26 22:10:51 +05:00
|> render(:edit,
page_title: "Редактировать заметку",
changeset: changeset,
stylesheets: ["/assets/admin.css"]
)
2024-07-29 18:07:44 +05:00
end
end
def show(conn, %{"id" => id}) do
note = Notes.get_note!(id)
conn
|> put_layout(html: :admin)
2024-08-31 17:02:54 +05:00
|> render(:show, page_title: "Заметка", note: note)
2024-07-29 18:07:44 +05:00
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
case Notes.delete_note(note) do
{:ok, _note} ->
conn
|> put_flash(:info, "Заметка удалена.")
|> redirect(to: ~p"/admin/notes")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при удалении заметки.")
|> put_layout(html: :admin)
2024-08-26 22:10:51 +05:00
|> render(:edit,
page_title: "Редактировать заметку",
changeset: changeset,
stylesheets: ["/assets/admin.css"]
)
2024-07-29 18:07:44 +05:00
end
end
end