From 154949f5174df7aa28a23b503bd302551682b5c7 Mon Sep 17 00:00:00 2001 From: Ashlynne Mitchell Date: Sat, 7 Jun 2025 00:23:29 +1000 Subject: [PATCH] feat(appview): add basic changesets for schemas --- apps/backend/lib/comet/repo/comment.ex | 9 ++++++++ apps/backend/lib/comet/repo/embed/facet.ex | 8 +++++++ apps/backend/lib/comet/repo/embed/link.ex | 7 ++++++ apps/backend/lib/comet/repo/identity.ex | 10 ++++++++- apps/backend/lib/comet/repo/like.ex | 9 ++++++++ apps/backend/lib/comet/repo/playlist.ex | 14 +++++++++++- apps/backend/lib/comet/repo/playlist_track.ex | 9 ++++++++ apps/backend/lib/comet/repo/profile.ex | 20 +++++++++++++++++ apps/backend/lib/comet/repo/repost.ex | 9 ++++++++ apps/backend/lib/comet/repo/track.ex | 22 +++++++++++++++++++ 10 files changed, 115 insertions(+), 2 deletions(-) diff --git a/apps/backend/lib/comet/repo/comment.ex b/apps/backend/lib/comet/repo/comment.ex index 92b011a..446409c 100644 --- a/apps/backend/lib/comet/repo/comment.ex +++ b/apps/backend/lib/comet/repo/comment.ex @@ -3,6 +3,7 @@ defmodule Comet.Repo.Comment do Schema containing information about a Comet comment. """ use Comet.Schema + import Ecto.Changeset schema "comments" do field :rkey, :string @@ -19,4 +20,12 @@ defmodule Comet.Repo.Comment do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:rkey, :did, :text, :facets, :subject_id, :subject_type, :langs, :created_at]) + |> validate_required([:rkey, :text]) + end end diff --git a/apps/backend/lib/comet/repo/embed/facet.ex b/apps/backend/lib/comet/repo/embed/facet.ex index 7162b43..19bfe5b 100644 --- a/apps/backend/lib/comet/repo/embed/facet.ex +++ b/apps/backend/lib/comet/repo/embed/facet.ex @@ -1,5 +1,6 @@ defmodule Comet.Repo.Embed.Facet do use Comet.Schema + import Ecto.Changeset @primary_key false embedded_schema do @@ -11,4 +12,11 @@ defmodule Comet.Repo.Embed.Facet do # Sadly Ecto doesn't support union types/embeds so this has to be generic, without doing weirdness in the database at least field :features, {:array, :map} end + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:features]) + |> cast_embed(:index, required: true) + |> validate_required([:features]) + end end diff --git a/apps/backend/lib/comet/repo/embed/link.ex b/apps/backend/lib/comet/repo/embed/link.ex index 770be30..757e77d 100644 --- a/apps/backend/lib/comet/repo/embed/link.ex +++ b/apps/backend/lib/comet/repo/embed/link.ex @@ -1,9 +1,16 @@ defmodule Comet.Repo.Embed.Link do use Comet.Schema + import Ecto.Changeset @primary_key false embedded_schema do field :type, :string field :value, :string end + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:type, :value]) + |> validate_required([:type, :value]) + end end diff --git a/apps/backend/lib/comet/repo/identity.ex b/apps/backend/lib/comet/repo/identity.ex index c44257b..4ee1309 100644 --- a/apps/backend/lib/comet/repo/identity.ex +++ b/apps/backend/lib/comet/repo/identity.ex @@ -3,6 +3,7 @@ defmodule Comet.Repo.Identity do Schema containing information about an ATProtocol identity. """ use Ecto.Schema + import Ecto.Changeset @primary_key {:did, :string, autogenerate: false} @foreign_key_type :string @@ -10,9 +11,16 @@ defmodule Comet.Repo.Identity do schema "identity" do field :handle, :string field :active, :boolean - # TODO: see if it'd be possible to set this to an enum, if ecto allows open enums field :status, :string timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:did, :handle, :active, :status]) + |> validate_required([:did, :active]) + end end diff --git a/apps/backend/lib/comet/repo/like.ex b/apps/backend/lib/comet/repo/like.ex index 73d971e..05ac9bd 100644 --- a/apps/backend/lib/comet/repo/like.ex +++ b/apps/backend/lib/comet/repo/like.ex @@ -3,6 +3,7 @@ defmodule Comet.Repo.Like do Schema containing information about a Comet like. """ use Comet.Schema + import Ecto.Changeset schema "likes" do field :rkey, :string @@ -14,4 +15,12 @@ defmodule Comet.Repo.Like do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:rkey, :did, :subject_id, :subject_type, :created_at]) + |> validate_required([:rkey, :did, :subject_id, :subject_type, :created_at]) + end end diff --git a/apps/backend/lib/comet/repo/playlist.ex b/apps/backend/lib/comet/repo/playlist.ex index bff3bf7..f153888 100644 --- a/apps/backend/lib/comet/repo/playlist.ex +++ b/apps/backend/lib/comet/repo/playlist.ex @@ -1,14 +1,16 @@ defmodule Comet.Repo.Playlist do @moduledoc """ - Schema containing information about a Comet playlist. + Sch ema containing information about a Comet playlist. """ use Comet.Schema + import Ecto.Changeset schema "playlists" do field :rkey, :string field :title, :string field :image, :string field :description, :string + # TODO: see how this looks with/without primary id embeds_one :description_facets, Repo.Embed.Facet, on_replace: :update field :type, :string field :tags, {:array, :string} @@ -20,4 +22,14 @@ defmodule Comet.Repo.Playlist do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:rkey, :did, :title, :image, :description, :type, :tags, :created_at]) + |> cast_embed(:description_facets) + |> cast_embed(:link) + |> validate_required([:rkey, :did, :title, :type, :created_at]) + end end diff --git a/apps/backend/lib/comet/repo/playlist_track.ex b/apps/backend/lib/comet/repo/playlist_track.ex index 8dedaad..cac831d 100644 --- a/apps/backend/lib/comet/repo/playlist_track.ex +++ b/apps/backend/lib/comet/repo/playlist_track.ex @@ -3,6 +3,7 @@ defmodule Comet.Repo.PlaylistTrack do Schema containing information about a track in a Comet playlist. """ use Comet.Schema + import Ecto.Changeset schema "playlist_tracks" do field :rkey, :string @@ -15,4 +16,12 @@ defmodule Comet.Repo.PlaylistTrack do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:rkey, :did, :position, :created_at, :track_id, :playlist_id]) + |> validate_required([:rkey, :did, :position, :created_at, :track_id, :playlist_id]) + end end diff --git a/apps/backend/lib/comet/repo/profile.ex b/apps/backend/lib/comet/repo/profile.ex index d33ed37..cc9e2b1 100644 --- a/apps/backend/lib/comet/repo/profile.ex +++ b/apps/backend/lib/comet/repo/profile.ex @@ -3,7 +3,9 @@ defmodule Comet.Repo.Profile do Schema containing information about a Comet profile. """ use Comet.Schema + import Ecto.Changeset + # TODO: should probably keep track of CID so as to not do unnecessary writes schema "profiles" do field :rkey, :string, default: "self" field :display_name, :string @@ -18,4 +20,22 @@ defmodule Comet.Repo.Profile do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [ + :rkey, + :did, + :display_name, + :description, + :avatar, + :banner, + :featured_items, + :created_at + ]) + |> cast_embed(:description_facets) + |> validate_required([:rkey, :did]) + end end diff --git a/apps/backend/lib/comet/repo/repost.ex b/apps/backend/lib/comet/repo/repost.ex index d2e5392..6a8bc3c 100644 --- a/apps/backend/lib/comet/repo/repost.ex +++ b/apps/backend/lib/comet/repo/repost.ex @@ -3,6 +3,7 @@ defmodule Comet.Repo.Repost do Schema containing information about a Comet repost. """ use Comet.Schema + import Ecto.Changeset schema "reposts" do field :rkey, :string @@ -14,4 +15,12 @@ defmodule Comet.Repo.Repost do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:rkey, :did, :subject_id, :subject_type, :created_at]) + |> validate_required([:rkey, :did, :subject_id, :subject_type, :created_at]) + end end diff --git a/apps/backend/lib/comet/repo/track.ex b/apps/backend/lib/comet/repo/track.ex index bad0528..dc3961e 100644 --- a/apps/backend/lib/comet/repo/track.ex +++ b/apps/backend/lib/comet/repo/track.ex @@ -3,6 +3,7 @@ defmodule Comet.Repo.Track do Schema containing information about a Comet track. """ use Comet.Schema + import Ecto.Changeset schema "tracks" do field :rkey, :string @@ -21,4 +22,25 @@ defmodule Comet.Repo.Track do timestamps(inserted_at: :indexed_at, updated_at: false) end + + def new(params \\ %{}), do: changeset(%__MODULE__{}, params) + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [ + :rkey, + :did, + :title, + :audio, + :image, + :description, + :explicit, + :tags, + :created_at, + :released_at + ]) + |> cast_embed(:description_facets) + |> cast_embed(:link) + |> validate_required([:rkey, :did, :audio, :title, :created_at]) + end end -- 2.51.2