Untrusted draft — no build/test had run yet. Review before building on it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
133 lines
5.7 KiB
C#
133 lines
5.7 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.Threading.Channels;
|
|
using ErsatzTV.Application;
|
|
using ErsatzTV.Application.Channels;
|
|
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;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace ErsatzTV.Controllers.Api;
|
|
|
|
[ApiController]
|
|
public class ChannelController(ChannelWriter<IBackgroundServiceRequest> workerChannel, IMediator mediator)
|
|
{
|
|
[HttpGet("/api/channels")]
|
|
[EndpointGroupName("general")]
|
|
public async Task<List<ChannelResponseModel>> GetAll() => await mediator.Send(new GetAllChannelsForApi());
|
|
|
|
[HttpGet("/api/channels/{id:int}", Name = "GetChannelById")]
|
|
[Tags("Channels")]
|
|
[EndpointSummary("Get a channel by id")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(ChannelViewModel), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
|
|
{
|
|
Option<ChannelViewModel> result = await mediator.Send(new GetChannelById(id), cancellationToken);
|
|
return result.ToGetResult();
|
|
}
|
|
|
|
[HttpPost("/api/channels")]
|
|
[Tags("Channels")]
|
|
[EndpointSummary("Create a channel")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(ChannelViewModel), StatusCodes.Status201Created)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
|
|
public async Task<IActionResult> Create(
|
|
[Required] [FromBody] CreateChannelRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Either<BaseError, CreateChannelResult> result = await mediator.Send(request.ToCommand(), cancellationToken);
|
|
return await result.Match(
|
|
Left: error => Task.FromResult(error.ToErrorResult()),
|
|
Right: async created =>
|
|
{
|
|
Option<ChannelViewModel> channel =
|
|
await mediator.Send(new GetChannelById(created.ChannelId), cancellationToken);
|
|
return channel.Match(
|
|
Some: vm => (IActionResult)new CreatedResult($"/api/channels/{vm.Id}", vm),
|
|
None: () => ApiResults.NotFoundProblem());
|
|
});
|
|
}
|
|
|
|
[HttpPut("/api/channels/{id:int}")]
|
|
[Tags("Channels")]
|
|
[EndpointSummary("Update a channel")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(ChannelViewModel), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
|
|
public async Task<IActionResult> Update(
|
|
int id,
|
|
[Required] [FromBody] UpdateChannelRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Either<BaseError, ChannelViewModel> result =
|
|
await mediator.Send(request.ToCommand(id), cancellationToken);
|
|
return result.ToUpdatedResult();
|
|
}
|
|
|
|
[HttpDelete("/api/channels/{id:int}")]
|
|
[Tags("Channels")]
|
|
[EndpointSummary("Delete a channel")]
|
|
[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 DeleteChannel(id), cancellationToken);
|
|
return result.ToDeletedResult();
|
|
}
|
|
|
|
[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<IActionResult> ResetPlayout(
|
|
string channelNumber,
|
|
[FromQuery] PlayoutBuildMode? mode,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Option<int> maybePlayoutId =
|
|
await mediator.Send(new GetPlayoutIdByChannelNumber(channelNumber), cancellationToken);
|
|
foreach (int playoutId in maybePlayoutId)
|
|
{
|
|
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<PlayoutBuildMode> DefaultResetMode(int playoutId, CancellationToken cancellationToken)
|
|
{
|
|
Option<PlayoutNameViewModel> 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);
|
|
}
|
|
}
|