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:
@@ -5,6 +5,8 @@ defmodule PinchflatWeb.Api.V1.ApiMediaItemController do
|
||||
|
||||
use PinchflatWeb, :controller
|
||||
|
||||
action_fallback PinchflatWeb.Api.V1.FallbackController
|
||||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Media.MediaItem
|
||||
@@ -14,8 +16,7 @@ defmodule PinchflatWeb.Api.V1.ApiMediaItemController do
|
||||
media_items =
|
||||
case params do
|
||||
%{"source_id" => source_id} ->
|
||||
source = Pinchflat.Sources.get_source!(source_id)
|
||||
Media.list_pending_media_items_for(source)
|
||||
Media.list_media_items_for_source(source_id)
|
||||
|
||||
_ ->
|
||||
Media.list_media_items()
|
||||
@@ -26,12 +27,14 @@ defmodule PinchflatWeb.Api.V1.ApiMediaItemController do
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
media_item =
|
||||
id
|
||||
|> Media.get_media_item!()
|
||||
|> Repo.preload([:source, tasks: [:job]])
|
||||
case Media.get_media_item(id) do
|
||||
nil ->
|
||||
{:error, :not_found}
|
||||
|
||||
json(conn, %{data: media_item})
|
||||
media_item ->
|
||||
media_item = Repo.preload(media_item, [:source, tasks: [:job]])
|
||||
json(conn, %{data: media_item})
|
||||
end
|
||||
end
|
||||
|
||||
def search(conn, %{"q" => query}) do
|
||||
|
||||
@@ -5,6 +5,8 @@ defmodule PinchflatWeb.Api.V1.ApiSourceController do
|
||||
|
||||
use PinchflatWeb, :controller
|
||||
|
||||
action_fallback PinchflatWeb.Api.V1.FallbackController
|
||||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Sources
|
||||
alias Pinchflat.Sources.Source
|
||||
@@ -21,14 +23,20 @@ defmodule PinchflatWeb.Api.V1.ApiSourceController do
|
||||
end
|
||||
|
||||
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}
|
||||
|
||||
pending_tasks =
|
||||
source
|
||||
|> Tasks.list_tasks_for(nil, [:executing, :available, :scheduled, :retryable])
|
||||
|> Repo.preload(:job)
|
||||
source ->
|
||||
source = Repo.preload(source, [:media_profile, :media_items])
|
||||
|
||||
json(conn, %{data: source, pending_tasks: pending_tasks})
|
||||
pending_tasks =
|
||||
source
|
||||
|> Tasks.list_tasks_for(nil, [:executing, :available, :scheduled, :retryable])
|
||||
|> Repo.preload(:job)
|
||||
|
||||
json(conn, %{data: source, pending_tasks: pending_tasks})
|
||||
end
|
||||
end
|
||||
|
||||
def create(conn, %{"source" => source_params}) do
|
||||
|
||||
@@ -5,6 +5,8 @@ defmodule PinchflatWeb.Api.V1.ApiTaskController do
|
||||
|
||||
use PinchflatWeb, :controller
|
||||
|
||||
action_fallback PinchflatWeb.Api.V1.FallbackController
|
||||
|
||||
alias Pinchflat.Repo
|
||||
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