use slugs

This commit is contained in:
2022-11-26 14:51:18 -05:00
parent e9360fb3d5
commit 264f13e523
48 changed files with 536 additions and 280 deletions

View File

@ -16,7 +16,7 @@ defmodule Memex.Contexts do
[%Context{}, ...]
iex> list_contexts("my context", %User{id: 123})
[%Context{title: "my context"}, ...]
[%Context{slug: "my context"}, ...]
"""
@spec list_contexts(User.t()) :: [Context.t()]
@ -24,7 +24,7 @@ defmodule Memex.Contexts do
def list_contexts(search \\ nil, user)
def list_contexts(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from c in Context, where: c.user_id == ^user_id, order_by: c.title)
Repo.all(from c in Context, where: c.user_id == ^user_id, order_by: c.slug)
end
def list_contexts(search, %{id: user_id}) when search |> is_binary() do
@ -57,7 +57,7 @@ defmodule Memex.Contexts do
[%Context{}, ...]
iex> list_public_contexts("my context")
[%Context{title: "my context"}, ...]
[%Context{slug: "my context"}, ...]
"""
@spec list_public_contexts() :: [Context.t()]
@ -65,7 +65,7 @@ defmodule Memex.Contexts do
def list_public_contexts(search \\ nil)
def list_public_contexts(search) when search |> is_nil() or search == "" do
Repo.all(from c in Context, where: c.visibility == :public, order_by: c.title)
Repo.all(from c in Context, where: c.visibility == :public, order_by: c.slug)
end
def list_public_contexts(search) when search |> is_binary() do
@ -120,6 +120,37 @@ defmodule Memex.Contexts do
)
end
@doc """
Gets a single context by a slug.
Raises `Ecto.NoResultsError` if the Context does not exist.
## Examples
iex> get_context_by_slug("my-context", %User{id: 123})
%Context{}
iex> get_context_by_slug("my-context", %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_context_by_slug(Context.slug(), User.t()) :: Context.t() | nil
def get_context_by_slug(slug, %{id: user_id}) do
Repo.one(
from c in Context,
where: c.slug == ^slug,
where: c.user_id == ^user_id or c.visibility in [:public, :unlisted]
)
end
def get_context_by_slug(slug, _invalid_user) do
Repo.one(
from c in Context,
where: c.slug == ^slug,
where: c.visibility in [:public, :unlisted]
)
end
@doc """
Creates a context.

View File

@ -3,19 +3,19 @@ defmodule Memex.Contexts.Context do
Represents a document that synthesizes multiple concepts as defined by notes
into a single consideration
"""
use Ecto.Schema
import Ecto.Changeset
import MemexWeb.Gettext
alias Ecto.{Changeset, UUID}
alias Memex.Accounts.User
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "contexts" do
field :slug, :string
field :content, :string
field :tags, {:array, :string}
field :tags_string, :string, virtual: true
field :title, :string
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User
@ -24,7 +24,7 @@ defmodule Memex.Contexts.Context do
end
@type t :: %__MODULE__{
title: String.t(),
slug: slug(),
content: String.t(),
tags: [String.t()] | nil,
tags_string: String.t(),
@ -35,24 +35,31 @@ defmodule Memex.Contexts.Context do
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type slug :: String.t()
@type changeset :: Changeset.t(t())
@doc false
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do
%__MODULE__{}
|> cast(attrs, [:title, :content, :tags, :visibility])
|> cast(attrs, [:slug, :content, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_required([:title, :content, :user_id, :visibility])
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :user_id, :visibility])
end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
note
|> cast(attrs, [:title, :content, :tags, :visibility])
|> cast(attrs, [:slug, :content, :tags, :visibility])
|> cast_tags_string(attrs)
|> validate_required([:title, :content, :visibility])
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :visibility])
end
defp cast_tags_string(changeset, %{"tags_string" => tags_string})

View File

@ -16,7 +16,7 @@ defmodule Memex.Notes do
[%Note{}, ...]
iex> list_notes("my note", %User{id: 123})
[%Note{title: "my note"}, ...]
[%Note{slug: "my note"}, ...]
"""
@spec list_notes(User.t()) :: [Note.t()]
@ -24,7 +24,7 @@ defmodule Memex.Notes do
def list_notes(search \\ nil, user)
def list_notes(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from n in Note, where: n.user_id == ^user_id, order_by: n.title)
Repo.all(from n in Note, where: n.user_id == ^user_id, order_by: n.slug)
end
def list_notes(search, %{id: user_id}) when search |> is_binary() do
@ -57,14 +57,14 @@ defmodule Memex.Notes do
[%Note{}, ...]
iex> list_public_notes("my note")
[%Note{title: "my note"}, ...]
[%Note{slug: "my note"}, ...]
"""
@spec list_public_notes() :: [Note.t()]
@spec list_public_notes(search :: String.t() | nil) :: [Note.t()]
def list_public_notes(search \\ nil)
def list_public_notes(search) when search |> is_nil() or search == "" do
Repo.all(from n in Note, where: n.visibility == :public, order_by: n.title)
Repo.all(from n in Note, where: n.visibility == :public, order_by: n.slug)
end
def list_public_notes(search) when search |> is_binary() do
@ -119,6 +119,37 @@ defmodule Memex.Notes do
)
end
@doc """
Gets a single note by slug.
Raises `Ecto.NoResultsError` if the Note does not exist.
## Examples
iex> get_note_by_slug("my-note", %User{id: 123})
%Note{}
iex> get_note_by_slug("my-note", %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_note_by_slug(Note.slug(), User.t()) :: Note.t() | nil
def get_note_by_slug(slug, %{id: user_id}) do
Repo.one(
from n in Note,
where: n.slug == ^slug,
where: n.user_id == ^user_id or n.visibility in [:public, :unlisted]
)
end
def get_note_by_slug(slug, _invalid_user) do
Repo.one(
from n in Note,
where: n.slug == ^slug,
where: n.visibility in [:public, :unlisted]
)
end
@doc """
Creates a note.
@ -189,7 +220,7 @@ defmodule Memex.Notes do
iex> change_note(note, %User{id: 123})
%Ecto.Changeset{data: %Note{}}
iex> change_note(note, %{title: "new title"}, %User{id: 123})
iex> change_note(note, %{slug: "new slug"}, %User{id: 123})
%Ecto.Changeset{data: %Note{}}
"""

View File

@ -4,13 +4,14 @@ defmodule Memex.Notes.Note do
"""
use Ecto.Schema
import Ecto.Changeset
import MemexWeb.Gettext
alias Ecto.{Changeset, UUID}
alias Memex.Accounts.User
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "notes" do
field :title, :string
field :slug, :string
field :content, :string
field :tags, {:array, :string}
field :tags_string, :string, virtual: true
@ -22,7 +23,7 @@ defmodule Memex.Notes.Note do
end
@type t :: %__MODULE__{
title: String.t(),
slug: slug(),
content: String.t(),
tags: [String.t()] | nil,
tags_string: String.t(),
@ -33,24 +34,31 @@ defmodule Memex.Notes.Note do
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type slug :: String.t()
@type changeset :: Changeset.t(t())
@doc false
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do
%__MODULE__{}
|> cast(attrs, [:title, :content, :tags, :visibility])
|> cast(attrs, [:slug, :content, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_required([:title, :content, :user_id, :visibility])
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :user_id, :visibility])
end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
note
|> cast(attrs, [:title, :content, :tags, :visibility])
|> cast(attrs, [:slug, :content, :tags, :visibility])
|> cast_tags_string(attrs)
|> validate_required([:title, :content, :visibility])
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :visibility])
end
defp cast_tags_string(changeset, %{"tags_string" => tags_string})

View File

@ -16,7 +16,7 @@ defmodule Memex.Pipelines do
[%Pipeline{}, ...]
iex> list_pipelines("my pipeline", %User{id: 123})
[%Pipeline{title: "my pipeline"}, ...]
[%Pipeline{slug: "my pipeline"}, ...]
"""
@spec list_pipelines(User.t()) :: [Pipeline.t()]
@ -24,7 +24,7 @@ defmodule Memex.Pipelines do
def list_pipelines(search \\ nil, user)
def list_pipelines(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from p in Pipeline, where: p.user_id == ^user_id, order_by: p.title)
Repo.all(from p in Pipeline, where: p.user_id == ^user_id, order_by: p.slug)
end
def list_pipelines(search, %{id: user_id}) when search |> is_binary() do
@ -57,14 +57,14 @@ defmodule Memex.Pipelines do
[%Pipeline{}, ...]
iex> list_public_pipelines("my pipeline")
[%Pipeline{title: "my pipeline"}, ...]
[%Pipeline{slug: "my pipeline"}, ...]
"""
@spec list_public_pipelines() :: [Pipeline.t()]
@spec list_public_pipelines(search :: String.t() | nil) :: [Pipeline.t()]
def list_public_pipelines(search \\ nil)
def list_public_pipelines(search) when search |> is_nil() or search == "" do
Repo.all(from p in Pipeline, where: p.visibility == :public, order_by: p.title)
Repo.all(from p in Pipeline, where: p.visibility == :public, order_by: p.slug)
end
def list_public_pipelines(search) when search |> is_binary() do
@ -119,6 +119,37 @@ defmodule Memex.Pipelines do
)
end
@doc """
Gets a single pipeline by it's slug.
Raises `Ecto.NoResultsError` if the Pipeline does not exist.
## Examples
iex> get_pipeline_by_slug("my-pipeline", %User{id: 123})
%Pipeline{}
iex> get_pipeline_by_slug("my-pipeline", %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_pipeline_by_slug(Pipeline.slug(), User.t()) :: Pipeline.t() | nil
def get_pipeline_by_slug(slug, %{id: user_id}) do
Repo.one(
from p in Pipeline,
where: p.slug == ^slug,
where: p.user_id == ^user_id or p.visibility in [:public, :unlisted]
)
end
def get_pipeline_by_slug(slug, _invalid_user) do
Repo.one(
from p in Pipeline,
where: p.slug == ^slug,
where: p.visibility in [:public, :unlisted]
)
end
@doc """
Creates a pipeline.
@ -191,7 +222,7 @@ defmodule Memex.Pipelines do
iex> change_pipeline(pipeline, %User{id: 123})
%Ecto.Changeset{data: %Pipeline{}}
iex> change_pipeline(pipeline, %{title: "new title"}, %User{id: 123})
iex> change_pipeline(pipeline, %{slug: "new slug"}, %User{id: 123})
%Ecto.Changeset{data: %Pipeline{}}
"""

View File

@ -4,13 +4,14 @@ defmodule Memex.Pipelines.Pipeline do
"""
use Ecto.Schema
import Ecto.Changeset
import MemexWeb.Gettext
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Pipelines.Step}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "pipelines" do
field :title, :string
field :slug, :string
field :description, :string
field :tags, {:array, :string}
field :tags_string, :string, virtual: true
@ -24,7 +25,7 @@ defmodule Memex.Pipelines.Pipeline do
end
@type t :: %__MODULE__{
title: String.t(),
slug: slug(),
description: String.t(),
tags: [String.t()] | nil,
tags_string: String.t(),
@ -35,24 +36,31 @@ defmodule Memex.Pipelines.Pipeline do
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type slug :: String.t()
@type changeset :: Changeset.t(t())
@doc false
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do
%__MODULE__{}
|> cast(attrs, [:title, :description, :tags, :visibility])
|> cast(attrs, [:slug, :description, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_required([:title, :user_id, :visibility])
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :user_id, :visibility])
end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = pipeline, attrs, %User{id: user_id}) do
pipeline
|> cast(attrs, [:title, :description, :tags, :visibility])
|> cast(attrs, [:slug, :description, :tags, :visibility])
|> cast_tags_string(attrs)
|> validate_required([:title, :visibility])
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :visibility])
end
defp cast_tags_string(changeset, %{"tags_string" => tags_string})

View File

@ -44,7 +44,7 @@ defmodule MemexWeb.Components.ContextsTableComponent do
end
columns = [
%{label: gettext("title"), key: :title},
%{label: gettext("slug"), key: :slug},
%{label: gettext("content"), key: :content},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
@ -89,20 +89,20 @@ defmodule MemexWeb.Components.ContextsTableComponent do
@spec get_value_for_key(atom(), Context.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:title, %{id: id, title: title}, _additional_data) do
assigns = %{id: id, title: title}
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
assigns = %{slug: slug}
title_block = ~H"""
slug_block = ~H"""
<.link
navigate={Routes.context_show_path(Endpoint, :show, @id)}
navigate={Routes.context_show_path(Endpoint, :show, @slug)}
class="link"
data-qa={"context-show-#{@id}"}
data-qa={"context-show-#{@slug}"}
>
<%= @title %>
<%= @slug %>
</.link>
"""
{title, title_block}
{slug, slug_block}
end
defp get_value_for_key(:content, %{content: content}, _additional_data) do

View File

@ -44,7 +44,7 @@ defmodule MemexWeb.Components.NotesTableComponent do
end
columns = [
%{label: gettext("title"), key: :title},
%{label: gettext("slug"), key: :slug},
%{label: gettext("content"), key: :content},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
@ -89,20 +89,20 @@ defmodule MemexWeb.Components.NotesTableComponent do
@spec get_value_for_key(atom(), Note.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:title, %{id: id, title: title}, _additional_data) do
assigns = %{id: id, title: title}
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
assigns = %{slug: slug}
title_block = ~H"""
slug_block = ~H"""
<.link
navigate={Routes.note_show_path(Endpoint, :show, @id)}
navigate={Routes.note_show_path(Endpoint, :show, @slug)}
class="link"
data-qa={"note-show-#{@id}"}
data-qa={"note-show-#{@slug}"}
>
<%= @title %>
<%= @slug %>
</.link>
"""
{title, title_block}
{slug, slug_block}
end
defp get_value_for_key(:content, %{content: content}, _additional_data) do

View File

@ -44,7 +44,7 @@ defmodule MemexWeb.Components.PipelinesTableComponent do
end
columns = [
%{label: gettext("title"), key: :title},
%{label: gettext("slug"), key: :slug},
%{label: gettext("description"), key: :description},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
@ -89,20 +89,20 @@ defmodule MemexWeb.Components.PipelinesTableComponent do
@spec get_value_for_key(atom(), Pipeline.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:title, %{id: id, title: title}, _additional_data) do
assigns = %{id: id, title: title}
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
assigns = %{slug: slug}
title_block = ~H"""
slug_block = ~H"""
<.link
navigate={Routes.pipeline_show_path(Endpoint, :show, @id)}
navigate={Routes.pipeline_show_path(Endpoint, :show, @slug)}
class="link"
data-qa={"pipeline-show-#{@id}"}
data-qa={"pipeline-show-#{@slug}"}
>
<%= @title %>
<%= @slug %>
</.link>
"""
{title, title_block}
{slug, slug_block}
end
defp get_value_for_key(:description, %{description: description}, _additional_data) do

View File

@ -38,10 +38,10 @@ defmodule MemexWeb.ContextLive.FormComponent do
context_params
) do
case Contexts.update_context(context, context_params, current_user) do
{:ok, %{title: title}} ->
{:ok, %{slug: slug}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} saved", title: title))
|> put_flash(:info, gettext("%{slug} saved", slug: slug))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
@ -55,10 +55,10 @@ defmodule MemexWeb.ContextLive.FormComponent do
context_params
) do
case Contexts.create_context(context_params, current_user) do
{:ok, %{title: title}} ->
{:ok, %{slug: slug}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} created", title: title))
|> put_flash(:info, gettext("%{slug} created", slug: slug))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->

View File

@ -9,11 +9,11 @@
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
>
<%= text_input(f, :title,
<%= text_input(f, :slug,
class: "input input-primary",
placeholder: gettext("title")
placeholder: gettext("slug")
) %>
<%= error_tag(f, :title) %>
<%= error_tag(f, :slug) %>
<%= textarea(f, :content,
id: "context-form-content",

View File

@ -16,11 +16,11 @@ defmodule MemexWeb.ContextLive.Index do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = context = Contexts.get_context!(id, current_user)
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = context = Contexts.get_context_by_slug(slug, current_user)
socket
|> assign(page_title: gettext("edit %{title}", title: title))
|> assign(page_title: gettext("edit %{slug}", slug: slug))
|> assign(context: context)
end
@ -49,12 +49,12 @@ defmodule MemexWeb.ContextLive.Index do
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
context = Contexts.get_context!(id, current_user)
{:ok, %{title: title}} = Contexts.delete_context(context, current_user)
{:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
socket =
socket
|> assign(contexts: Contexts.list_contexts(current_user))
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
{:noreply, socket}
end

View File

@ -32,7 +32,7 @@
<:actions :let={context}>
<%= if is_owner?(context, @current_user) do %>
<.link
patch={Routes.context_index_path(@socket, :edit, context)}
patch={Routes.context_index_path(@socket, :edit, context.slug)}
data-qa={"context-edit-#{context.id}"}
>
<%= dgettext("actions", "edit") %>

View File

@ -10,11 +10,15 @@ defmodule MemexWeb.ContextLive.Show do
@impl true
def handle_params(
%{"id" => id},
%{"slug" => slug},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
context = Contexts.get_context!(id, current_user)
context =
case Contexts.get_context_by_slug(slug, current_user) do
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
context -> context
end
socket =
socket
@ -30,18 +34,18 @@ defmodule MemexWeb.ContextLive.Show do
_params,
%{assigns: %{context: context, current_user: current_user}} = socket
) do
{:ok, %{title: title}} = Contexts.delete_context(context, current_user)
{:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|> push_navigate(to: Routes.context_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show, %{title: title}), do: title
defp page_title(:edit, %{title: title}), do: gettext("edit %{title}", title: title)
defp page_title(:show, %{slug: slug}), do: slug
defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug)
@spec is_owner_or_admin?(Context.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true

View File

@ -1,6 +1,6 @@
<div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @context.title %>
<%= @context.slug %>
</h1>
<p><%= if @context.tags, do: @context.tags |> Enum.join(", ") %></p>
@ -23,7 +23,10 @@
<%= dgettext("actions", "back") %>
</.link>
<%= if is_owner?(@context, @current_user) do %>
<.link class="btn btn-primary" patch={Routes.context_show_path(@socket, :edit, @context)}>
<.link
class="btn btn-primary"
patch={Routes.context_show_path(@socket, :edit, @context.slug)}
>
<%= dgettext("actions", "edit") %>
</.link>
<% end %>
@ -42,7 +45,7 @@
</div>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.context_show_path(@socket, :show, @context)}>
<.modal return_to={Routes.context_show_path(@socket, :show, @context.slug)}>
<.live_component
module={MemexWeb.ContextLive.FormComponent}
id={@context.id}
@ -50,7 +53,7 @@
title={@page_title}
action={@live_action}
context={@context}
return_to={Routes.context_show_path(@socket, :show, @context)}
return_to={Routes.context_show_path(@socket, :show, @context.slug)}
/>
</.modal>
<% end %>

View File

@ -37,10 +37,10 @@ defmodule MemexWeb.NoteLive.FormComponent do
note_params
) do
case Notes.update_note(note, note_params, current_user) do
{:ok, %{title: title}} ->
{:ok, %{slug: slug}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} saved", title: title))
|> put_flash(:info, gettext("%{slug} saved", slug: slug))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
@ -54,10 +54,10 @@ defmodule MemexWeb.NoteLive.FormComponent do
note_params
) do
case Notes.create_note(note_params, current_user) do
{:ok, %{title: title}} ->
{:ok, %{slug: slug}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} created", title: title))
|> put_flash(:info, gettext("%{slug} created", slug: slug))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->

View File

@ -9,11 +9,11 @@
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
>
<%= text_input(f, :title,
<%= text_input(f, :slug,
class: "input input-primary",
placeholder: gettext("title")
placeholder: gettext("slug")
) %>
<%= error_tag(f, :title) %>
<%= error_tag(f, :slug) %>
<%= textarea(f, :content,
id: "note-form-content",

View File

@ -16,11 +16,11 @@ defmodule MemexWeb.NoteLive.Index do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = note = Notes.get_note!(id, current_user)
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = note = Notes.get_note_by_slug(slug, current_user)
socket
|> assign(page_title: gettext("edit %{title}", title: title))
|> assign(page_title: gettext("edit %{slug}", slug: slug))
|> assign(note: note)
end
@ -49,12 +49,12 @@ defmodule MemexWeb.NoteLive.Index do
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
note = Notes.get_note!(id, current_user)
{:ok, %{title: title}} = Notes.delete_note(note, current_user)
{:ok, %{slug: slug}} = Notes.delete_note(note, current_user)
socket =
socket
|> assign(notes: Notes.list_notes(current_user))
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
{:noreply, socket}
end

View File

@ -32,7 +32,7 @@
<:actions :let={note}>
<%= if is_owner?(note, @current_user) do %>
<.link
patch={Routes.note_index_path(@socket, :edit, note)}
patch={Routes.note_index_path(@socket, :edit, note.slug)}
data-qa={"note-edit-#{note.id}"}
>
<%= dgettext("actions", "edit") %>

View File

@ -10,11 +10,15 @@ defmodule MemexWeb.NoteLive.Show do
@impl true
def handle_params(
%{"id" => id},
%{"slug" => slug},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
note = Notes.get_note!(id, current_user)
note =
case Notes.get_note_by_slug(slug, current_user) do
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
note -> note
end
socket =
socket
@ -30,18 +34,18 @@ defmodule MemexWeb.NoteLive.Show do
_params,
%{assigns: %{note: note, current_user: current_user}} = socket
) do
{:ok, %{title: title}} = Notes.delete_note(note, current_user)
{:ok, %{slug: slug}} = Notes.delete_note(note, current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|> push_navigate(to: Routes.note_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show, %{title: title}), do: title
defp page_title(:edit, %{title: title}), do: gettext("edit %{title}", title: title)
defp page_title(:show, %{slug: slug}), do: slug
defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug)
@spec is_owner_or_admin?(Note.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true

View File

@ -1,6 +1,6 @@
<div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @note.title %>
<%= @note.slug %>
</h1>
<p><%= if @note.tags, do: @note.tags |> Enum.join(", ") %></p>
@ -23,7 +23,7 @@
<%= dgettext("actions", "back") %>
</.link>
<%= if is_owner?(@note, @current_user) do %>
<.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}>
<.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note.slug)}>
<%= dgettext("actions", "edit") %>
</.link>
<% end %>
@ -42,7 +42,7 @@
</div>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.note_show_path(@socket, :show, @note)}>
<.modal return_to={Routes.note_show_path(@socket, :show, @note.slug)}>
<.live_component
module={MemexWeb.NoteLive.FormComponent}
id={@note.id}
@ -50,7 +50,7 @@
title={@page_title}
action={@live_action}
note={@note}
return_to={Routes.note_show_path(@socket, :show, @note)}
return_to={Routes.note_show_path(@socket, :show, @note.slug)}
/>
</.modal>
<% end %>

View File

@ -42,10 +42,10 @@ defmodule MemexWeb.PipelineLive.FormComponent do
pipeline_params
) do
case Pipelines.update_pipeline(pipeline, pipeline_params, current_user) do
{:ok, %{title: title}} ->
{:ok, %{slug: slug}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} saved", title: title))
|> put_flash(:info, gettext("%{slug} saved", slug: slug))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
@ -59,10 +59,10 @@ defmodule MemexWeb.PipelineLive.FormComponent do
pipeline_params
) do
case Pipelines.create_pipeline(pipeline_params, current_user) do
{:ok, %{title: title}} ->
{:ok, %{slug: slug}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} created", title: title))
|> put_flash(:info, gettext("%{slug} created", slug: slug))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->

View File

@ -9,11 +9,11 @@
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
>
<%= text_input(f, :title,
<%= text_input(f, :slug,
class: "input input-primary",
placeholder: gettext("title")
placeholder: gettext("slug")
) %>
<%= error_tag(f, :title) %>
<%= error_tag(f, :slug) %>
<%= textarea(f, :description,
id: "pipeline-form-description",

View File

@ -16,11 +16,11 @@ defmodule MemexWeb.PipelineLive.Index do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = pipeline = Pipelines.get_pipeline!(id, current_user)
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = pipeline = Pipelines.get_pipeline_by_slug(slug, current_user)
socket
|> assign(page_title: gettext("edit %{title}", title: title))
|> assign(page_title: gettext("edit %{slug}", slug: slug))
|> assign(pipeline: pipeline)
end
@ -49,12 +49,12 @@ defmodule MemexWeb.PipelineLive.Index do
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
pipeline = Pipelines.get_pipeline!(id, current_user)
{:ok, %{title: title}} = Pipelines.delete_pipeline(pipeline, current_user)
{:ok, %{slug: slug}} = Pipelines.delete_pipeline(pipeline, current_user)
socket =
socket
|> assign(pipelines: Pipelines.list_pipelines(current_user))
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
{:noreply, socket}
end

View File

@ -32,7 +32,7 @@
<:actions :let={pipeline}>
<%= if is_owner?(pipeline, @current_user) do %>
<.link
patch={Routes.pipeline_index_path(@socket, :edit, pipeline)}
patch={Routes.pipeline_index_path(@socket, :edit, pipeline.slug)}
data-qa={"pipeline-edit-#{pipeline.id}"}
>
<%= dgettext("actions", "edit") %>

View File

@ -10,11 +10,15 @@ defmodule MemexWeb.PipelineLive.Show do
@impl true
def handle_params(
%{"id" => id},
%{"slug" => slug},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
pipeline = Pipelines.get_pipeline!(id, current_user)
pipeline =
case Pipelines.get_pipeline_by_slug(slug, current_user) do
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
pipeline -> pipeline
end
socket =
socket
@ -30,18 +34,18 @@ defmodule MemexWeb.PipelineLive.Show do
_params,
%{assigns: %{pipeline: pipeline, current_user: current_user}} = socket
) do
{:ok, %{title: title}} = Pipelines.delete_pipeline(pipeline, current_user)
{:ok, %{slug: slug}} = Pipelines.delete_pipeline(pipeline, current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|> push_navigate(to: Routes.pipeline_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show, %{title: title}), do: title
defp page_title(:edit, %{title: title}), do: gettext("edit %{title}", title: title)
defp page_title(:show, %{slug: slug}), do: slug
defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug)
@spec is_owner_or_admin?(Pipeline.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true

View File

@ -1,6 +1,6 @@
<div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @pipeline.title %>
<%= @pipeline.slug %>
</h1>
<p><%= if @pipeline.tags, do: @pipeline.tags |> Enum.join(", ") %></p>
@ -23,7 +23,10 @@
<%= dgettext("actions", "back") %>
</.link>
<%= if is_owner?(@pipeline, @current_user) do %>
<.link class="btn btn-primary" patch={Routes.pipeline_show_path(@socket, :edit, @pipeline)}>
<.link
class="btn btn-primary"
patch={Routes.pipeline_show_path(@socket, :edit, @pipeline.slug)}
>
<%= dgettext("actions", "edit") %>
</.link>
<% end %>
@ -42,7 +45,7 @@
</div>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}>
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline.slug)}>
<.live_component
module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id}
@ -50,7 +53,7 @@
title={@page_title}
action={@live_action}
pipeline={@pipeline}
return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}
return_to={Routes.pipeline_show_path(@socket, :show, @pipeline.slug)}
/>
</.modal>
<% end %>

View File

@ -0,0 +1,3 @@
defmodule MemexWeb.NotFoundError do
defexception [:message, plug_status: 404]
end

View File

@ -58,16 +58,16 @@ defmodule MemexWeb.Router do
pipe_through [:browser, :require_authenticated_user]
live "/notes/new", NoteLive.Index, :new
live "/notes/:id/edit", NoteLive.Index, :edit
live "/note/:id/edit", NoteLive.Show, :edit
live "/notes/:slug/edit", NoteLive.Index, :edit
live "/note/:slug/edit", NoteLive.Show, :edit
live "/contexts/new", ContextLive.Index, :new
live "/contexts/:id/edit", ContextLive.Index, :edit
live "/context/:id/edit", ContextLive.Show, :edit
live "/contexts/:slug/edit", ContextLive.Index, :edit
live "/context/:slug/edit", ContextLive.Show, :edit
live "/pipelines/new", PipelineLive.Index, :new
live "/pipelines/:id/edit", PipelineLive.Index, :edit
live "/pipelines/:id/show/edit", PipelineLive.Show, :edit
live "/pipelines/:slug/edit", PipelineLive.Index, :edit
live "/pipeline/:slug/edit", PipelineLive.Show, :edit
get "/users/settings", UserSettingsController, :edit
put "/users/settings", UserSettingsController, :update
@ -80,15 +80,15 @@ defmodule MemexWeb.Router do
live "/notes", NoteLive.Index, :index
live "/notes/:search", NoteLive.Index, :search
live "/note/:id", NoteLive.Show, :show
live "/note/:slug", NoteLive.Show, :show
live "/contexts", ContextLive.Index, :index
live "/contexts/:search", ContextLive.Index, :search
live "/context/:id", ContextLive.Show, :show
live "/context/:slug", ContextLive.Show, :show
live "/pipelines", PipelineLive.Index, :index
live "/pipelines/:search", PipelineLive.Index, :search
live "/pipeline/:id", PipelineLive.Show, :show
live "/pipeline/:slug", PipelineLive.Show, :show
end
end

View File

@ -11,7 +11,7 @@
<script defer type="text/javascript" src="/js/app.js">
</script>
</head>
<body class="pb-8 m-0 p-0 w-full h-full">
<body class="m-0 p-0 w-full h-full bg-primary-800 text-primary-400 subpixel-antialiased">
<header>
<.topbar current_user={assigns[:current_user]}></.topbar>
</header>
@ -25,7 +25,7 @@
<hr class="w-full hr" />
<a href={Routes.live_path(Endpoint, HomeLive)} class="link title text-primary-400 text-lg">
<%= dgettext("errors", "Go back home") %>
<%= dgettext("errors", "go back home") %>
</a>
</div>
</div>

View File

@ -6,9 +6,9 @@ defmodule MemexWeb.ErrorView do
def template_not_found(error_path, _assigns) do
error_string =
case error_path do
"404.html" -> dgettext("errors", "Not found")
"401.html" -> dgettext("errors", "Unauthorized")
_ -> dgettext("errors", "Internal Server Error")
"404.html" -> dgettext("errors", "not found")
"401.html" -> dgettext("errors", "unauthorized")
_ -> dgettext("errors", "internal server error")
end
render("error.html", %{error_string: error_string})