list and show comments in admin

This commit is contained in:
Inhji 2023-05-09 16:18:10 +02:00
parent 13a6eddf1b
commit 18b8a49695
6 changed files with 73 additions and 2 deletions

View file

@ -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)

View file

@ -1,6 +1,12 @@
<header class="px-4 sm:px-6 lg:px-8">
<div class="flex items-center justify-end border-b border-gray-300 dark:border-gray-800 py-3">
<div class="flex items-center gap-4">
<.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>
<.link
href={~p"/admin/notes"}
class="text-xs font-semibold leading-6 text-gray-900 hover:text-gray-700 dark:text-gray-200"

View file

@ -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")

View file

@ -0,0 +1,25 @@
<.header>
<.icon name="hero-document-text" /> Comments
<:subtitle>Comments are attached to notes</:subtitle>
</.header>
<.table id="comments" rows={@comments} row_click={&JS.navigate(~p"/admin/comments/#{&1}")}>
<:col :let={comment} label="Author"><%= comment.author_name %></:col>
<:col :let={comment} label="Inserted at"><%= from_now(comment.inserted_at) %></:col>
<:col :let={comment} label="Approved at"><%= from_now(comment.approved_at) %></:col>
<:action :let={comment}>
<div class="sr-only">
<.link navigate={~p"/admin/comments/#{comment}"}>Show</.link>
</div>
</:action>
<:action :let={comment}>
<.link href={~p"/admin/comments/#{comment}"} method="delete" data-confirm="Are you sure?">
Delete
</.link>
</:action>
<:action :let={comment}>
<div class="sr-only">
<.link navigate={~p"/admin/comments/#{comment}/approve"}>Approve</.link>
</div>
</:action>
</.table>

View file

@ -0,0 +1,20 @@
<.header>
Comment <%= @comment.id %>
<:subtitle>This is a comment record from your database.</:subtitle>
<:actions>
<.link href={~p"/admin/notes/#{@comment}/approve"}>
<.button>Approve note</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Name"><%= @comment.author_name %></:item>
<:item title="Content"><%= @comment.content %></:item>
<:item title="Inserted at"><%= @comment.inserted_at %></:item>
<:item title="Approved at"><%= @comment.approved_at %></:item>
<:item title="Kind"><%= @comment.kind %></:item>
<:item title="Note"><a href={"/admin/notes/#{@comment.note.id}"}><%= @comment.note.name %></a></:item>
</.list>
<.back navigate={~p"/admin/comments"}>Back to comments</.back>

View file

@ -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