Added a JSON encoder for all the main model types

This commit is contained in:
Kieran Eglin 2024-05-01 10:13:55 -07:00
parent 799fa4fe32
commit 9b4b09efb7
No known key found for this signature in database
GPG key ID: 193984967FCF432D
6 changed files with 66 additions and 0 deletions

View file

@ -7,6 +7,8 @@ defmodule Pinchflat.Media.MediaItem do
import Ecto.Changeset
import Pinchflat.Utils.ChangesetUtils
alias __MODULE__
alias Pinchflat.Repo
alias Pinchflat.Tasks.Task
alias Pinchflat.Sources.Source
alias Pinchflat.Metadata.MediaMetadata
@ -116,4 +118,18 @@ defmodule Pinchflat.Media.MediaItem do
end)
|> Enum.into(%{})
end
@doc false
def json_exluded_fields do
~w(__meta__ __struct__ metadata tasks media_items_search_index)a
end
defimpl Jason.Encoder, for: MediaItem do
def encode(value, opts) do
value
|> Repo.preload(:source)
|> Map.drop(MediaItem.json_exluded_fields())
|> Jason.Encode.map(opts)
end
end
end

View file

@ -6,6 +6,7 @@ defmodule Pinchflat.Profiles.MediaProfile do
use Ecto.Schema
import Ecto.Changeset
alias __MODULE__
alias Pinchflat.Sources.Source
@allowed_fields ~w(
@ -84,4 +85,17 @@ defmodule Pinchflat.Profiles.MediaProfile do
def ext_regex do
~r/\.({{ ?ext ?}}|%\( ?ext ?\)[sS])$/
end
@doc false
def json_exluded_fields do
~w(__meta__ __struct__ sources)a
end
defimpl Jason.Encoder, for: MediaProfile do
def encode(value, opts) do
value
|> Map.drop(MediaProfile.json_exluded_fields())
|> Jason.Encode.map(opts)
end
end
end

View file

@ -7,6 +7,8 @@ defmodule Pinchflat.Sources.Source do
import Ecto.Changeset
import Pinchflat.Utils.ChangesetUtils
alias __MODULE__
alias Pinchflat.Repo
alias Pinchflat.Tasks.Task
alias Pinchflat.Media.MediaItem
alias Pinchflat.Profiles.MediaProfile
@ -133,4 +135,18 @@ defmodule Pinchflat.Sources.Source do
def filepath_attributes do
~w(nfo_filepath fanart_filepath poster_filepath banner_filepath)a
end
@doc false
def json_exluded_fields do
~w(__meta__ __struct__ metadata tasks media_items)a
end
defimpl Jason.Encoder, for: Source do
def encode(value, opts) do
value
|> Repo.preload(:media_profile)
|> Map.drop(Source.json_exluded_fields())
|> Jason.Encode.map(opts)
end
end
end

View file

@ -29,6 +29,12 @@ defmodule Pinchflat.MediaTest do
Repo.reload!(metadata)
end
end
test "can be JSON encoded without error" do
media_item = media_item_fixture()
assert {:ok, _} = Phoenix.json_library().encode(media_item)
end
end
describe "list_media_items/0" do

View file

@ -10,6 +10,14 @@ defmodule Pinchflat.ProfilesTest do
@invalid_attrs %{name: nil, output_path_template: nil}
describe "schema" do
test "can be JSON encoded without error" do
profile = media_profile_fixture()
assert {:ok, _} = Phoenix.json_library().encode(profile)
end
end
describe "list_media_profiles/0" do
test "it returns all media_profiles" do
media_profile = media_profile_fixture()

View file

@ -32,6 +32,12 @@ defmodule Pinchflat.SourcesTest do
Repo.reload!(metadata)
end
end
test "can be JSON encoded without error" do
source = source_fixture()
assert {:ok, _} = Phoenix.json_library().encode(source)
end
end
describe "output_path_template/1" do