90 lines
2.5 KiB
Elixir
90 lines
2.5 KiB
Elixir
|
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)
|
||
|
|> render(:index, page_title: "Заметки", notes: notes)
|
||
|
end
|
||
|
|
||
|
def new(conn, _params) do
|
||
|
changeset = Notes.change_note(%Note{})
|
||
|
|
||
|
conn
|
||
|
|> put_layout(html: :admin)
|
||
|
|> render(:new, page_title: "Новая заметка", changeset: changeset)
|
||
|
end
|
||
|
|
||
|
def edit(conn, %{"id" => id}) do
|
||
|
note = Notes.get_note!(id)
|
||
|
|
||
|
changeset = Notes.change_note(note)
|
||
|
|
||
|
conn
|
||
|
|> put_layout(html: :admin)
|
||
|
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
|
||
|
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)
|
||
|
|> render(:new, page_title: "Создать заметку", changeset: changeset)
|
||
|
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)
|
||
|
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def show(conn, %{"id" => id}) do
|
||
|
note = Notes.get_note!(id)
|
||
|
|
||
|
conn
|
||
|
|> put_layout(html: :admin)
|
||
|
|> render(:show, page_title: "Заметка", note: note)
|
||
|
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)
|
||
|
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
|
||
|
end
|
||
|
end
|
||
|
end
|