mirror of https://github.com/ErsatzTV/ErsatzTV.git
9 changed files with 310 additions and 98 deletions
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
using ErsatzTV.Core; |
||||
using LanguageExt; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Commands |
||||
{ |
||||
public record UpdatePlayoutDaysToBuild(int DaysToBuild) : MediatR.IRequest<Either<BaseError, Unit>>; |
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Channels; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Application.Playouts.Commands; |
||||
using ErsatzTV.Core; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using ErsatzTV.Infrastructure.Data; |
||||
using LanguageExt; |
||||
using Microsoft.EntityFrameworkCore; |
||||
using static LanguageExt.Prelude; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Commands |
||||
{ |
||||
public class |
||||
UpdatePlayoutDaysToBuildHandler : MediatR.IRequestHandler<UpdatePlayoutDaysToBuild, Either<BaseError, Unit>> |
||||
{ |
||||
private readonly IConfigElementRepository _configElementRepository; |
||||
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
||||
private readonly ChannelWriter<IBackgroundServiceRequest> _workerChannel; |
||||
|
||||
public UpdatePlayoutDaysToBuildHandler( |
||||
IConfigElementRepository configElementRepository, |
||||
IDbContextFactory<TvContext> dbContextFactory, |
||||
ChannelWriter<IBackgroundServiceRequest> workerChannel) |
||||
{ |
||||
_configElementRepository = configElementRepository; |
||||
_dbContextFactory = dbContextFactory; |
||||
_workerChannel = workerChannel; |
||||
} |
||||
|
||||
public async Task<Either<BaseError, Unit>> Handle( |
||||
UpdatePlayoutDaysToBuild request, |
||||
CancellationToken cancellationToken) |
||||
{ |
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
||||
Validation<BaseError, Unit> validation = await Validate(request); |
||||
return await validation.Apply<Unit, Unit>(_ => ApplyUpdate(dbContext, request.DaysToBuild)); |
||||
} |
||||
|
||||
private async Task<Unit> ApplyUpdate(TvContext dbContext, int daysToBuild) |
||||
{ |
||||
await _configElementRepository.Upsert(ConfigElementKey.PlayoutDaysToBuild, daysToBuild); |
||||
|
||||
// build all playouts to proper number of days
|
||||
List<Playout> playouts = await dbContext.Playouts |
||||
.Include(p => p.Channel) |
||||
.ToListAsync(); |
||||
foreach (int playoutId in playouts.OrderBy(p => decimal.Parse(p.Channel.Number)).Map(p => p.Id)) |
||||
{ |
||||
await _workerChannel.WriteAsync(new BuildPlayout(playoutId)); |
||||
} |
||||
|
||||
return Unit.Default; |
||||
} |
||||
|
||||
private static Task<Validation<BaseError, Unit>> Validate(UpdatePlayoutDaysToBuild request) => |
||||
Optional(request.DaysToBuild) |
||||
.Filter(days => days > 0) |
||||
.Map(_ => Unit.Default) |
||||
.ToValidation<BaseError>("Days to build must be greater than zero") |
||||
.AsTask(); |
||||
} |
||||
} |
||||
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
using MediatR; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Queries |
||||
{ |
||||
public record GetPlayoutDaysToBuild : IRequest<int>; |
||||
} |
||||
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Queries |
||||
{ |
||||
public class GetPlayoutDaysToBuildHandler : IRequestHandler<GetPlayoutDaysToBuild, int> |
||||
{ |
||||
private readonly IConfigElementRepository _configElementRepository; |
||||
|
||||
public GetPlayoutDaysToBuildHandler(IConfigElementRepository configElementRepository) => |
||||
_configElementRepository = configElementRepository; |
||||
|
||||
public Task<int> Handle(GetPlayoutDaysToBuild request, CancellationToken cancellationToken) => |
||||
_configElementRepository.GetValue<int>(ConfigElementKey.PlayoutDaysToBuild) |
||||
.Map(result => result.IfNone(2)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue