work on contexts

This commit is contained in:
2022-11-24 12:44:34 -05:00
parent a0a0697f2d
commit cc11491106
18 changed files with 771 additions and 249 deletions

View File

@@ -4,21 +4,89 @@ defmodule Memex.Contexts do
"""
import Ecto.Query, warn: false
alias Memex.Repo
alias Memex.Contexts.Context
alias Ecto.Changeset
alias Memex.{Accounts.User, Contexts.Context, Repo}
@doc """
Returns the list of contexts.
## Examples
iex> list_contexts()
iex> list_contexts(%User{id: 123})
[%Context{}, ...]
iex> list_contexts("my context", %User{id: 123})
[%Context{title: "my context"}, ...]
"""
def list_contexts do
Repo.all(Context)
@spec list_contexts(User.t()) :: [Context.t()]
@spec list_contexts(search :: String.t() | nil, User.t()) :: [Context.t()]
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)
end
def list_contexts(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from c in Context,
where: c.user_id == ^user_id,
where:
fragment(
"search @@ to_tsquery(websearch_to_tsquery(?)::text || ':*')",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, to_tsquery(websearch_to_tsquery(?)::text || ':*'), 4)",
^trimmed_search
)
}
)
end
@doc """
Returns the list of public contexts for viewing.
## Examples
iex> list_public_contexts()
[%Context{}, ...]
iex> list_public_contexts("my context")
[%Context{title: "my context"}, ...]
"""
@spec list_public_contexts() :: [Context.t()]
@spec list_public_contexts(search :: String.t() | nil) :: [Context.t()]
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)
end
def list_public_contexts(search) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from c in Context,
where: c.visibility == :public,
where:
fragment(
"search @@ to_tsquery(websearch_to_tsquery(?)::text || ':*')",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, to_tsquery(websearch_to_tsquery(?)::text || ':*'), 4)",
^trimmed_search
)
}
)
end
@doc """
@@ -28,31 +96,47 @@ defmodule Memex.Contexts do
## Examples
iex> get_context!(123)
iex> get_context!(123, %User{id: 123})
%Context{}
iex> get_context!(456)
iex> get_context!(456, %User{id: 123})
** (Ecto.NoResultsError)
"""
def get_context!(id), do: Repo.get!(Context, id)
@spec get_context!(Context.id(), User.t()) :: Context.t()
def get_context!(id, %{id: user_id}) do
Repo.one!(
from c in Context,
where: c.id == ^id,
where: c.user_id == ^user_id or c.visibility in [:public, :unlisted]
)
end
def get_context!(id, _invalid_user) do
Repo.one!(
from c in Context,
where: c.id == ^id,
where: c.visibility in [:public, :unlisted]
)
end
@doc """
Creates a context.
## Examples
iex> create_context(%{field: value})
iex> create_context(%{field: value}, %User{id: 123})
{:ok, %Context{}}
iex> create_context(%{field: bad_value})
iex> create_context(%{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}}
"""
def create_context(attrs \\ %{}) do
%Context{}
|> Context.changeset(attrs)
|> Repo.insert()
@spec create_context(User.t()) :: {:ok, Context.t()} | {:error, Context.changeset()}
@spec create_context(attrs :: map(), User.t()) ::
{:ok, Context.t()} | {:error, Context.changeset()}
def create_context(attrs \\ %{}, user) do
Context.create_changeset(attrs, user) |> Repo.insert()
end
@doc """
@@ -60,16 +144,18 @@ defmodule Memex.Contexts do
## Examples
iex> update_context(context, %{field: new_value})
iex> update_context(context, %{field: new_value}, %User{id: 123})
{:ok, %Context{}}
iex> update_context(context, %{field: bad_value})
iex> update_context(context, %{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}}
"""
def update_context(%Context{} = context, attrs) do
@spec update_context(Context.t(), attrs :: map(), User.t()) ::
{:ok, Context.t()} | {:error, Context.changeset()}
def update_context(%Context{} = context, attrs, user) do
context
|> Context.changeset(attrs)
|> Context.update_changeset(attrs, user)
|> Repo.update()
end
@@ -78,15 +164,24 @@ defmodule Memex.Contexts do
## Examples
iex> delete_context(context)
iex> delete_context(%Context{user_id: 123}, %User{id: 123})
{:ok, %Context{}}
iex> delete_context(context)
iex> delete_context(%Context{user_id: 123}, %User{role: :admin})
{:ok, %Context{}}
iex> delete_context(%Context{}, %User{id: 123})
{:error, %Ecto.Changeset{}}
"""
def delete_context(%Context{} = context) do
Repo.delete(context)
@spec delete_context(Context.t(), User.t()) ::
{:ok, Context.t()} | {:error, Context.changeset()}
def delete_context(%Context{user_id: user_id} = context, %{id: user_id}) do
context |> Repo.delete()
end
def delete_context(%Context{} = context, %{role: :admin}) do
context |> Repo.delete()
end
@doc """
@@ -98,7 +193,23 @@ defmodule Memex.Contexts do
%Ecto.Changeset{data: %Context{}}
"""
def change_context(%Context{} = context, attrs \\ %{}) do
Context.changeset(context, attrs)
@spec change_context(Context.t(), User.t()) :: Context.changeset()
@spec change_context(Context.t(), attrs :: map(), User.t()) :: Context.changeset()
def change_context(%Context{} = context, attrs \\ %{}, user) do
context |> Context.update_changeset(attrs, user)
end
@doc """
Gets a canonical string representation of the `:tags` field for a Note
"""
@spec get_tags_string(Context.t() | Context.changeset() | [String.t()] | nil) :: String.t()
def get_tags_string(nil), do: ""
def get_tags_string(tags) when tags |> is_list(), do: tags |> Enum.join(",")
def get_tags_string(%Context{tags: tags}), do: tags |> get_tags_string()
def get_tags_string(%Changeset{} = changeset) do
changeset
|> Changeset.get_field(:tags)
|> get_tags_string()
end
end