My space on the web https://inhji.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 lines
2.1 KiB

defmodule MirageWeb.NoteController do
use MirageWeb, :controller
alias Mirage.Notes
alias Mirage.Notes.Note
plug :require_authenticated_user when action in [:new, :edit, :update, :delete]
def index(conn, _params) do
notes = Notes.list_notes()
render(conn, "index.html", notes: notes)
end
def new(conn, _params) do
changeset = Notes.change_note(%Note{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"note" => note_params}) do
case Notes.create_note(note_params) do
{:ok, note} ->
note
|> Notes.preload_note()
|> Notes.Tags.update_tags(note_params["topic_string"])
|> Notes.link_note()
conn
|> put_flash(:info, "Note created successfully.")
|> redirect(to: Routes.note_path(conn, :show, note))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
render(conn, "show.html", note: note)
end
def edit(conn, %{"id" => id}) do
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
changeset = Notes.change_note(note)
render(conn, "edit.html", note: note, changeset: changeset)
end
def update(conn, %{"id" => id, "note" => note_params}) do
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
case Notes.update_note(note, note_params) do
{:ok, note} ->
note
|> Notes.Tags.update_tags(note_params["topic_string"])
|> Notes.link_note()
conn
|> put_flash(:info, "Note updated successfully.")
|> redirect(to: Routes.note_path(conn, :show, note))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", note: note, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
{:ok, _note} = Notes.delete_note(note)
conn
|> put_flash(:info, "Note deleted successfully.")
|> redirect(to: Routes.note_path(conn, :index))
end
end