Adds DecoController (groups + decos CRUD + full-state PUT), playlist and
artist/multi-collection search picker wrappers, a PlaylistController for the
break-content playlist cascade, and PUT /api/playouts/{id}/deco. Fixes the
CreateDecoHandler missing deco-group existence check (FK 500 -> 422), matching
the CreateBlock/CreateTemplate precedent. ReplaceDecoRequest.ToCommand rejects
Merge on the non-mergeable sections (422).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
299 lines
13 KiB
C#
299 lines
13 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using ErsatzTV.Application.Playouts;
|
|
using ErsatzTV.Application.Scheduling;
|
|
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 playout")]
|
|
[EndpointDescription(
|
|
"Creates a playout of any kind (Classic, Block, Sequential, Scripted, or ExternalJson) for a channel. " +
|
|
"Classic requires ProgramScheduleId; Sequential/Scripted/ExternalJson require ScheduleFile; Block requires " +
|
|
"neither. A channel may only have one 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, CreatePlayout> commandOrError = request.ToCommand();
|
|
return await commandOrError.Match(
|
|
Left: error => Task.FromResult(error.ToErrorResult()),
|
|
Right: async command =>
|
|
{
|
|
Either<BaseError, CreatePlayoutResponse> result = await mediator.Send(command, 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());
|
|
});
|
|
});
|
|
}
|
|
|
|
[HttpPut("/api/playouts/{id:int}")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Update playout scheduling details")]
|
|
[EndpointDescription(
|
|
"DailyRebuildTime is always applied; omit it (null) to clear the daily reset. ScheduleFile is only valid " +
|
|
"for Sequential, Scripted, and ExternalJson playouts; omit it (null) to leave it unchanged.")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PlayoutResponseModel), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
|
|
public async Task<IActionResult> Update(
|
|
int id,
|
|
[Required] [FromBody] UpdatePlayoutDetailsRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Option<PlayoutNameViewModel> maybePlayout = await mediator.Send(new GetPlayoutById(id), cancellationToken);
|
|
if (maybePlayout.IsNone)
|
|
{
|
|
return ApiResults.NotFoundProblem();
|
|
}
|
|
|
|
var hasScheduleFile = !string.IsNullOrWhiteSpace(request.ScheduleFile);
|
|
foreach (PlayoutNameViewModel playout in maybePlayout)
|
|
{
|
|
if (hasScheduleFile && playout.ScheduleKind is not (PlayoutScheduleKind.Sequential
|
|
or PlayoutScheduleKind.Scripted or PlayoutScheduleKind.ExternalJson))
|
|
{
|
|
BaseError error =
|
|
BaseError.New("[ScheduleFile] is only valid for Sequential, Scripted, or ExternalJson playouts");
|
|
return error.ToErrorResult();
|
|
}
|
|
}
|
|
|
|
// the schedule-file update is the only step that can fail after the pre-checks,
|
|
// so it goes first — a rejected file must not leave DailyRebuildTime applied
|
|
if (hasScheduleFile)
|
|
{
|
|
foreach (PlayoutNameViewModel playout in maybePlayout)
|
|
{
|
|
Either<BaseError, PlayoutNameViewModel> scheduleFileResult =
|
|
await UpdateScheduleFile(playout, request.ScheduleFile, cancellationToken);
|
|
foreach (BaseError error in scheduleFileResult.LeftToSeq())
|
|
{
|
|
return error.ToErrorResult();
|
|
}
|
|
}
|
|
}
|
|
|
|
Option<TimeSpan> dailyRebuildTime = request.DailyRebuildTime is { } t ? Some(t) : Option<TimeSpan>.None;
|
|
Either<BaseError, PlayoutNameViewModel> result =
|
|
await mediator.Send(new UpdatePlayout(id, dailyRebuildTime), cancellationToken);
|
|
|
|
return result.Match(
|
|
Left: error => error.ToErrorResult(),
|
|
Right: playout => (IActionResult)new OkObjectResult(ToResponse(playout)));
|
|
}
|
|
|
|
private async Task<Either<BaseError, PlayoutNameViewModel>> UpdateScheduleFile(
|
|
PlayoutNameViewModel playout,
|
|
string scheduleFile,
|
|
CancellationToken cancellationToken) =>
|
|
playout.ScheduleKind switch
|
|
{
|
|
PlayoutScheduleKind.Sequential => await mediator.Send(
|
|
new UpdateSequentialPlayout(playout.PlayoutId, scheduleFile),
|
|
cancellationToken),
|
|
PlayoutScheduleKind.Scripted => await mediator.Send(
|
|
new UpdateScriptedPlayout(playout.PlayoutId, scheduleFile),
|
|
cancellationToken),
|
|
PlayoutScheduleKind.ExternalJson => await mediator.Send(
|
|
new UpdateExternalJsonPlayout(playout.PlayoutId, scheduleFile),
|
|
cancellationToken),
|
|
_ => BaseError.New("[ScheduleFile] is only valid for Sequential, Scripted, or ExternalJson playouts")
|
|
};
|
|
|
|
[HttpPut("/api/playouts/{id:int}/deco")]
|
|
[Tags("Playouts")]
|
|
[EndpointSummary("Set (or clear) a playout's default deco")]
|
|
[EndpointDescription("Assigns the default deco for a block playout. Send a null decoId to clear it.")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PlayoutResponseModel), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
|
|
public async Task<IActionResult> UpdateDefaultDeco(
|
|
int id,
|
|
[Required] [FromBody] UpdateDefaultDecoRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Option<PlayoutNameViewModel> maybePlayout = await mediator.Send(new GetPlayoutById(id), cancellationToken);
|
|
if (maybePlayout.IsNone)
|
|
{
|
|
return ApiResults.NotFoundProblem();
|
|
}
|
|
|
|
if (request.DecoId is { } decoId)
|
|
{
|
|
Option<DecoViewModel> maybeDeco = await mediator.Send(new GetDecoById(decoId), cancellationToken);
|
|
if (maybeDeco.IsNone)
|
|
{
|
|
return BaseError.New("Deco does not exist").ToErrorResult();
|
|
}
|
|
}
|
|
|
|
Option<BaseError> result = await mediator.Send(request.ToCommand(id), cancellationToken);
|
|
if (result.IsSome)
|
|
{
|
|
foreach (BaseError error in result)
|
|
{
|
|
return error.ToErrorResult();
|
|
}
|
|
}
|
|
|
|
Option<PlayoutNameViewModel> refreshed = await mediator.Send(new GetPlayoutById(id), cancellationToken);
|
|
return refreshed.Match(
|
|
Some: vm => (IActionResult)new OkObjectResult(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));
|
|
}
|