- GET /api/playouts — paged list over GetPagedPlayouts (query/pageNum/pageSize), list DTO with buildStatus (#100, #107) - GET /api/playouts/{id}/items — paged future items + UNSCHEDULED gaps over GetFuturePlayoutItemsById; 404 ProblemDetails for unknown playout (matches /api/schedules/{id}/items precedent) (#101) - buildStatus {lastBuild, success, message} on GET /api/playouts/{id} (BuildStatus now included by GetPlayoutByIdHandler) (#107) - GET /api/playouts/warnings/count — failed-build count for the warnings badge (#107) - POST /api/playouts/reset-all — 202 Accepted, wraps ResetAllPlayouts (#110) - POST /api/channels/{channelNumber}/playout/reset — optional ?mode= override; default branches by ScheduleKind (Classic→Refresh, others→Reset) to match Blazor semantics (#110) - Option<FillerKind>→FillerKind? projection uses MatchUnsafe (Match throws on null-returning branch; idiom per Health/Mapper.cs) - Tests: controller unit tests (routes, projections, 404s, reset-mode defaults), OpenAPI ProblemDetails contract entry for /api/playouts/{id}/items get 404 - Regenerated wwwroot/openapi/v1.json Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
170 lines
6.9 KiB
C#
170 lines
6.9 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using ErsatzTV.Application.Playouts;
|
|
using ErsatzTV.Controllers.Api.Requests;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Api.Playouts;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Domain.Filler;
|
|
using ErsatzTV.Extensions;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace ErsatzTV.Controllers.Api;
|
|
|
|
[ApiController]
|
|
public class PlayoutController(IMediator mediator) : ControllerBase
|
|
{
|
|
[HttpGet("/api/playouts", Name = "GetPlayouts")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("List playouts")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PagedPlayoutsResponseModel), StatusCodes.Status200OK)]
|
|
public async Task<PagedPlayoutsResponseModel> GetAll(
|
|
[FromQuery] string query = "",
|
|
[FromQuery] int pageNum = 0,
|
|
[FromQuery] int pageSize = 100,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
PagedPlayoutsViewModel result =
|
|
await mediator.Send(new GetPagedPlayouts(query, pageNum, pageSize), cancellationToken);
|
|
return new PagedPlayoutsResponseModel(
|
|
result.TotalCount,
|
|
result.Page.Map(ToListItemResponse).ToList());
|
|
}
|
|
|
|
[HttpGet("/api/playouts/warnings/count", Name = "GetPlayoutWarningsCount")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Count playouts with a failed build")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
public async Task<int> GetWarningsCount(CancellationToken cancellationToken) =>
|
|
await mediator.Send(new GetPlayoutWarningsCount(), cancellationToken);
|
|
|
|
[HttpGet("/api/playouts/{id:int}", Name = "GetPlayoutById")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Get a playout by id")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PlayoutResponseModel), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
|
|
{
|
|
Option<PlayoutNameViewModel> result = await mediator.Send(new GetPlayoutById(id), cancellationToken);
|
|
return result.Map(ToResponse).ToGetResult();
|
|
}
|
|
|
|
[HttpGet("/api/playouts/{id:int}/items", Name = "GetPlayoutItems")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Get upcoming items (and unscheduled gaps) for a playout")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PagedPlayoutItemsResponseModel), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetItems(
|
|
int id,
|
|
[FromQuery] bool showFiller = false,
|
|
[FromQuery] int pageNum = 0,
|
|
[FromQuery] int pageSize = 100,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
Option<PlayoutNameViewModel> maybePlayout = await mediator.Send(new GetPlayoutById(id), cancellationToken);
|
|
if (maybePlayout.IsNone)
|
|
{
|
|
return ApiResults.NotFoundProblem();
|
|
}
|
|
|
|
PagedPlayoutItemsViewModel result = await mediator.Send(
|
|
new GetFuturePlayoutItemsById(id, showFiller, pageNum, pageSize),
|
|
cancellationToken);
|
|
return new OkObjectResult(
|
|
new PagedPlayoutItemsResponseModel(
|
|
result.TotalCount,
|
|
result.Page.Map(ToItemResponse).ToList()));
|
|
}
|
|
|
|
[HttpPost("/api/playouts")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Create a classic playout")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PlayoutResponseModel), StatusCodes.Status201Created)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
|
|
public async Task<IActionResult> Create(
|
|
[Required] [FromBody] CreatePlayoutRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Either<BaseError, CreatePlayoutResponse> result = await mediator.Send(request.ToCommand(), cancellationToken);
|
|
return await result.Match(
|
|
Left: error => Task.FromResult(error.ToErrorResult()),
|
|
Right: async created =>
|
|
{
|
|
Option<PlayoutNameViewModel> playout =
|
|
await mediator.Send(new GetPlayoutById(created.PlayoutId), cancellationToken);
|
|
return playout.Match(
|
|
Some: vm => (IActionResult)new CreatedResult($"/api/playouts/{vm.PlayoutId}", ToResponse(vm)),
|
|
None: () => ApiResults.NotFoundProblem());
|
|
});
|
|
}
|
|
|
|
[HttpPost("/api/playouts/reset-all", Name = "ResetAllPlayouts")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Reset all playouts")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(StatusCodes.Status202Accepted)]
|
|
public async Task<IActionResult> ResetAll(CancellationToken cancellationToken)
|
|
{
|
|
await mediator.Send(new ResetAllPlayouts(), cancellationToken);
|
|
return Accepted();
|
|
}
|
|
|
|
[HttpDelete("/api/playouts/{id:int}")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Delete a playout")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
|
|
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
|
|
{
|
|
Either<BaseError, Unit> result = await mediator.Send(new DeletePlayout(id), cancellationToken);
|
|
return result.ToDeletedResult();
|
|
}
|
|
|
|
private static PlayoutResponseModel ToResponse(PlayoutNameViewModel vm) =>
|
|
PlayoutResponseModel.From(
|
|
vm.PlayoutId,
|
|
vm.ScheduleKind,
|
|
vm.ChannelName,
|
|
vm.ChannelNumber,
|
|
vm.PlayoutMode,
|
|
vm.ScheduleName,
|
|
vm.ScheduleFile,
|
|
vm.DbDailyRebuildTime,
|
|
ToBuildStatus(vm.BuildStatus));
|
|
|
|
private static PlayoutListItemResponseModel ToListItemResponse(PlayoutNameViewModel vm) =>
|
|
new(
|
|
vm.PlayoutId,
|
|
vm.ChannelNumber,
|
|
vm.ChannelName,
|
|
vm.ScheduleKind,
|
|
vm.ScheduleName,
|
|
vm.DbDailyRebuildTime,
|
|
ToBuildStatus(vm.BuildStatus));
|
|
|
|
private static PlayoutBuildStatusResponseModel ToBuildStatus(PlayoutBuildStatus buildStatus) =>
|
|
buildStatus is null
|
|
? null
|
|
: new PlayoutBuildStatusResponseModel(
|
|
buildStatus.LastBuild,
|
|
buildStatus.Success,
|
|
buildStatus.Message);
|
|
|
|
private static PlayoutItemResponseModel ToItemResponse(PlayoutItemViewModel vm) =>
|
|
new(
|
|
vm.Title,
|
|
vm.Start,
|
|
vm.Finish,
|
|
vm.Duration,
|
|
vm.FillerKind.MatchUnsafe(fk => (FillerKind?)fk, () => null));
|
|
}
|