Files
ersatztv/ErsatzTV.Application/ProgramSchedules/Commands/AddProgramScheduleItemHandler.cs
T
timothy b552905799
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 3m40s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m52s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
feat(api): add schedule REST endpoints
Refs #36
2026-06-29 08:13:14 +02:00

88 lines
3.6 KiB
C#

using System.Threading.Channels;
using ErsatzTV.Application.Playouts;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Scheduling;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using static ErsatzTV.Application.ProgramSchedules.Mapper;
namespace ErsatzTV.Application.ProgramSchedules;
public class AddProgramScheduleItemHandler : ProgramScheduleItemCommandBase,
IRequestHandler<AddProgramScheduleItem, Either<BaseError, ProgramScheduleItemViewModel>>
{
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
private readonly IDbContextFactory<TvContext> _dbContextFactory;
public AddProgramScheduleItemHandler(
IDbContextFactory<TvContext> dbContextFactory,
ChannelWriter<IBackgroundServiceRequest> channel)
{
_dbContextFactory = dbContextFactory;
_channel = channel;
}
public async Task<Either<BaseError, ProgramScheduleItemViewModel>> Handle(
AddProgramScheduleItem request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<ProgramSchedule> maybeProgramSchedule =
await ProgramScheduleMustExist(dbContext, request.ProgramScheduleId, cancellationToken);
return await maybeProgramSchedule.Match(
Some: async programSchedule =>
{
Validation<BaseError, ProgramSchedule> validation = await Validate(dbContext, request, programSchedule);
return await validation.Apply(ps => PersistItem(dbContext, request, ps, cancellationToken));
},
None: () => Task.FromResult<Either<BaseError, ProgramScheduleItemViewModel>>(
new NotFoundError("[ProgramScheduleId] does not exist.")));
}
private async Task<ProgramScheduleItemViewModel> PersistItem(
TvContext dbContext,
AddProgramScheduleItem request,
ProgramSchedule programSchedule,
CancellationToken cancellationToken)
{
int nextIndex = programSchedule.Items.Select(i => i.Index).DefaultIfEmpty(0).Max() + 1;
ProgramScheduleItem item = BuildItem(programSchedule, nextIndex, request);
programSchedule.Items.Add(item);
await dbContext.SaveChangesAsync(cancellationToken);
// refresh any playouts that use this schedule
foreach (Playout playout in programSchedule.Playouts)
{
await _channel.WriteAsync(new BuildPlayout(playout.Id, PlayoutBuildMode.Refresh), cancellationToken);
}
return ProjectToViewModel(item);
}
private static async Task<Validation<BaseError, ProgramSchedule>> Validate(
TvContext dbContext,
AddProgramScheduleItem request,
ProgramSchedule programSchedule)
{
Validation<BaseError, ProgramSchedule> validation =
PlayoutModeMustBeValid(request, programSchedule)
.Bind(programSchedule => CollectionTypeMustBeValid(request, programSchedule));
return await validation.ToEither().Match(
Left: error => Task.FromResult<Validation<BaseError, ProgramSchedule>>(
Fail<BaseError, ProgramSchedule>(error)),
Right: async validProgramSchedule =>
{
Either<BaseError, ProgramSchedule> fillerResult = await FillerConfigurationMustBeValid(
dbContext,
request,
validProgramSchedule);
return fillerResult.ToValidation();
});
}
}