work on contexts
This commit is contained in:
@ -1,22 +1,70 @@
|
||||
defmodule Memex.Contexts.Context do
|
||||
@moduledoc """
|
||||
Represents a document that synthesizes multiple concepts as defined by notes
|
||||
into a single consideration
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Ecto.{Changeset, UUID}
|
||||
alias Memex.Accounts.User
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "contexts" do
|
||||
field :content, :string
|
||||
field :tag, {:array, :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
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
title: String.t(),
|
||||
content: String.t(),
|
||||
tags: [String.t()] | nil,
|
||||
tags_string: String.t(),
|
||||
visibility: :public | :private | :unlisted,
|
||||
user: User.t() | Ecto.Association.NotLoaded.t(),
|
||||
user_id: User.id(),
|
||||
inserted_at: NaiveDateTime.t(),
|
||||
updated_at: NaiveDateTime.t()
|
||||
}
|
||||
@type id :: UUID.t()
|
||||
@type changeset :: Changeset.t(t())
|
||||
|
||||
@doc false
|
||||
def changeset(context, attrs) do
|
||||
context
|
||||
|> cast(attrs, [:title, :content, :tag, :visibility])
|
||||
|> validate_required([:title, :content, :tag, :visibility])
|
||||
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
|
||||
def create_changeset(attrs, %User{id: user_id}) do
|
||||
%__MODULE__{}
|
||||
|> cast(attrs, [:title, :content, :tags, :visibility])
|
||||
|> change(user_id: user_id)
|
||||
|> cast_tags_string(attrs)
|
||||
|> validate_required([:title, :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_tags_string(attrs)
|
||||
|> validate_required([:title, :content, :visibility])
|
||||
end
|
||||
|
||||
defp cast_tags_string(changeset, %{"tags_string" => tags_string})
|
||||
when tags_string |> is_binary() do
|
||||
tags =
|
||||
tags_string
|
||||
|> String.split(",", trim: true)
|
||||
|> Enum.map(fn str -> str |> String.trim() end)
|
||||
|> Enum.sort()
|
||||
|
||||
changeset |> change(tags: tags)
|
||||
end
|
||||
|
||||
defp cast_tags_string(changeset, _attrs), do: changeset
|
||||
end
|
||||
|
@ -1,20 +1,35 @@
|
||||
defmodule Memex.Contexts.ContextNote do
|
||||
@moduledoc """
|
||||
Represents a mapping of a note to a context
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Memex.{Contexts.Context, Contexts.ContextNote, Notes.Note}
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "context_notes" do
|
||||
field :context_id, :binary_id
|
||||
field :note_id, :binary_id
|
||||
belongs_to :context, Context
|
||||
belongs_to :note, Note
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@type t :: %ContextNote{
|
||||
context: Context.t() | nil,
|
||||
context_id: Context.id(),
|
||||
note: Note.t(),
|
||||
note_id: Note.id(),
|
||||
inserted_at: NaiveDateTime.t(),
|
||||
updated_at: NaiveDateTime.t()
|
||||
}
|
||||
@type new_context_note :: %ContextNote{}
|
||||
|
||||
@doc false
|
||||
def changeset(context_note, attrs) do
|
||||
context_note
|
||||
|> cast(attrs, [])
|
||||
|> validate_required([])
|
||||
def create_changeset(%Context{id: context_id}, %Note{id: note_id}) do
|
||||
%ContextNote{}
|
||||
|> change(context_id: context_id, note_id: note_id)
|
||||
|> validate_required([:context_id, :note_id])
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user