diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index e9fd096..f066cf1 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -278,6 +278,17 @@ defmodule Chiya.Notes do Repo.delete(note_tag) end + def list_note_comments() do + NoteComment + |> order_by(:inserted_at) + |> Repo.all() + |> Repo.preload(:note) + end + + def get_note_comment!(id) do + Repo.get!(NoteComment, id) |> Repo.preload(:note) + end + def create_note_comment(attrs \\ %{}) do %NoteComment{} |> NoteComment.changeset(attrs) diff --git a/lib/chiya_web/components/layouts/app.html.heex b/lib/chiya_web/components/layouts/app.html.heex index 7c67fd8..10b4e61 100644 --- a/lib/chiya_web/components/layouts/app.html.heex +++ b/lib/chiya_web/components/layouts/app.html.heex @@ -1,6 +1,12 @@
+ <.link + href={~p"/admin/comments"} + class="text-xs font-semibold leading-6 text-gray-900 hover:text-gray-700 dark:text-gray-200" + > + <.icon name="hero-document-text" class="w-4 h-4" /> Comments + <.link href={~p"/admin/notes"} class="text-xs font-semibold leading-6 text-gray-900 hover:text-gray-700 dark:text-gray-200" diff --git a/lib/chiya_web/controllers/comment_controller.ex b/lib/chiya_web/controllers/comment_controller.ex index 5559a96..0fbbb8b 100644 --- a/lib/chiya_web/controllers/comment_controller.ex +++ b/lib/chiya_web/controllers/comment_controller.ex @@ -1,11 +1,19 @@ defmodule ChiyaWeb.CommentController do use ChiyaWeb, :controller + def index(conn, _params) do + comments = Chiya.Notes.list_note_comments() + render(conn, comments: comments) + end + + def show(conn, %{"id" => comment_id}) do + comment = Chiya.Notes.get_note_comment!(comment_id) + render(conn, comment: comment) + end + def create(conn, %{"slug" => note_slug, "note_comment" => comment_params}) do note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug) - IO.inspect(comment_params) - case Chiya.Notes.create_note_comment(comment_params) do {:ok, _comment} -> redirect(conn, to: ~p"/#{note_slug}?error=0") diff --git a/lib/chiya_web/controllers/comment_html/index.html.heex b/lib/chiya_web/controllers/comment_html/index.html.heex new file mode 100644 index 0000000..01f9447 --- /dev/null +++ b/lib/chiya_web/controllers/comment_html/index.html.heex @@ -0,0 +1,25 @@ +<.header> + <.icon name="hero-document-text" /> Comments + <:subtitle>Comments are attached to notes + + +<.table id="comments" rows={@comments} row_click={&JS.navigate(~p"/admin/comments/#{&1}")}> + <:col :let={comment} label="Author"><%= comment.author_name %> + <:col :let={comment} label="Inserted at"><%= from_now(comment.inserted_at) %> + <:col :let={comment} label="Approved at"><%= from_now(comment.approved_at) %> + <:action :let={comment}> +
+ <.link navigate={~p"/admin/comments/#{comment}"}>Show +
+ + <:action :let={comment}> + <.link href={~p"/admin/comments/#{comment}"} method="delete" data-confirm="Are you sure?"> + Delete + + + <:action :let={comment}> +
+ <.link navigate={~p"/admin/comments/#{comment}/approve"}>Approve +
+ + diff --git a/lib/chiya_web/controllers/comment_html/show.html.heex b/lib/chiya_web/controllers/comment_html/show.html.heex new file mode 100644 index 0000000..bf98b2b --- /dev/null +++ b/lib/chiya_web/controllers/comment_html/show.html.heex @@ -0,0 +1,20 @@ +<.header> + Comment <%= @comment.id %> + <:subtitle>This is a comment record from your database. + <:actions> + <.link href={~p"/admin/notes/#{@comment}/approve"}> + <.button>Approve note + + + + +<.list> + <:item title="Name"><%= @comment.author_name %> + <:item title="Content"><%= @comment.content %> + <:item title="Inserted at"><%= @comment.inserted_at %> + <:item title="Approved at"><%= @comment.approved_at %> + <:item title="Kind"><%= @comment.kind %> + <:item title="Note"><%= @comment.note.name %> + + +<.back navigate={~p"/admin/comments"}>Back to comments diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index 3af5fff..db4038c 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -60,6 +60,7 @@ defmodule ChiyaWeb.Router do resources "/notes", NoteController, except: [:show] resources "/settings", SettingController, singleton: true resources "/identities", IdentityController + resources "/comments", CommentController, only: [:index, :show] get "/notes/import", NoteController, :import_prepare post "/notes/import", NoteController, :import_run