api changes to support etvcli (#2519)

This commit is contained in:
Jason Dove
2025-10-12 10:13:30 -05:00
committed by GitHub
parent bf4182f115
commit 48e7c85f7b
17 changed files with 421 additions and 46 deletions
@@ -5,17 +5,14 @@ using static ErsatzTV.Application.Channels.Mapper;
namespace ErsatzTV.Application.Channels; namespace ErsatzTV.Application.Channels;
public class GetAllChannelsForApiHandler : IRequestHandler<GetAllChannelsForApi, List<ChannelResponseModel>> public class GetAllChannelsForApiHandler(IChannelRepository channelRepository)
: IRequestHandler<GetAllChannelsForApi, List<ChannelResponseModel>>
{ {
private readonly IChannelRepository _channelRepository;
public GetAllChannelsForApiHandler(IChannelRepository channelRepository) => _channelRepository = channelRepository;
public async Task<List<ChannelResponseModel>> Handle( public async Task<List<ChannelResponseModel>> Handle(
GetAllChannelsForApi request, GetAllChannelsForApi request,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
IEnumerable<Channel> channels = Optional(await _channelRepository.GetAll(cancellationToken)).Flatten(); IEnumerable<Channel> channels = Optional(await channelRepository.GetAll(cancellationToken)).Flatten();
return channels.Map(ProjectToResponseModel).ToList(); return channels.Map(ProjectToResponseModel).ToList();
} }
} }
@@ -0,0 +1,3 @@
namespace ErsatzTV.Application.MediaCollections;
public record CreateSmartCollectionResult(int SmartCollectionId) : EntityIdResult(SmartCollectionId);
@@ -2,4 +2,5 @@
namespace ErsatzTV.Application.MediaCollections; namespace ErsatzTV.Application.MediaCollections;
public record UpdateSmartCollection(int Id, string Name, string Query) : IRequest<Either<BaseError, Unit>>; public record UpdateSmartCollection(int Id, string Name, string Query)
: IRequest<Either<BaseError, UpdateSmartCollectionResult>>;
@@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.MediaCollections; namespace ErsatzTV.Application.MediaCollections;
public class UpdateSmartCollectionHandler : IRequestHandler<UpdateSmartCollection, Either<BaseError, Unit>> public class UpdateSmartCollectionHandler : IRequestHandler<UpdateSmartCollection, Either<BaseError, UpdateSmartCollectionResult>>
{ {
private readonly ChannelWriter<IBackgroundServiceRequest> _channel; private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
private readonly IDbContextFactory<TvContext> _dbContextFactory; private readonly IDbContextFactory<TvContext> _dbContextFactory;
@@ -34,7 +34,7 @@ public class UpdateSmartCollectionHandler : IRequestHandler<UpdateSmartCollectio
_smartCollectionCache = smartCollectionCache; _smartCollectionCache = smartCollectionCache;
} }
public async Task<Either<BaseError, Unit>> Handle( public async Task<Either<BaseError, UpdateSmartCollectionResult>> Handle(
UpdateSmartCollection request, UpdateSmartCollection request,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
@@ -43,7 +43,7 @@ public class UpdateSmartCollectionHandler : IRequestHandler<UpdateSmartCollectio
return await validation.Apply(c => ApplyUpdateRequest(dbContext, c, request, cancellationToken)); return await validation.Apply(c => ApplyUpdateRequest(dbContext, c, request, cancellationToken));
} }
private async Task<Unit> ApplyUpdateRequest( private async Task<UpdateSmartCollectionResult> ApplyUpdateRequest(
TvContext dbContext, TvContext dbContext,
SmartCollection c, SmartCollection c,
UpdateSmartCollection request, UpdateSmartCollection request,
@@ -65,7 +65,7 @@ public class UpdateSmartCollectionHandler : IRequestHandler<UpdateSmartCollectio
} }
} }
return Unit.Default; return new UpdateSmartCollectionResult(c.Id);
} }
private static Task<Validation<BaseError, SmartCollection>> Validate( private static Task<Validation<BaseError, SmartCollection>> Validate(
@@ -0,0 +1,3 @@
namespace ErsatzTV.Application.MediaCollections;
public record UpdateSmartCollectionResult(int SmartCollectionId) : EntityIdResult(SmartCollectionId);
@@ -1,4 +1,5 @@
using ErsatzTV.Application.Tree; using ErsatzTV.Application.Tree;
using ErsatzTV.Core.Api.SmartCollections;
using ErsatzTV.Core.Domain; using ErsatzTV.Core.Domain;
namespace ErsatzTV.Application.MediaCollections; namespace ErsatzTV.Application.MediaCollections;
@@ -23,6 +24,9 @@ internal static class Mapper
internal static SmartCollectionViewModel ProjectToViewModel(SmartCollection collection) => internal static SmartCollectionViewModel ProjectToViewModel(SmartCollection collection) =>
new(collection.Id, collection.Name, collection.Query); new(collection.Id, collection.Name, collection.Query);
internal static SmartCollectionResponseModel ProjectToResponseModel(SmartCollection collection) =>
new(collection.Id, collection.Name, collection.Query);
internal static RerunCollectionViewModel ProjectToViewModel(RerunCollection collection) => internal static RerunCollectionViewModel ProjectToViewModel(RerunCollection collection) =>
new( new(
collection.Id, collection.Id,
@@ -0,0 +1,5 @@
using ErsatzTV.Core.Api.SmartCollections;
namespace ErsatzTV.Application.MediaCollections;
public record GetAllSmartCollectionsForApi : IRequest<List<SmartCollectionResponseModel>>;
@@ -0,0 +1,22 @@
using ErsatzTV.Core.Api.SmartCollections;
using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using static ErsatzTV.Application.MediaCollections.Mapper;
namespace ErsatzTV.Application.MediaCollections;
public class GetAllSmartCollectionsForApiHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<GetAllSmartCollectionsForApi, List<SmartCollectionResponseModel>>
{
public async Task<List<SmartCollectionResponseModel>> Handle(
GetAllSmartCollectionsForApi request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
List<SmartCollection> ffmpegProfiles = await dbContext.SmartCollections
.AsNoTracking()
.ToListAsync(cancellationToken);
return ffmpegProfiles.Map(ProjectToResponseModel).ToList();
}
}
@@ -0,0 +1,3 @@
namespace ErsatzTV.Core.Api.SmartCollections;
public record SmartCollectionResponseModel(int Id, string Name, string Query);
@@ -4,16 +4,11 @@ using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Infrastructure.Data.Repositories; namespace ErsatzTV.Infrastructure.Data.Repositories;
public class ChannelRepository : IChannelRepository public class ChannelRepository(IDbContextFactory<TvContext> dbContextFactory) : IChannelRepository
{ {
private readonly IDbContextFactory<TvContext> _dbContextFactory;
public ChannelRepository(IDbContextFactory<TvContext> dbContextFactory) =>
_dbContextFactory = dbContextFactory;
public async Task<Option<Channel>> GetChannel(int id) public async Task<Option<Channel>> GetChannel(int id)
{ {
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
return await dbContext.Channels return await dbContext.Channels
.AsNoTracking() .AsNoTracking()
.Include(c => c.Artwork) .Include(c => c.Artwork)
@@ -25,7 +20,7 @@ public class ChannelRepository : IChannelRepository
public async Task<Option<Channel>> GetByNumber(string number) public async Task<Option<Channel>> GetByNumber(string number)
{ {
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
return await dbContext.Channels return await dbContext.Channels
.AsNoTracking() .AsNoTracking()
.Include(c => c.FFmpegProfile) .Include(c => c.FFmpegProfile)
@@ -39,7 +34,7 @@ public class ChannelRepository : IChannelRepository
public async Task<List<Channel>> GetAll(CancellationToken cancellationToken) public async Task<List<Channel>> GetAll(CancellationToken cancellationToken)
{ {
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
return await dbContext.Channels return await dbContext.Channels
.AsNoTracking() .AsNoTracking()
.Include(c => c.FFmpegProfile) .Include(c => c.FFmpegProfile)
@@ -52,7 +47,7 @@ public class ChannelRepository : IChannelRepository
public async Task<Option<ChannelWatermark>> GetWatermarkByName(string name) public async Task<Option<ChannelWatermark>> GetWatermarkByName(string name)
{ {
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
List<ChannelWatermark> maybeWatermarks = await dbContext.ChannelWatermarks List<ChannelWatermark> maybeWatermarks = await dbContext.ChannelWatermarks
.AsNoTracking() .AsNoTracking()
@@ -14,7 +14,7 @@ namespace ErsatzTV.Controllers.Api;
public class ChannelController(ChannelWriter<IBackgroundServiceRequest> workerChannel, IMediator mediator) public class ChannelController(ChannelWriter<IBackgroundServiceRequest> workerChannel, IMediator mediator)
{ {
[HttpGet("/api/channels")] [HttpGet("/api/channels")]
[V2ApiActionFilter] [EndpointGroupName("general")]
public async Task<List<ChannelResponseModel>> GetAll() => await mediator.Send(new GetAllChannelsForApi()); public async Task<List<ChannelResponseModel>> GetAll() => await mediator.Send(new GetAllChannelsForApi());
[HttpPost("/api/channels/{channelNumber}/playout/reset")] [HttpPost("/api/channels/{channelNumber}/playout/reset")]
@@ -10,30 +10,26 @@ namespace ErsatzTV.Controllers.Api;
[ApiController] [ApiController]
[V2ApiActionFilter] [V2ApiActionFilter]
public class FFmpegProfileController public class FFmpegProfileController(IMediator mediator)
{ {
private readonly IMediator _mediator;
public FFmpegProfileController(IMediator mediator) => _mediator = mediator;
[HttpGet("/api/ffmpeg/profiles")] [HttpGet("/api/ffmpeg/profiles")]
public async Task<List<FFmpegProfileResponseModel>> GetAll() => public async Task<List<FFmpegProfileResponseModel>> GetAll() =>
await _mediator.Send(new GetAllFFmpegProfilesForApi()); await mediator.Send(new GetAllFFmpegProfilesForApi());
[HttpPost("/api/ffmpeg/profiles/new")] [HttpPost("/api/ffmpeg/profiles/new")]
public async Task<Either<BaseError, CreateFFmpegProfileResult>> AddOne( public async Task<Either<BaseError, CreateFFmpegProfileResult>> AddOne(
[Required] [FromBody] [Required] [FromBody]
CreateFFmpegProfile request) => await _mediator.Send(request); CreateFFmpegProfile request) => await mediator.Send(request);
[HttpPut("/api/ffmpeg/profiles/update")] [HttpPut("/api/ffmpeg/profiles/update")]
public async Task<Either<BaseError, UpdateFFmpegProfileResult>> UpdateOne( public async Task<Either<BaseError, UpdateFFmpegProfileResult>> UpdateOne(
[Required] [FromBody] [Required] [FromBody]
UpdateFFmpegProfile request) => await _mediator.Send(request); UpdateFFmpegProfile request) => await mediator.Send(request);
[HttpGet("/api/ffmpeg/profiles/{id:int}")] [HttpGet("/api/ffmpeg/profiles/{id:int}")]
public async Task<Option<FFmpegFullProfileResponseModel>> GetOne(int id) => public async Task<Option<FFmpegFullProfileResponseModel>> GetOne(int id) =>
await _mediator.Send(new GetFFmpegFullProfileByIdForApi(id)); await mediator.Send(new GetFFmpegFullProfileByIdForApi(id));
[HttpDelete("/api/ffmpeg/delete/{id:int}")] [HttpDelete("/api/ffmpeg/delete/{id:int}")]
public async Task DeleteProfileAsync(int id) => await _mediator.Send(new DeleteFFmpegProfile(id)); public async Task DeleteProfileAsync(int id) => await mediator.Send(new DeleteFFmpegProfile(id));
} }
@@ -0,0 +1,43 @@
using System.ComponentModel.DataAnnotations;
using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Core;
using ErsatzTV.Core.Api.SmartCollections;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace ErsatzTV.Controllers.Api;
[ApiController]
[EndpointGroupName("general")]
public class SmartCollectionController(IMediator mediator) : ControllerBase
{
[HttpGet("/api/collections/smart", Name="GetSmartCollections")]
public async Task<List<SmartCollectionResponseModel>> GetAll() =>
await mediator.Send(new GetAllSmartCollectionsForApi());
[HttpPost("/api/collections/smart/new", Name = "CreateSmartCollection")]
public async Task<IActionResult> AddOne(
[Required] [FromBody]
CreateSmartCollection request)
{
Either<BaseError, CreateSmartCollectionResult> result =
await mediator.Send(request).MapT(r => new CreateSmartCollectionResult(r.Id));
return result.Match<IActionResult>(Ok, error => Problem(error.ToString()));
}
[HttpPut("/api/collections/smart/update", Name="UpdateSmartCollection")]
public async Task<IActionResult> UpdateOne(
[Required] [FromBody]
UpdateSmartCollection request)
{
Either<BaseError, UpdateSmartCollectionResult> result = await mediator.Send(request);
return result.Match<IActionResult>(Ok, error => Problem(error.ToString()));
}
[HttpDelete("/api/collections/smart/delete/{id:int}", Name="DeleteSmartCollection")]
public async Task<IActionResult> DeleteSmartCollection(int id)
{
Either<BaseError, Unit> result = await mediator.Send(new DeleteSmartCollection(id));
return result.Match<IActionResult>(_ => Ok(), error => Problem(error.ToString()));
}
}
+10 -6
View File
@@ -7,15 +7,19 @@ namespace ErsatzTV.Controllers.Api;
[EndpointGroupName("general")] [EndpointGroupName("general")]
public class VersionController public class VersionController
{ {
private static readonly string Version; private static readonly CombinedVersion Version;
static VersionController() => static VersionController() =>
Version = Assembly.GetEntryAssembly()? Version = new CombinedVersion(
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()? 1,
.InformationalVersion ?? "unknown"; Assembly.GetEntryAssembly()?
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion ?? "unknown");
[HttpGet("/api/version")] [HttpGet("/api/version", Name="GetVersion")]
[Tags("Version")] [Tags("Version")]
[EndpointSummary("Get version")] [EndpointSummary("Get version")]
public string GetVersion() => Version; public CombinedVersion GetVersion() => Version;
public record CombinedVersion(int ApiVersion, string AppVersion);
} }
+1 -1
View File
@@ -953,7 +953,7 @@
collectionResult.Item1.Name, collectionResult.Item1.Name,
_query); _query);
Either<BaseError, Unit> updateResult = await Mediator.Send(request, CancellationToken); Either<BaseError, UpdateSmartCollectionResult> updateResult = await Mediator.Send(request, CancellationToken);
updateResult.Match( updateResult.Match(
Left: error => Left: error =>
{ {
+1 -1
View File
@@ -70,7 +70,7 @@
await _form.Validate(); await _form.Validate();
if (_success) if (_success)
{ {
Either<BaseError, Unit> result = await Mediator.Send(new UpdateSmartCollection(Id, _model.Name, _model.Query), _cts.Token); Either<BaseError, UpdateSmartCollectionResult> result = await Mediator.Send(new UpdateSmartCollection(Id, _model.Name, _model.Query), _cts.Token);
result.Match( result.Match(
_ => NavigationManager.NavigateTo("media/smart-collections"), _ => NavigationManager.NavigateTo("media/smart-collections"),
error => error =>
+305 -6
View File
@@ -5,6 +5,44 @@
"version": "1.0.0" "version": "1.0.0"
}, },
"paths": { "paths": {
"/api/channels": {
"get": {
"tags": [
"Channel"
],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ChannelResponseModel"
}
}
},
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ChannelResponseModel"
}
}
},
"text/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ChannelResponseModel"
}
}
}
}
}
}
}
},
"/api/channels/{channelNumber}/playout/reset": { "/api/channels/{channelNumber}/playout/reset": {
"post": { "post": {
"tags": [ "tags": [
@@ -199,29 +237,169 @@
} }
} }
}, },
"/api/version": { "/api/collections/smart": {
"get": { "get": {
"tags": [ "tags": [
"Version" "SmartCollection"
], ],
"summary": "Get version", "operationId": "GetSmartCollections",
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"content": { "content": {
"text/plain": { "text/plain": {
"schema": { "schema": {
"type": "string" "type": "array",
"items": {
"$ref": "#/components/schemas/SmartCollectionResponseModel"
}
} }
}, },
"application/json": { "application/json": {
"schema": { "schema": {
"type": "string" "type": "array",
"items": {
"$ref": "#/components/schemas/SmartCollectionResponseModel"
}
} }
}, },
"text/json": { "text/json": {
"schema": { "schema": {
"type": "string" "type": "array",
"items": {
"$ref": "#/components/schemas/SmartCollectionResponseModel"
}
}
}
}
}
}
}
},
"/api/collections/smart/new": {
"post": {
"tags": [
"SmartCollection"
],
"operationId": "CreateSmartCollection",
"requestBody": {
"content": {
"application/json-patch+json": {
"schema": {
"$ref": "#/components/schemas/CreateSmartCollection"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateSmartCollection"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/CreateSmartCollection"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/CreateSmartCollection"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/collections/smart/update": {
"put": {
"tags": [
"SmartCollection"
],
"operationId": "UpdateSmartCollection",
"requestBody": {
"content": {
"application/json-patch+json": {
"schema": {
"$ref": "#/components/schemas/UpdateSmartCollection"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/UpdateSmartCollection"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/UpdateSmartCollection"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/UpdateSmartCollection"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/collections/smart/delete/{id}": {
"delete": {
"tags": [
"SmartCollection"
],
"operationId": "DeleteSmartCollection",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/version": {
"get": {
"tags": [
"Version"
],
"summary": "Get version",
"operationId": "GetVersion",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/CombinedVersion"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/CombinedVersion"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/CombinedVersion"
} }
} }
} }
@@ -232,6 +410,77 @@
}, },
"components": { "components": {
"schemas": { "schemas": {
"ChannelResponseModel": {
"required": [
"id",
"number",
"name",
"fFmpegProfile",
"language",
"streamingMode"
],
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"number": {
"type": "string",
"nullable": true
},
"name": {
"type": "string",
"nullable": true
},
"fFmpegProfile": {
"type": "string",
"nullable": true
},
"language": {
"type": "string",
"nullable": true
},
"streamingMode": {
"type": "string",
"nullable": true
}
}
},
"CombinedVersion": {
"required": [
"apiVersion",
"appVersion"
],
"type": "object",
"properties": {
"apiVersion": {
"type": "integer",
"format": "int32"
},
"appVersion": {
"type": "string",
"nullable": true
}
}
},
"CreateSmartCollection": {
"required": [
"query",
"name"
],
"type": "object",
"properties": {
"query": {
"type": "string",
"nullable": true
},
"name": {
"type": "string",
"nullable": true
}
}
},
"HlsSessionModel": { "HlsSessionModel": {
"required": [ "required": [
"channelNumber", "channelNumber",
@@ -274,10 +523,57 @@
"default": false "default": false
} }
} }
},
"SmartCollectionResponseModel": {
"required": [
"id",
"name",
"query"
],
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string",
"nullable": true
},
"query": {
"type": "string",
"nullable": true
}
}
},
"UpdateSmartCollection": {
"required": [
"id",
"name",
"query"
],
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string",
"nullable": true
},
"query": {
"type": "string",
"nullable": true
}
}
} }
} }
}, },
"tags": [ "tags": [
{
"name": "Channel"
},
{ {
"name": "Channels" "name": "Channels"
}, },
@@ -290,6 +586,9 @@
{ {
"name": "Sessions" "name": "Sessions"
}, },
{
"name": "SmartCollection"
},
{ {
"name": "Version" "name": "Version"
} }