diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index 240f90c..3200a62 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -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) diff --git a/lib/chiya/tags.ex b/lib/chiya/tags.ex index 2d2d382..a3d16bc 100644 --- a/lib/chiya/tags.ex +++ b/lib/chiya/tags.ex @@ -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. diff --git a/lib/chiya/tags/tag.ex b/lib/chiya/tags/tag.ex index 35a9c72..5731715 100644 --- a/lib/chiya/tags/tag.ex +++ b/lib/chiya/tags/tag.ex @@ -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 diff --git a/lib/chiya_web/components/layouts/app.html.heex b/lib/chiya_web/components/layouts/app.html.heex index 2ee54a9..0b90cdd 100644 --- a/lib/chiya_web/components/layouts/app.html.heex +++ b/lib/chiya_web/components/layouts/app.html.heex @@ -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", diff --git a/lib/chiya_web/controllers/tag_controller.ex b/lib/chiya_web/controllers/tag_controller.ex new file mode 100644 index 0000000..08bc249 --- /dev/null +++ b/lib/chiya_web/controllers/tag_controller.ex @@ -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 diff --git a/lib/chiya_web/controllers/tag_html.ex b/lib/chiya_web/controllers/tag_html.ex new file mode 100644 index 0000000..d3daf2f --- /dev/null +++ b/lib/chiya_web/controllers/tag_html.ex @@ -0,0 +1,5 @@ +defmodule ChiyaWeb.TagHTML do + use ChiyaWeb, :html + + embed_templates "tag_html/*" +end diff --git a/lib/chiya_web/controllers/tag_html/apply_prepare.html.heex b/lib/chiya_web/controllers/tag_html/apply_prepare.html.heex new file mode 100644 index 0000000..e98aeb1 --- /dev/null +++ b/lib/chiya_web/controllers/tag_html/apply_prepare.html.heex @@ -0,0 +1,9 @@ +<.header> + Apply Tag <%= @tag.id %> + <:subtitle>This is a tag record from your database. + <:actions> + <.link href={~p"/admin/tags/#{@tag}/apply"}> + <.button>Apply tag + + + diff --git a/lib/chiya_web/controllers/tag_html/edit.html.heex b/lib/chiya_web/controllers/tag_html/edit.html.heex new file mode 100644 index 0000000..2152110 --- /dev/null +++ b/lib/chiya_web/controllers/tag_html/edit.html.heex @@ -0,0 +1,10 @@ +<.header> + Edit Tag “<%= @tag.name %>” + <:actions> + <.link href={~p"/admin/tags"}> + <.button><.icon name="hero-arrow-left" /> Back to Tags + + + + +<.tag_form changeset={@changeset} action={~p"/admin/tags/#{@tag}"} /> diff --git a/lib/chiya_web/controllers/tag_html/index.html.heex b/lib/chiya_web/controllers/tag_html/index.html.heex new file mode 100644 index 0000000..a973bb2 --- /dev/null +++ b/lib/chiya_web/controllers/tag_html/index.html.heex @@ -0,0 +1,20 @@ +<.header> + <.icon name="hero-document-text" /> Tags + <:subtitle>Tags are tags. + + +
+ <%= for tag <- @tags do %> +
+
+

+ <%= tag.name %> +

+
+ +
+ <% end %> +
diff --git a/lib/chiya_web/controllers/tag_html/show.html.heex b/lib/chiya_web/controllers/tag_html/show.html.heex new file mode 100644 index 0000000..b22843f --- /dev/null +++ b/lib/chiya_web/controllers/tag_html/show.html.heex @@ -0,0 +1,21 @@ +<.header> + Tag <%= @tag.id %> + <:subtitle>This is a tag record from your database. + <:actions> + <.link href={~p"/admin/tags/#{@tag}/edit"}> + <.button>Edit tag + + <.link href={~p"/admin/tags/#{@tag}/apply"}> + <.button>Apply tag + + + + +<.list> + <:item title="Name"><%= @tag.name %> + <:item title="Content"><%= @tag.content %> + <:item title="Slug"><%= @tag.slug %> + <:item title="Regex"><%= @tag.regex %> + + +<.back navigate={~p"/admin/notes"}>Back to notes diff --git a/lib/chiya_web/controllers/tag_html/tag_form.html.heex b/lib/chiya_web/controllers/tag_html/tag_form.html.heex new file mode 100644 index 0000000..bbb905d --- /dev/null +++ b/lib/chiya_web/controllers/tag_html/tag_form.html.heex @@ -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. + + + <.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 + + diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index 5070af6..d0f30c3 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -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