78 lines
3.2 KiB
C#
78 lines
3.2 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.Extensions;
|
|
using ErsatzTV.Filters;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace ErsatzTV.Controllers.Api;
|
|
|
|
[ApiController]
|
|
[ServiceFilter(typeof(ApiKeyAuthorizationFilter))]
|
|
public class PlayoutController(IMediator mediator) : ControllerBase
|
|
{
|
|
[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();
|
|
}
|
|
|
|
[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());
|
|
});
|
|
}
|
|
|
|
[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);
|
|
}
|