From 20ca71b388740db79343e9a16c6d9c7b8c4ef5de Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 3 Jul 2026 22:18:19 +0200 Subject: [PATCH] feat(api): playout read endpoints + reset-all (#100 #101 #107 #110) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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? 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 --- .../Playouts/Queries/GetPlayoutByIdHandler.cs | 1 + .../PagedPlayoutItemsResponseModel.cs | 5 + .../Playouts/PagedPlayoutsResponseModel.cs | 5 + .../PlayoutBuildStatusResponseModel.cs | 6 + .../Api/Playouts/PlayoutItemResponseModel.cs | 10 + .../Playouts/PlayoutListItemResponseModel.cs | 13 + .../Api/Playouts/PlayoutResponseModel.cs | 14 +- .../Controllers/ChannelControllerTests.cs | 56 +- .../OpenApiErrorResponseContractTests.cs | 1 + .../Controllers/PlayoutControllerTests.cs | 137 ++++- ErsatzTV/Controllers/Api/ChannelController.cs | 31 +- ErsatzTV/Controllers/Api/PlayoutController.cs | 96 +++- ErsatzTV/wwwroot/openapi/v1.json | 495 +++++++++++++++--- 13 files changed, 792 insertions(+), 78 deletions(-) create mode 100644 ErsatzTV.Core/Api/Playouts/PagedPlayoutItemsResponseModel.cs create mode 100644 ErsatzTV.Core/Api/Playouts/PagedPlayoutsResponseModel.cs create mode 100644 ErsatzTV.Core/Api/Playouts/PlayoutBuildStatusResponseModel.cs create mode 100644 ErsatzTV.Core/Api/Playouts/PlayoutItemResponseModel.cs create mode 100644 ErsatzTV.Core/Api/Playouts/PlayoutListItemResponseModel.cs diff --git a/ErsatzTV.Application/Playouts/Queries/GetPlayoutByIdHandler.cs b/ErsatzTV.Application/Playouts/Queries/GetPlayoutByIdHandler.cs index e47f91433..b765ff7dd 100644 --- a/ErsatzTV.Application/Playouts/Queries/GetPlayoutByIdHandler.cs +++ b/ErsatzTV.Application/Playouts/Queries/GetPlayoutByIdHandler.cs @@ -16,6 +16,7 @@ public class GetPlayoutByIdHandler(IDbContextFactory dbContextFactory .AsNoTracking() .Include(p => p.ProgramSchedule) .Include(p => p.Channel) + .Include(p => p.BuildStatus) .SelectOneAsync(p => p.Id, p => p.Id == request.PlayoutId, cancellationToken) .MapT(p => new PlayoutNameViewModel( p.Id, diff --git a/ErsatzTV.Core/Api/Playouts/PagedPlayoutItemsResponseModel.cs b/ErsatzTV.Core/Api/Playouts/PagedPlayoutItemsResponseModel.cs new file mode 100644 index 000000000..be6249ef1 --- /dev/null +++ b/ErsatzTV.Core/Api/Playouts/PagedPlayoutItemsResponseModel.cs @@ -0,0 +1,5 @@ +namespace ErsatzTV.Core.Api.Playouts; + +public record PagedPlayoutItemsResponseModel( + int TotalCount, + List Page); diff --git a/ErsatzTV.Core/Api/Playouts/PagedPlayoutsResponseModel.cs b/ErsatzTV.Core/Api/Playouts/PagedPlayoutsResponseModel.cs new file mode 100644 index 000000000..5473e67d2 --- /dev/null +++ b/ErsatzTV.Core/Api/Playouts/PagedPlayoutsResponseModel.cs @@ -0,0 +1,5 @@ +namespace ErsatzTV.Core.Api.Playouts; + +public record PagedPlayoutsResponseModel( + int TotalCount, + List Page); diff --git a/ErsatzTV.Core/Api/Playouts/PlayoutBuildStatusResponseModel.cs b/ErsatzTV.Core/Api/Playouts/PlayoutBuildStatusResponseModel.cs new file mode 100644 index 000000000..5410c9d23 --- /dev/null +++ b/ErsatzTV.Core/Api/Playouts/PlayoutBuildStatusResponseModel.cs @@ -0,0 +1,6 @@ +namespace ErsatzTV.Core.Api.Playouts; + +public record PlayoutBuildStatusResponseModel( + DateTimeOffset LastBuild, + bool Success, + string Message); diff --git a/ErsatzTV.Core/Api/Playouts/PlayoutItemResponseModel.cs b/ErsatzTV.Core/Api/Playouts/PlayoutItemResponseModel.cs new file mode 100644 index 000000000..ab9c7104e --- /dev/null +++ b/ErsatzTV.Core/Api/Playouts/PlayoutItemResponseModel.cs @@ -0,0 +1,10 @@ +using ErsatzTV.Core.Domain.Filler; + +namespace ErsatzTV.Core.Api.Playouts; + +public record PlayoutItemResponseModel( + string Title, + DateTimeOffset Start, + DateTimeOffset Finish, + string Duration, + FillerKind? FillerKind); diff --git a/ErsatzTV.Core/Api/Playouts/PlayoutListItemResponseModel.cs b/ErsatzTV.Core/Api/Playouts/PlayoutListItemResponseModel.cs new file mode 100644 index 000000000..b7caa66ce --- /dev/null +++ b/ErsatzTV.Core/Api/Playouts/PlayoutListItemResponseModel.cs @@ -0,0 +1,13 @@ +#nullable enable +using ErsatzTV.Core.Domain; + +namespace ErsatzTV.Core.Api.Playouts; + +public record PlayoutListItemResponseModel( + int Id, + string ChannelNumber, + string ChannelName, + PlayoutScheduleKind ScheduleKind, + string ScheduleName, + TimeSpan? DailyRebuildTime, + PlayoutBuildStatusResponseModel? BuildStatus); diff --git a/ErsatzTV.Core/Api/Playouts/PlayoutResponseModel.cs b/ErsatzTV.Core/Api/Playouts/PlayoutResponseModel.cs index a80fd6ed0..cd382a278 100644 --- a/ErsatzTV.Core/Api/Playouts/PlayoutResponseModel.cs +++ b/ErsatzTV.Core/Api/Playouts/PlayoutResponseModel.cs @@ -1,3 +1,4 @@ +#nullable enable using ErsatzTV.Core.Domain; namespace ErsatzTV.Core.Api.Playouts; @@ -9,8 +10,9 @@ public record PlayoutResponseModel( string ChannelNumber, ChannelPlayoutMode PlayoutMode, string ScheduleName, - string ScheduleFile, - TimeSpan? DailyRebuildTime) + string? ScheduleFile, + TimeSpan? DailyRebuildTime, + PlayoutBuildStatusResponseModel? BuildStatus) { public static PlayoutResponseModel From( int id, @@ -19,8 +21,9 @@ public record PlayoutResponseModel( string channelNumber, ChannelPlayoutMode playoutMode, string scheduleName, - string scheduleFile, - TimeSpan? dailyRebuildTime) => + string? scheduleFile, + TimeSpan? dailyRebuildTime, + PlayoutBuildStatusResponseModel? buildStatus) => new( id, scheduleKind, @@ -29,5 +32,6 @@ public record PlayoutResponseModel( playoutMode, scheduleName, scheduleFile, - dailyRebuildTime); + dailyRebuildTime, + buildStatus); } diff --git a/ErsatzTV.Tests/Controllers/ChannelControllerTests.cs b/ErsatzTV.Tests/Controllers/ChannelControllerTests.cs index b8cf0ec1f..1a03ca2e0 100644 --- a/ErsatzTV.Tests/Controllers/ChannelControllerTests.cs +++ b/ErsatzTV.Tests/Controllers/ChannelControllerTests.cs @@ -8,6 +8,7 @@ using ErsatzTV.Controllers.Api.Requests; using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Errors; +using ErsatzTV.Core.Scheduling; using LanguageExt; using static LanguageExt.Prelude; using MediatR; @@ -23,15 +24,15 @@ namespace ErsatzTV.Tests.Controllers; public class ChannelControllerTests { private IMediator _mediator = null!; + private Channel _workerChannel = null!; private ChannelController _controller = null!; [SetUp] public void SetUp() { _mediator = Substitute.For(); - ChannelWriter writer = - System.Threading.Channels.Channel.CreateUnbounded().Writer; - _controller = new ChannelController(writer, _mediator); + _workerChannel = System.Threading.Channels.Channel.CreateUnbounded(); + _controller = new ChannelController(_workerChannel.Writer, _mediator); } [Test] @@ -157,7 +158,7 @@ public class ChannelControllerTests _mediator.Send(Arg.Any(), Arg.Any()) .Returns(Option.None); - IActionResult result = await _controller.ResetPlayout("404"); + IActionResult result = await _controller.ResetPlayout("404", mode: null, CancellationToken.None); var notFound = result.ShouldBeOfType(); var problemDetails = notFound.Value.ShouldBeOfType(); @@ -165,6 +166,53 @@ public class ChannelControllerTests problemDetails.Title.ShouldBe("Resource not found"); } + [TestCase(PlayoutScheduleKind.Classic, PlayoutBuildMode.Refresh)] + [TestCase(PlayoutScheduleKind.Block, PlayoutBuildMode.Reset)] + [TestCase(PlayoutScheduleKind.Sequential, PlayoutBuildMode.Reset)] + public async Task ResetPlayout_Should_Default_Mode_By_ScheduleKind( + PlayoutScheduleKind scheduleKind, + PlayoutBuildMode expectedMode) + { + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(Option.Some(9)); + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(Option.Some(MakePlayout(9, scheduleKind))); + + IActionResult result = await _controller.ResetPlayout("5", mode: null, CancellationToken.None); + + result.ShouldBeOfType(); + _workerChannel.Reader.TryRead(out IBackgroundServiceRequest? request).ShouldBeTrue(); + var buildPlayout = request.ShouldBeOfType(); + buildPlayout.PlayoutId.ShouldBe(9); + buildPlayout.Mode.ShouldBe(expectedMode); + } + + [Test] + public async Task ResetPlayout_Should_Honor_Explicit_Mode() + { + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(Option.Some(9)); + + IActionResult result = await _controller.ResetPlayout("5", PlayoutBuildMode.Continue, CancellationToken.None); + + result.ShouldBeOfType(); + _workerChannel.Reader.TryRead(out IBackgroundServiceRequest? request).ShouldBeTrue(); + request.ShouldBeOfType().Mode.ShouldBe(PlayoutBuildMode.Continue); + await _mediator.DidNotReceive().Send(Arg.Any(), Arg.Any()); + } + + private static PlayoutNameViewModel MakePlayout(int id, PlayoutScheduleKind scheduleKind) => + new( + id, + scheduleKind, + "Channel", + "5", + ChannelPlayoutMode.Continuous, + "Schedule", + string.Empty, + null, + null); + private static ChannelViewModel MakeVm(int id) => new( id, diff --git a/ErsatzTV.Tests/Controllers/OpenApiErrorResponseContractTests.cs b/ErsatzTV.Tests/Controllers/OpenApiErrorResponseContractTests.cs index 71854b956..e0222045d 100644 --- a/ErsatzTV.Tests/Controllers/OpenApiErrorResponseContractTests.cs +++ b/ErsatzTV.Tests/Controllers/OpenApiErrorResponseContractTests.cs @@ -68,6 +68,7 @@ public class OpenApiErrorResponseContractTests [TestCase("/api/playouts", "post", "422")] [TestCase("/api/playouts/{id}", "delete", "404")] [TestCase("/api/playouts/{id}", "delete", "422")] + [TestCase("/api/playouts/{id}/items", "get", "404")] [TestCase("/api/ffmpeg/profiles/{id}", "get", "404")] [TestCase("/api/ffmpeg/profiles", "post", "404")] [TestCase("/api/ffmpeg/profiles", "post", "401")] diff --git a/ErsatzTV.Tests/Controllers/PlayoutControllerTests.cs b/ErsatzTV.Tests/Controllers/PlayoutControllerTests.cs index 0726e9a6f..054fcdc43 100644 --- a/ErsatzTV.Tests/Controllers/PlayoutControllerTests.cs +++ b/ErsatzTV.Tests/Controllers/PlayoutControllerTests.cs @@ -5,6 +5,7 @@ using ErsatzTV.Controllers.Api.Requests; using ErsatzTV.Core; using ErsatzTV.Core.Api.Playouts; using ErsatzTV.Core.Domain; +using ErsatzTV.Core.Domain.Filler; using ErsatzTV.Core.Errors; using LanguageExt; using MediatR; @@ -34,8 +35,12 @@ public class PlayoutControllerTests [Test] public void Controller_Should_Expose_Idiomatic_Rest_Routes() { + ShouldHaveActionRoute(nameof(PlayoutController.GetAll), "GET", "/api/playouts"); ShouldHaveActionRoute(nameof(PlayoutController.GetById), "GET", "/api/playouts/{id:int}"); + ShouldHaveActionRoute(nameof(PlayoutController.GetItems), "GET", "/api/playouts/{id:int}/items"); + ShouldHaveActionRoute(nameof(PlayoutController.GetWarningsCount), "GET", "/api/playouts/warnings/count"); ShouldHaveActionRoute(nameof(PlayoutController.Create), "POST", "/api/playouts"); + ShouldHaveActionRoute(nameof(PlayoutController.ResetAll), "POST", "/api/playouts/reset-all"); ShouldHaveActionRoute(nameof(PlayoutController.Delete), "DELETE", "/api/playouts/{id:int}"); } @@ -162,6 +167,130 @@ public class PlayoutControllerTests problem.Title.ShouldBe("Resource not found"); } + [Test] + public async Task GetAll_Should_Project_Paged_List_With_BuildStatus() + { + var buildStatus = new PlayoutBuildStatus + { + LastBuild = new DateTimeOffset(2026, 7, 2, 10, 0, 0, TimeSpan.Zero), + Success = false, + Message = "boom" + }; + PlayoutNameViewModel vm = MakePlayout(9) with + { + BuildStatus = buildStatus, + DbDailyRebuildTime = TimeSpan.FromHours(4) + }; + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(new PagedPlayoutsViewModel(1, [vm])); + + PagedPlayoutsResponseModel result = await _controller.GetAll("q", 2, 25, CancellationToken.None); + + result.TotalCount.ShouldBe(1); + PlayoutListItemResponseModel item = result.Page.Single(); + item.Id.ShouldBe(9); + item.ChannelNumber.ShouldBe("101"); + item.ChannelName.ShouldBe("Channel"); + item.ScheduleKind.ShouldBe(PlayoutScheduleKind.Classic); + item.ScheduleName.ShouldBe("Schedule"); + item.DailyRebuildTime.ShouldBe(TimeSpan.FromHours(4)); + item.BuildStatus.ShouldNotBeNull(); + item.BuildStatus.Success.ShouldBeFalse(); + item.BuildStatus.Message.ShouldBe("boom"); + item.BuildStatus.LastBuild.ShouldBe(buildStatus.LastBuild); + + await _mediator.Received(1).Send( + Arg.Is(q => q.Query == "q" && q.PageNum == 2 && q.PageSize == 25), + Arg.Any()); + } + + [Test] + public async Task GetAll_Should_Emit_Null_BuildStatus_When_Absent() + { + PlayoutNameViewModel vm = MakePlayout(9) with { BuildStatus = null }; + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(new PagedPlayoutsViewModel(1, [vm])); + + PagedPlayoutsResponseModel result = await _controller.GetAll("", 0, 100, CancellationToken.None); + + result.Page.Single().BuildStatus.ShouldBeNull(); + } + + [Test] + public async Task GetItems_Should_Project_Items_And_Null_FillerKind_For_Gaps() + { + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(Option.Some(MakePlayout(9))); + + var item = new PlayoutItemViewModel( + "Movie", + new DateTimeOffset(2026, 7, 2, 12, 0, 0, TimeSpan.Zero), + new DateTimeOffset(2026, 7, 2, 13, 0, 0, TimeSpan.Zero), + "1:00:00", + string.Empty, + Some(FillerKind.MidRoll)); + var gap = new PlayoutItemViewModel( + "UNSCHEDULED", + new DateTimeOffset(2026, 7, 2, 13, 0, 0, TimeSpan.Zero), + new DateTimeOffset(2026, 7, 2, 13, 30, 0, TimeSpan.Zero), + "30:00", + string.Empty, + Option.None); + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(new PagedPlayoutItemsViewModel(2, [item, gap])); + + IActionResult actionResult = await _controller.GetItems(9, showFiller: true, 1, 10, CancellationToken.None); + + var result = actionResult.ShouldBeOfType().Value.ShouldBeOfType(); + result.TotalCount.ShouldBe(2); + result.Page[0].Title.ShouldBe("Movie"); + result.Page[0].Duration.ShouldBe("1:00:00"); + result.Page[0].FillerKind.ShouldBe(FillerKind.MidRoll); + result.Page[1].Title.ShouldBe("UNSCHEDULED"); + result.Page[1].FillerKind.ShouldBeNull(); + + await _mediator.Received(1).Send( + Arg.Is(q => + q.PlayoutId == 9 && q.ShowFiller && q.PageNum == 1 && q.PageSize == 10), + Arg.Any()); + } + + [Test] + public async Task GetItems_Should_Return_404_For_Unknown_Playout_With_ProblemDetails() + { + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(Option.None); + + IActionResult result = await _controller.GetItems(404, showFiller: false, 0, 100, CancellationToken.None); + + var notFound = result.ShouldBeOfType(); + var problem = notFound.Value.ShouldBeOfType(); + problem.Status.ShouldBe(404); + problem.Title.ShouldBe("Resource not found"); + + await _mediator.DidNotReceive().Send(Arg.Any(), Arg.Any()); + } + + [Test] + public async Task GetWarningsCount_Should_Return_Count() + { + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(7); + + int result = await _controller.GetWarningsCount(CancellationToken.None); + + result.ShouldBe(7); + } + + [Test] + public async Task ResetAll_Should_Return_202_And_Send_Command() + { + IActionResult result = await _controller.ResetAll(CancellationToken.None); + + result.ShouldBeOfType(); + await _mediator.Received(1).Send(Arg.Any(), Arg.Any()); + } + private static PlayoutNameViewModel MakePlayout(int id) => new( id, @@ -183,7 +312,13 @@ public class PlayoutControllerTests vm.PlayoutMode, vm.ScheduleName, vm.ScheduleFile, - vm.DbDailyRebuildTime); + vm.DbDailyRebuildTime, + vm.BuildStatus is null + ? null + : new PlayoutBuildStatusResponseModel( + vm.BuildStatus.LastBuild, + vm.BuildStatus.Success, + vm.BuildStatus.Message)); private static void ShouldHaveActionRoute(string actionName, string httpMethod, string route) { diff --git a/ErsatzTV/Controllers/Api/ChannelController.cs b/ErsatzTV/Controllers/Api/ChannelController.cs index 6220adfa5..76577ca10 100644 --- a/ErsatzTV/Controllers/Api/ChannelController.cs +++ b/ErsatzTV/Controllers/Api/ChannelController.cs @@ -6,6 +6,7 @@ using ErsatzTV.Application.Playouts; using ErsatzTV.Controllers.Api.Requests; using ErsatzTV.Core; using ErsatzTV.Core.Api.Channels; +using ErsatzTV.Core.Domain; using ErsatzTV.Core.Scheduling; using ErsatzTV.Extensions; using MediatR; @@ -90,18 +91,42 @@ public class ChannelController(ChannelWriter workerCh [HttpPost("/api/channels/{channelNumber}/playout/reset")] [Tags("Channels")] [EndpointSummary("Reset channel playout")] + [EndpointDescription( + "When mode is omitted, classic playouts use Refresh (rebuild while maintaining collection " + + "progress) and all other playout kinds use Reset (rebuild from scratch), matching the Blazor UI. " + + "Pass mode to force a specific PlayoutBuildMode.")] [EndpointGroupName("general")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] - public async Task ResetPlayout(string channelNumber) + public async Task ResetPlayout( + string channelNumber, + [FromQuery] PlayoutBuildMode? mode, + CancellationToken cancellationToken) { - Option maybePlayoutId = await mediator.Send(new GetPlayoutIdByChannelNumber(channelNumber)); + Option maybePlayoutId = + await mediator.Send(new GetPlayoutIdByChannelNumber(channelNumber), cancellationToken); foreach (int playoutId in maybePlayoutId) { - await workerChannel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Reset)); + PlayoutBuildMode buildMode = mode ?? await DefaultResetMode(playoutId, cancellationToken); + await workerChannel.WriteAsync(new BuildPlayout(playoutId, buildMode), cancellationToken); return new OkResult(); } return ApiResults.NotFoundProblem(); } + + // Match Blazor's Playouts.razor reset semantics: classic playouts refresh (preserve progress), + // every other kind resets from scratch. + private async Task DefaultResetMode(int playoutId, CancellationToken cancellationToken) + { + Option maybePlayout = + await mediator.Send(new GetPlayoutById(playoutId), cancellationToken); + return maybePlayout.Match( + Some: vm => vm.ScheduleKind switch + { + PlayoutScheduleKind.Classic => PlayoutBuildMode.Refresh, + _ => PlayoutBuildMode.Reset + }, + None: () => PlayoutBuildMode.Reset); + } } diff --git a/ErsatzTV/Controllers/Api/PlayoutController.cs b/ErsatzTV/Controllers/Api/PlayoutController.cs index 833ce778b..82d3c7889 100644 --- a/ErsatzTV/Controllers/Api/PlayoutController.cs +++ b/ErsatzTV/Controllers/Api/PlayoutController.cs @@ -3,6 +3,8 @@ 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; @@ -13,6 +15,32 @@ 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 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 GetWarningsCount(CancellationToken cancellationToken) => + await mediator.Send(new GetPlayoutWarningsCount(), cancellationToken); + [HttpGet("/api/playouts/{id:int}", Name = "GetPlayoutById")] [Tags("Playouts")] [EndpointSummary("Get a playout by id")] @@ -25,6 +53,34 @@ public class PlayoutController(IMediator mediator) : ControllerBase 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 GetItems( + int id, + [FromQuery] bool showFiller = false, + [FromQuery] int pageNum = 0, + [FromQuery] int pageSize = 100, + CancellationToken cancellationToken = default) + { + Option 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")] @@ -49,6 +105,17 @@ public class PlayoutController(IMediator mediator) : ControllerBase }); } + [HttpPost("/api/playouts/reset-all", Name = "ResetAllPlayouts")] + [Tags("Playouts")] + [EndpointSummary("Reset all playouts")] + [EndpointGroupName("general")] + [ProducesResponseType(StatusCodes.Status202Accepted)] + public async Task ResetAll(CancellationToken cancellationToken) + { + await mediator.Send(new ResetAllPlayouts(), cancellationToken); + return Accepted(); + } + [HttpDelete("/api/playouts/{id:int}")] [Tags("Playouts")] [EndpointSummary("Delete a playout")] @@ -71,5 +138,32 @@ public class PlayoutController(IMediator mediator) : ControllerBase vm.PlayoutMode, vm.ScheduleName, vm.ScheduleFile, - vm.DbDailyRebuildTime); + 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)); } diff --git a/ErsatzTV/wwwroot/openapi/v1.json b/ErsatzTV/wwwroot/openapi/v1.json index a8e70ff8e..9b201c81f 100644 --- a/ErsatzTV/wwwroot/openapi/v1.json +++ b/ErsatzTV/wwwroot/openapi/v1.json @@ -370,6 +370,7 @@ "Channels" ], "summary": "Reset channel playout", + "description": "When mode is omitted, classic playouts use Refresh (rebuild while maintaining collection progress) and all other playout kinds use Reset (rebuild from scratch), matching the Blazor UI. Pass mode to force a specific PlayoutBuildMode.", "parameters": [ { "name": "channelNumber", @@ -378,6 +379,13 @@ "schema": { "type": "string" } + }, + { + "name": "mode", + "in": "query", + "schema": { + "$ref": "#/components/schemas/PlayoutBuildMode" + } } ], "responses": { @@ -1595,6 +1603,192 @@ } } }, + "/api/playouts": { + "get": { + "tags": [ + "Playouts" + ], + "summary": "List playouts", + "operationId": "GetPlayouts", + "parameters": [ + { + "name": "query", + "in": "query", + "schema": { + "type": "string", + "default": "" + } + }, + { + "name": "pageNum", + "in": "query", + "schema": { + "type": "integer", + "format": "int32", + "default": 0 + } + }, + { + "name": "pageSize", + "in": "query", + "schema": { + "type": "integer", + "format": "int32", + "default": 100 + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "$ref": "#/components/schemas/PagedPlayoutsResponseModel" + } + }, + "application/json": { + "schema": { + "$ref": "#/components/schemas/PagedPlayoutsResponseModel" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/PagedPlayoutsResponseModel" + } + } + } + } + } + }, + "post": { + "tags": [ + "Playouts" + ], + "summary": "Create a classic playout", + "requestBody": { + "content": { + "application/json-patch+json": { + "schema": { + "$ref": "#/components/schemas/CreatePlayoutRequest" + } + }, + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreatePlayoutRequest" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/CreatePlayoutRequest" + } + }, + "application/*+json": { + "schema": { + "$ref": "#/components/schemas/CreatePlayoutRequest" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Created", + "content": { + "text/plain": { + "schema": { + "$ref": "#/components/schemas/PlayoutResponseModel" + } + }, + "application/json": { + "schema": { + "$ref": "#/components/schemas/PlayoutResponseModel" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/PlayoutResponseModel" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "text/plain": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "422": { + "description": "Unprocessable Entity", + "content": { + "text/plain": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/api/playouts/warnings/count": { + "get": { + "tags": [ + "Playouts" + ], + "summary": "Count playouts with a failed build", + "operationId": "GetPlayoutWarningsCount", + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "type": "integer", + "format": "int32" + } + }, + "application/json": { + "schema": { + "type": "integer", + "format": "int32" + } + }, + "text/json": { + "schema": { + "type": "integer", + "format": "int32" + } + } + } + } + } + } + }, "/api/playouts/{id}": { "get": { "tags": [ @@ -1719,54 +1913,67 @@ } } }, - "/api/playouts": { - "post": { + "/api/playouts/{id}/items": { + "get": { "tags": [ "Playouts" ], - "summary": "Create a classic playout", - "requestBody": { - "content": { - "application/json-patch+json": { - "schema": { - "$ref": "#/components/schemas/CreatePlayoutRequest" - } - }, - "application/json": { - "schema": { - "$ref": "#/components/schemas/CreatePlayoutRequest" - } - }, - "text/json": { - "schema": { - "$ref": "#/components/schemas/CreatePlayoutRequest" - } - }, - "application/*+json": { - "schema": { - "$ref": "#/components/schemas/CreatePlayoutRequest" - } + "summary": "Get upcoming items (and unscheduled gaps) for a playout", + "operationId": "GetPlayoutItems", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int32" } }, - "required": true - }, + { + "name": "showFiller", + "in": "query", + "schema": { + "type": "boolean", + "default": false + } + }, + { + "name": "pageNum", + "in": "query", + "schema": { + "type": "integer", + "format": "int32", + "default": 0 + } + }, + { + "name": "pageSize", + "in": "query", + "schema": { + "type": "integer", + "format": "int32", + "default": 100 + } + } + ], "responses": { - "201": { - "description": "Created", + "200": { + "description": "OK", "content": { "text/plain": { "schema": { - "$ref": "#/components/schemas/PlayoutResponseModel" + "$ref": "#/components/schemas/PagedPlayoutItemsResponseModel" } }, "application/json": { "schema": { - "$ref": "#/components/schemas/PlayoutResponseModel" + "$ref": "#/components/schemas/PagedPlayoutItemsResponseModel" } }, "text/json": { "schema": { - "$ref": "#/components/schemas/PlayoutResponseModel" + "$ref": "#/components/schemas/PagedPlayoutItemsResponseModel" } } } @@ -1790,26 +1997,20 @@ } } } - }, - "422": { - "description": "Unprocessable Entity", - "content": { - "text/plain": { - "schema": { - "$ref": "#/components/schemas/ProblemDetails" - } - }, - "application/json": { - "schema": { - "$ref": "#/components/schemas/ProblemDetails" - } - }, - "text/json": { - "schema": { - "$ref": "#/components/schemas/ProblemDetails" - } - } - } + } + } + } + }, + "/api/playouts/reset-all": { + "post": { + "tags": [ + "Playouts" + ], + "summary": "Reset all playouts", + "operationId": "ResetAllPlayouts", + "responses": { + "202": { + "description": "Accepted" } } } @@ -4650,6 +4851,50 @@ ], "type": "string" }, + "PagedPlayoutItemsResponseModel": { + "required": [ + "totalCount", + "page" + ], + "type": "object", + "properties": { + "totalCount": { + "type": "integer", + "format": "int32" + }, + "page": { + "type": [ + "null", + "array" + ], + "items": { + "$ref": "#/components/schemas/PlayoutItemResponseModel" + } + } + } + }, + "PagedPlayoutsResponseModel": { + "required": [ + "totalCount", + "page" + ], + "type": "object", + "properties": { + "totalCount": { + "type": "integer", + "format": "int32" + }, + "page": { + "type": [ + "null", + "array" + ], + "items": { + "$ref": "#/components/schemas/PlayoutListItemResponseModel" + } + } + } + }, "PlaybackOrder": { "enum": [ "None", @@ -4692,6 +4937,126 @@ } } }, + "PlayoutBuildMode": { + "enum": [ + "Continue", + "Refresh", + "Reset" + ], + "type": "string" + }, + "PlayoutBuildStatusResponseModel": { + "required": [ + "lastBuild", + "success", + "message" + ], + "type": "object", + "properties": { + "lastBuild": { + "type": "string", + "format": "date-time" + }, + "success": { + "type": "boolean" + }, + "message": { + "type": [ + "null", + "string" + ] + } + } + }, + "PlayoutItemResponseModel": { + "required": [ + "title", + "start", + "finish", + "duration", + "fillerKind" + ], + "type": "object", + "properties": { + "title": { + "type": [ + "null", + "string" + ] + }, + "start": { + "type": "string", + "format": "date-time" + }, + "finish": { + "type": "string", + "format": "date-time" + }, + "duration": { + "type": [ + "null", + "string" + ] + }, + "fillerKind": { + "oneOf": [ + { + "type": "null" + }, + { + "$ref": "#/components/schemas/FillerKind" + } + ] + } + } + }, + "PlayoutListItemResponseModel": { + "required": [ + "id", + "channelNumber", + "channelName", + "scheduleKind", + "scheduleName", + "dailyRebuildTime", + "buildStatus" + ], + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int32" + }, + "channelNumber": { + "type": "string" + }, + "channelName": { + "type": "string" + }, + "scheduleKind": { + "$ref": "#/components/schemas/PlayoutScheduleKind" + }, + "scheduleName": { + "type": "string" + }, + "dailyRebuildTime": { + "pattern": "^-?(\\d+\\.)?\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,7})?$", + "type": [ + "null", + "string" + ] + }, + "buildStatus": { + "oneOf": [ + { + "type": "null" + }, + { + "$ref": "#/components/schemas/PlayoutBuildStatusResponseModel" + } + ] + } + } + }, "PlayoutMode": { "enum": [ "Flood", @@ -4710,7 +5075,8 @@ "playoutMode", "scheduleName", "scheduleFile", - "dailyRebuildTime" + "dailyRebuildTime", + "buildStatus" ], "type": "object", "properties": { @@ -4722,25 +5088,16 @@ "$ref": "#/components/schemas/PlayoutScheduleKind" }, "channelName": { - "type": [ - "null", - "string" - ] + "type": "string" }, "channelNumber": { - "type": [ - "null", - "string" - ] + "type": "string" }, "playoutMode": { "$ref": "#/components/schemas/ChannelPlayoutMode" }, "scheduleName": { - "type": [ - "null", - "string" - ] + "type": "string" }, "scheduleFile": { "type": [ @@ -4754,6 +5111,16 @@ "null", "string" ] + }, + "buildStatus": { + "oneOf": [ + { + "type": "null" + }, + { + "$ref": "#/components/schemas/PlayoutBuildStatusResponseModel" + } + ] } } },