This commit is contained in:
Inhji 2023-09-10 22:55:16 +02:00
parent 949bffcf48
commit 4905e38615
12 changed files with 161 additions and 0 deletions

View file

@ -57,6 +57,12 @@ defmodule Chiya.Notes do
Chiya.Flop.validate_and_run(q, params, for: Chiya.Notes.Note)
end
def list_apply_notes(regex) do
Note
|> where([n], fragment("? ~ ?", n.name, ^regex))
|> Repo.all()
end
def list_notes_by_channel(%Chiya.Channels.Channel{} = channel) do
list_notes_by_channel_query(channel)
|> order_by([n], desc: n.updated_at, desc: n.published_at)

View file

@ -38,6 +38,16 @@ defmodule Chiya.Tags do
|> Repo.all()
end
def list_admin_tags(params) do
q =
Tag
|> with_preloads()
Repo.all(q)
end
def get_tag!(id), do: Repo.get!(Tag, id) |> preload_tag()
@doc """
Gets a single tag.

View file

@ -6,6 +6,10 @@ defmodule Chiya.Tags.Tag do
import Ecto.Changeset
alias Chiya.Tags.TagSlug
@derive {
Flop.Schema,
filterable: [], sortable: [], default_limit: 100
}
@derive {Jason.Encoder, only: [:name]}
schema "tags" do
field :name, :string

View file

@ -11,6 +11,11 @@
icon: "hero-speaker-wave-solid",
name: "Channels"
},
%{
path: ~p"/admin/tags",
icon: "hero-document-text-solid",
name: "Tags"
},
%{
path: ~p"/admin/identities",
icon: "hero-user-solid",

View file

@ -0,0 +1,54 @@
defmodule ChiyaWeb.TagController do
use ChiyaWeb, :controller
alias Chiya.Tags
def index(conn, params) do
tags = Chiya.Tags.list_admin_tags(params)
render(conn, :index, tags: tags)
end
def show(conn, %{"id" => id}) do
tag = Tags.get_tag!(id)
render(conn, :show, tag: tag)
end
def apply(conn, %{"id" => id}) do
tag = Tags.get_tag!(id)
notes = Chiya.Notes.list_apply_notes(tag.regex)
render(conn, :apply_prepare, tag: tag, notes: notes)
end
def edit(conn, %{"id" => id}) do
IO.inspect(id)
tag = Tags.get_tag!(id)
changeset = Tags.change_tag(tag)
render(conn, :edit,
tag: tag,
changeset: changeset,
page_title: "EDIT #{tag.name}"
)
end
def update(conn, %{"id" => id, "tag" => tag_params}) do
tag = Tags.get_tag!(id)
case Tags.update_tag(tag, tag_params) do
{:ok, _tag} ->
conn
|> put_flash(:info, "Tags updated successfully.")
|> redirect(to: ~p"/admin/tags")
# TODO: set channels from changeset when error happened?
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit,
tag: tag,
changeset: changeset,
page_title: "EDIT #{tag.name}"
)
end
end
end

View file

@ -0,0 +1,5 @@
defmodule ChiyaWeb.TagHTML do
use ChiyaWeb, :html
embed_templates "tag_html/*"
end

View file

@ -0,0 +1,9 @@
<.header>
Apply Tag <%= @tag.id %>
<:subtitle>This is a tag record from your database.</:subtitle>
<:actions>
<.link href={~p"/admin/tags/#{@tag}/apply"}>
<.button>Apply tag</.button>
</.link>
</:actions>
</.header>

View file

@ -0,0 +1,10 @@
<.header>
Edit Tag “<%= @tag.name %>”
<:actions>
<.link href={~p"/admin/tags"}>
<.button><.icon name="hero-arrow-left" /> Back to Tags</.button>
</.link>
</:actions>
</.header>
<.tag_form changeset={@changeset} action={~p"/admin/tags/#{@tag}"} />

View file

@ -0,0 +1,20 @@
<.header>
<.icon name="hero-document-text" /> Tags
<:subtitle>Tags are tags.</:subtitle>
</.header>
<section class="flex flex-col gap-3 mt-6">
<%= for tag <- @tags do %>
<article class="bg-slate-100 dark:bg-slate-800 text-slate-900 dark:text-slate-100 p-3 rounded">
<header>
<h2 class="text-xl leading-normal">
<a href={"/admin/tags/#{tag.id}"}><%= tag.name %></a>
</h2>
</header>
<footer class="flex gap-3 text-sm ">
<span><%= Enum.count(tag.notes) %> notes</span>
<span>Updated <%= from_now(tag.updated_at) %></span>
</footer>
</article>
<% end %>
</section>

View file

@ -0,0 +1,21 @@
<.header>
Tag <%= @tag.id %>
<:subtitle>This is a tag record from your database.</:subtitle>
<:actions>
<.link href={~p"/admin/tags/#{@tag}/edit"}>
<.button>Edit tag</.button>
</.link>
<.link href={~p"/admin/tags/#{@tag}/apply"}>
<.button>Apply tag</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Name"><%= @tag.name %></:item>
<:item title="Content"><%= @tag.content %></:item>
<:item title="Slug"><%= @tag.slug %></:item>
<:item title="Regex"><%= @tag.regex %></:item>
</.list>
<.back navigate={~p"/admin/notes"}>Back to notes</.back>

View file

@ -0,0 +1,14 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:name]} type="text" />
<.input field={f[:content]} type="textarea" label="Content" rows="5" class="font-mono" />
<.input field={f[:regex]} type="text" label="Regex" />
<.input field={f[:slug]} type="text" label="Slug" />
<:actions>
<.button>Save Tag</.button>
</:actions>
</.simple_form>

View file

@ -75,6 +75,9 @@ defmodule ChiyaWeb.Router do
resources "/identities", IdentityController
resources "/comments", CommentController, only: [:index, :show]
resources "/tokens", TokenController, only: [:index, :show, :new, :create, :delete]
resources "/tags", TagController, only: [:index, :edit, :update, :show]
get "/tags/:id/apply", TagController, :apply
get "/notes/import", NoteController, :import_prepare
post "/notes/import", NoteController, :import_run