Fix API controller tests: 404 handling, JSON encoding, media filtering
- Add FallbackController for proper 404 JSON responses on missing resources - Add non-bang get_source/1 and get_media_item/1 for safe lookups - Fix Task JSON encoder: remove Repo.preload call (not available in encoder scope), handle NotLoaded associations by returning nil - Fix media item index: use list_media_items_for_source/1 instead of list_pending_media_items_for/1 which had overly restrictive filtering - Add missing extras_directory to test config - All 1007 tests pass
This commit is contained in:
@@ -6,6 +6,7 @@ config :pinchflat,
|
|||||||
apprise_executable: Path.join([File.cwd!(), "/test/support/scripts/yt-dlp-mocks/repeater.sh"]),
|
apprise_executable: Path.join([File.cwd!(), "/test/support/scripts/yt-dlp-mocks/repeater.sh"]),
|
||||||
media_directory: Path.join([System.tmp_dir!(), "test", "media"]),
|
media_directory: Path.join([System.tmp_dir!(), "test", "media"]),
|
||||||
metadata_directory: Path.join([System.tmp_dir!(), "test", "metadata"]),
|
metadata_directory: Path.join([System.tmp_dir!(), "test", "metadata"]),
|
||||||
|
extras_directory: Path.join([System.tmp_dir!(), "test", "extras"]),
|
||||||
tmpfile_directory: Path.join([System.tmp_dir!(), "test", "tmpfiles"]),
|
tmpfile_directory: Path.join([System.tmp_dir!(), "test", "tmpfiles"]),
|
||||||
file_watcher_poll_interval: 50
|
file_watcher_poll_interval: 50
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,16 @@ defmodule Pinchflat.Media do
|
|||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns all media_items for a given source_id, without any pending/upgradeable filtering.
|
||||||
|
Used by the API to list all media items associated with a source.
|
||||||
|
|
||||||
|
Returns [%MediaItem{}, ...].
|
||||||
|
"""
|
||||||
|
def list_media_items_for_source(source_id) do
|
||||||
|
Repo.all(from mi in MediaItem, where: mi.source_id == ^source_id)
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
For a given media_item, tells you if it is pending download. This is defined as
|
For a given media_item, tells you if it is pending download. This is defined as
|
||||||
the media_item satisfying `MediaQuery.pending` which you should really check out.
|
the media_item satisfying `MediaQuery.pending` which you should really check out.
|
||||||
@@ -110,6 +120,7 @@ defmodule Pinchflat.Media do
|
|||||||
Returns %MediaItem{}. Raises `Ecto.NoResultsError` if the Media item does not exist.
|
Returns %MediaItem{}. Raises `Ecto.NoResultsError` if the Media item does not exist.
|
||||||
"""
|
"""
|
||||||
def get_media_item!(id), do: Repo.get!(MediaItem, id)
|
def get_media_item!(id), do: Repo.get!(MediaItem, id)
|
||||||
|
def get_media_item(id), do: Repo.get(MediaItem, id)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a media_item.
|
Creates a media_item.
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ defmodule Pinchflat.Sources do
|
|||||||
Returns %Source{}. Raises `Ecto.NoResultsError` if the Source does not exist.
|
Returns %Source{}. Raises `Ecto.NoResultsError` if the Source does not exist.
|
||||||
"""
|
"""
|
||||||
def get_source!(id), do: Repo.get!(Source, id)
|
def get_source!(id), do: Repo.get!(Source, id)
|
||||||
|
def get_source(id), do: Repo.get(Source, id)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a source. May attempt to pull additional source details from the
|
Creates a source. May attempt to pull additional source details from the
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ defmodule Pinchflat.Tasks.Task do
|
|||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
|
||||||
alias Pinchflat.Media.MediaItem
|
alias Pinchflat.Media.MediaItem
|
||||||
alias Pinchflat.Sources.Source
|
alias Pinchflat.Sources.Source
|
||||||
|
|
||||||
@@ -34,12 +33,22 @@ end
|
|||||||
defimpl Jason.Encoder, for: Pinchflat.Tasks.Task do
|
defimpl Jason.Encoder, for: Pinchflat.Tasks.Task do
|
||||||
def encode(value, opts) do
|
def encode(value, opts) do
|
||||||
value
|
value
|
||||||
|> Repo.preload(:job)
|
|
||||||
|> Map.drop(Pinchflat.Tasks.Task.json_excluded_fields())
|
|> Map.drop(Pinchflat.Tasks.Task.json_excluded_fields())
|
||||||
|> Map.update!(:job, fn
|
|> Map.update!(:job, fn
|
||||||
nil -> nil
|
nil -> nil
|
||||||
|
%Ecto.Association.NotLoaded{} -> nil
|
||||||
job -> Map.drop(job, [:__meta__, :__struct__, :args, :meta, :worker, :queue])
|
job -> Map.drop(job, [:__meta__, :__struct__, :args, :meta, :worker, :queue])
|
||||||
end)
|
end)
|
||||||
|
|> Map.update!(:source, fn
|
||||||
|
nil -> nil
|
||||||
|
%Ecto.Association.NotLoaded{} -> nil
|
||||||
|
source -> source
|
||||||
|
end)
|
||||||
|
|> Map.update!(:media_item, fn
|
||||||
|
nil -> nil
|
||||||
|
%Ecto.Association.NotLoaded{} -> nil
|
||||||
|
item -> item
|
||||||
|
end)
|
||||||
|> Jason.Encode.map(opts)
|
|> Jason.Encode.map(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ defmodule PinchflatWeb.Api.V1.ApiMediaItemController do
|
|||||||
|
|
||||||
use PinchflatWeb, :controller
|
use PinchflatWeb, :controller
|
||||||
|
|
||||||
|
action_fallback PinchflatWeb.Api.V1.FallbackController
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
alias Pinchflat.Media
|
alias Pinchflat.Media
|
||||||
alias Pinchflat.Media.MediaItem
|
alias Pinchflat.Media.MediaItem
|
||||||
@@ -14,8 +16,7 @@ defmodule PinchflatWeb.Api.V1.ApiMediaItemController do
|
|||||||
media_items =
|
media_items =
|
||||||
case params do
|
case params do
|
||||||
%{"source_id" => source_id} ->
|
%{"source_id" => source_id} ->
|
||||||
source = Pinchflat.Sources.get_source!(source_id)
|
Media.list_media_items_for_source(source_id)
|
||||||
Media.list_pending_media_items_for(source)
|
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
Media.list_media_items()
|
Media.list_media_items()
|
||||||
@@ -26,13 +27,15 @@ defmodule PinchflatWeb.Api.V1.ApiMediaItemController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def show(conn, %{"id" => id}) do
|
def show(conn, %{"id" => id}) do
|
||||||
media_item =
|
case Media.get_media_item(id) do
|
||||||
id
|
nil ->
|
||||||
|> Media.get_media_item!()
|
{:error, :not_found}
|
||||||
|> Repo.preload([:source, tasks: [:job]])
|
|
||||||
|
|
||||||
|
media_item ->
|
||||||
|
media_item = Repo.preload(media_item, [:source, tasks: [:job]])
|
||||||
json(conn, %{data: media_item})
|
json(conn, %{data: media_item})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def search(conn, %{"q" => query}) do
|
def search(conn, %{"q" => query}) do
|
||||||
results = Media.search(query) |> Repo.preload(:source)
|
results = Media.search(query) |> Repo.preload(:source)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ defmodule PinchflatWeb.Api.V1.ApiSourceController do
|
|||||||
|
|
||||||
use PinchflatWeb, :controller
|
use PinchflatWeb, :controller
|
||||||
|
|
||||||
|
action_fallback PinchflatWeb.Api.V1.FallbackController
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
alias Pinchflat.Sources
|
alias Pinchflat.Sources
|
||||||
alias Pinchflat.Sources.Source
|
alias Pinchflat.Sources.Source
|
||||||
@@ -21,7 +23,12 @@ defmodule PinchflatWeb.Api.V1.ApiSourceController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def show(conn, %{"id" => id}) do
|
def show(conn, %{"id" => id}) do
|
||||||
source = Sources.get_source!(id) |> Repo.preload([:media_profile, :media_items])
|
case Sources.get_source(id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
source ->
|
||||||
|
source = Repo.preload(source, [:media_profile, :media_items])
|
||||||
|
|
||||||
pending_tasks =
|
pending_tasks =
|
||||||
source
|
source
|
||||||
@@ -30,6 +37,7 @@ defmodule PinchflatWeb.Api.V1.ApiSourceController do
|
|||||||
|
|
||||||
json(conn, %{data: source, pending_tasks: pending_tasks})
|
json(conn, %{data: source, pending_tasks: pending_tasks})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def create(conn, %{"source" => source_params}) do
|
def create(conn, %{"source" => source_params}) do
|
||||||
case Sources.create_source(source_params) do
|
case Sources.create_source(source_params) do
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ defmodule PinchflatWeb.Api.V1.ApiTaskController do
|
|||||||
|
|
||||||
use PinchflatWeb, :controller
|
use PinchflatWeb, :controller
|
||||||
|
|
||||||
|
action_fallback PinchflatWeb.Api.V1.FallbackController
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
alias Pinchflat.Tasks
|
alias Pinchflat.Tasks
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
defmodule PinchflatWeb.Api.V1.FallbackController do
|
||||||
|
@moduledoc """
|
||||||
|
Translates controller failures into JSON error responses.
|
||||||
|
"""
|
||||||
|
use PinchflatWeb, :controller
|
||||||
|
|
||||||
|
def call(conn, {:error, :not_found}) do
|
||||||
|
conn
|
||||||
|
|> put_status(:not_found)
|
||||||
|
|> json(%{errors: %{detail: "Not found"}})
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(conn, %Ecto.NoResultsError{} = _error) do
|
||||||
|
conn
|
||||||
|
|> put_status(:not_found)
|
||||||
|
|> json(%{errors: %{detail: "Not found"}})
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user