diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index b53c2f1..84d168d 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -138,7 +138,7 @@ defmodule Chiya.Notes do @doc """ Gets a single note image. """ - def get_note_image!(id), do: Repo.get!(NoteImage, id) + def get_note_image!(id), do: Repo.get!(NoteImage, id) |> Repo.preload(:note) @doc """ Creates a note image and attaches it to a note. diff --git a/lib/chiya/notes/note_image.ex b/lib/chiya/notes/note_image.ex index 0ca7f30..0c30629 100644 --- a/lib/chiya/notes/note_image.ex +++ b/lib/chiya/notes/note_image.ex @@ -6,9 +6,10 @@ defmodule Chiya.Notes.NoteImage do schema "note_images" do field :content, :string, default: "" field :path, ChiyaWeb.Uploaders.NoteImage.Type - field :note_id, :id field :featured, :boolean, default: false + belongs_to :note, Chiya.Notes.Note + timestamps() end diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index 8ad0f20..f4059ce 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -75,6 +75,26 @@ defmodule ChiyaWeb.NoteController do |> text(raw_note) 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 + defp from_channel_ids(note_params) do selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1) diff --git a/lib/chiya_web/controllers/note_html/edit_image.html.heex b/lib/chiya_web/controllers/note_html/edit_image.html.heex new file mode 100644 index 0000000..c8461a5 --- /dev/null +++ b/lib/chiya_web/controllers/note_html/edit_image.html.heex @@ -0,0 +1,21 @@ +<.header> + Edit Image <%= @image.id %> + <:subtitle>Use this form to manage note image records in your database. + + +<.simple_form :let={f} for={@changeset} action={~p"/admin/notes/#{@image.note.id}/image/#{@image.id}"}> + <.error :if={@changeset.action}> + Oops, something went wrong! Please check the errors below. + + + <.input field={f[:id]} type="hidden" value={@image.id} /> + <.input field={f[:content]} type="textarea" label="Content" /> + <.input field={f[:featured]} type="checkbox" label="Featured" /> + + <:actions> + <.button type="submit">Save + + + + +<.back navigate={~p"/admin/notes/#{@image.note_id}"}>Back to notes \ No newline at end of file diff --git a/lib/chiya_web/controllers/page_html/note.html.heex b/lib/chiya_web/controllers/page_html/note.html.heex index bfd0774..2493119 100644 --- a/lib/chiya_web/controllers/page_html/note.html.heex +++ b/lib/chiya_web/controllers/page_html/note.html.heex @@ -17,7 +17,7 @@