Files
ersatztv/ErsatzTV/Controllers/Api/SearchController.cs
T
timothyandClaude Opus 4.8 ef2bd65c27
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 10s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 10s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 1m12s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 3m4s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m17s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 10m36s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
feat(api): #286 — mount the whole /api surface at /api/v1
Version every /api route to /api/v1 (251 controller routes + ~24 Location
headers + the scanner callback URL + the Startup request-log literal),
uniform across the machine API, auth, scanner and scripted-build surfaces.

Add ApiVersionRewriteMiddleware: a legacy unversioned /api/* request is
rewritten (NOT redirected) to /api/v1/* in-pipeline — method, body, auth
headers and query survive — carrying RFC 8594 Deprecation/Sunset headers,
so curl / the future MCP server / bookmarks keep working. An already-
versioned path passes through; a future /api/v2 is never forced to v1.

Standardize the route convention (leading-slash absolute route per method,
no class-[Route] — except the two Scanner/Scripted controllers whose ~all
actions share a parametrized {id} prefix), enforced by ApiRouteVersioningTests
(^/api/v\d+/ over the whole Controllers.Api surface; browser-nav
/auth/oidc/login is out of scope).

Regenerate v1.json (160 paths, all /api/v1)/endpoint-index/v1.d.ts; sweep 945
SPA request literals + the test mocks (regex + positional URL parsers). /api/v1
is additive-only after freeze; the legacy-rewrite shim sunsets in ~2 releases
(owner decision) with removal tracked as a Phase-3 follow-up.

Docs: decisions.md 2026-07-13, api-conventions §1/§9, rest-api/spa-conventions/
blazor-route-parity/e2e-local/domain-model.

fixes #286
refs #197

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 00:30:20 +02:00

177 lines
8.0 KiB
C#

using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Application.MediaItems;
using ErsatzTV.Application.Search;
using ErsatzTV.Core;
using ErsatzTV.Core.Api.Scheduling;
using ErsatzTV.Core.Api.Search;
using ErsatzTV.Extensions;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ErsatzTV.Controllers.Api;
[ApiController]
public class SearchController(IMediator mediator) : ControllerBase
{
private const int MaxPageSize = 100;
[HttpGet("/api/v1/search", Name = "Search")]
[Tags("Search")]
[EndpointSummary("Search library items across all media kinds")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(SearchResultsResponseModel), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
public async Task<IActionResult> Search(
[FromQuery] string query = "",
[FromQuery] int pageNum = 0,
[FromQuery] int pageSize = 50,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(query))
{
return BaseError.New("A non-empty query is required").ToErrorResult();
}
int clampedPageNum = Math.Max(0, pageNum);
int clampedPageSize = Math.Clamp(pageSize, 1, MaxPageSize);
SearchResultsResponseModel result = await mediator.Send(
new GetSearchResults(query, clampedPageNum, clampedPageSize),
cancellationToken);
return new OkObjectResult(result);
}
[HttpGet("/api/v1/search/all-items", Name = "SearchAllItems")]
[Tags("Search")]
[EndpointSummary("Search library items across all media kinds and return raw id lists")]
[EndpointDescription(
"Returns every matching item's id, grouped by media kind, with no paging. Used by the SPA's " +
"\"add all to collection/playlist\" flow to materialize ids before calling the add endpoints.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(SearchResultAllItemsResponseModel), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
public async Task<IActionResult> SearchAllItems(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(query))
{
return BaseError.New("A non-empty query is required").ToErrorResult();
}
SearchResultAllItemsViewModel result = await mediator.Send(
new QuerySearchIndexAllItems(query),
cancellationToken);
return new OkObjectResult(Project(result));
}
[HttpGet("/api/v1/search/collections", Name = "SearchCollections")]
[Tags("Search")]
[EndpointSummary("Search collections by name")]
[EndpointDescription("Returns matching collections as {id, name} options for scheduling editors.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<SchedulingPickerOptionResponseModel>), StatusCodes.Status200OK)]
public async Task<List<SchedulingPickerOptionResponseModel>> SearchCollections(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
List<MediaCollectionViewModel> results = await mediator.Send(
new SearchCollections(query ?? string.Empty),
cancellationToken);
return results.Map(c => new SchedulingPickerOptionResponseModel(c.Id, c.Name)).ToList();
}
[HttpGet("/api/v1/search/television-shows", Name = "SearchTelevisionShows")]
[Tags("Search")]
[EndpointSummary("Search television shows by name")]
[EndpointDescription("Returns matching television shows as {id, name} options for scheduling editors.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<SchedulingPickerOptionResponseModel>), StatusCodes.Status200OK)]
public async Task<List<SchedulingPickerOptionResponseModel>> SearchTelevisionShows(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
List<NamedMediaItemViewModel> results = await mediator.Send(
new SearchTelevisionShows(query ?? string.Empty),
cancellationToken);
return results.Map(s => new SchedulingPickerOptionResponseModel(s.MediaItemId, s.Name)).ToList();
}
[HttpGet("/api/v1/search/television-seasons", Name = "SearchTelevisionSeasons")]
[Tags("Search")]
[EndpointSummary("Search television seasons by name")]
[EndpointDescription("Returns matching television seasons as {id, name} options for scheduling editors.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<SchedulingPickerOptionResponseModel>), StatusCodes.Status200OK)]
public async Task<List<SchedulingPickerOptionResponseModel>> SearchTelevisionSeasons(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
List<NamedMediaItemViewModel> results = await mediator.Send(
new SearchTelevisionSeasons(query ?? string.Empty),
cancellationToken);
return results.Map(s => new SchedulingPickerOptionResponseModel(s.MediaItemId, s.Name)).ToList();
}
[HttpGet("/api/v1/search/smart-collections", Name = "SearchSmartCollections")]
[Tags("Search")]
[EndpointSummary("Search smart collections by name")]
[EndpointDescription("Returns matching smart collections as {id, name} options for scheduling editors.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<SchedulingPickerOptionResponseModel>), StatusCodes.Status200OK)]
public async Task<List<SchedulingPickerOptionResponseModel>> SearchSmartCollections(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
List<SmartCollectionViewModel> results = await mediator.Send(
new SearchSmartCollections(query ?? string.Empty),
cancellationToken);
return results.Map(c => new SchedulingPickerOptionResponseModel(c.Id, c.Name)).ToList();
}
[HttpGet("/api/v1/search/artists", Name = "SearchArtists")]
[Tags("Search")]
[EndpointSummary("Search artists by name")]
[EndpointDescription("Returns matching artists as {id, name} options for scheduling editors.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<SchedulingPickerOptionResponseModel>), StatusCodes.Status200OK)]
public async Task<List<SchedulingPickerOptionResponseModel>> SearchArtists(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
List<NamedMediaItemViewModel> results = await mediator.Send(
new SearchArtists(query ?? string.Empty),
cancellationToken);
return results.Map(a => new SchedulingPickerOptionResponseModel(a.MediaItemId, a.Name)).ToList();
}
[HttpGet("/api/v1/search/multi-collections", Name = "SearchMultiCollections")]
[Tags("Search")]
[EndpointSummary("Search multi collections by name")]
[EndpointDescription("Returns matching multi collections as {id, name} options for scheduling editors.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<SchedulingPickerOptionResponseModel>), StatusCodes.Status200OK)]
public async Task<List<SchedulingPickerOptionResponseModel>> SearchMultiCollections(
[FromQuery] string query = "",
CancellationToken cancellationToken = default)
{
List<MultiCollectionViewModel> results = await mediator.Send(
new SearchMultiCollections(query ?? string.Empty),
cancellationToken);
return results.Map(c => new SchedulingPickerOptionResponseModel(c.Id, c.Name)).ToList();
}
private static SearchResultAllItemsResponseModel Project(SearchResultAllItemsViewModel vm) =>
new(
vm.MovieIds,
vm.ShowIds,
vm.SeasonIds,
vm.EpisodeIds,
vm.ArtistIds,
vm.MusicVideoIds,
vm.OtherVideoIds,
vm.SongIds,
vm.ImageIds,
vm.RemoteStreamIds);
}