|
|
|
|
@ -1,5 +1,7 @@
@@ -1,5 +1,7 @@
|
|
|
|
|
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; |
|
|
|
|
@ -7,17 +9,21 @@ using LanguageExt;
@@ -7,17 +9,21 @@ using LanguageExt;
|
|
|
|
|
|
|
|
|
|
namespace ErsatzTV.Application.MediaCollections.Commands |
|
|
|
|
{ |
|
|
|
|
public class AddSeasonToCollectionHandler : MediatR.IRequestHandler<AddSeasonToCollection, Either<BaseError, Unit>> |
|
|
|
|
public class |
|
|
|
|
AddSeasonToCollectionHandler : MediatR.IRequestHandler<AddSeasonToCollection, Either<BaseError, Unit>> |
|
|
|
|
{ |
|
|
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _channel; |
|
|
|
|
private readonly IMediaCollectionRepository _mediaCollectionRepository; |
|
|
|
|
private readonly ITelevisionRepository _televisionRepository; |
|
|
|
|
|
|
|
|
|
public AddSeasonToCollectionHandler( |
|
|
|
|
IMediaCollectionRepository mediaCollectionRepository, |
|
|
|
|
ITelevisionRepository televisionRepository) |
|
|
|
|
ITelevisionRepository televisionRepository, |
|
|
|
|
ChannelWriter<IBackgroundServiceRequest> channel) |
|
|
|
|
{ |
|
|
|
|
_mediaCollectionRepository = mediaCollectionRepository; |
|
|
|
|
_televisionRepository = televisionRepository; |
|
|
|
|
_channel = channel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Task<Either<BaseError, Unit>> Handle( |
|
|
|
|
@ -27,8 +33,20 @@ namespace ErsatzTV.Application.MediaCollections.Commands
@@ -27,8 +33,20 @@ namespace ErsatzTV.Application.MediaCollections.Commands
|
|
|
|
|
.MapT(_ => ApplyAddTelevisionSeasonRequest(request)) |
|
|
|
|
.Bind(v => v.ToEitherAsync()); |
|
|
|
|
|
|
|
|
|
private async Task<Unit> ApplyAddTelevisionSeasonRequest(AddSeasonToCollection request) => |
|
|
|
|
await _mediaCollectionRepository.AddMediaItem(request.CollectionId, request.SeasonId); |
|
|
|
|
private async Task<Unit> ApplyAddTelevisionSeasonRequest(AddSeasonToCollection request) |
|
|
|
|
{ |
|
|
|
|
if (await _mediaCollectionRepository.AddMediaItem(request.CollectionId, request.SeasonId)) |
|
|
|
|
{ |
|
|
|
|
// rebuild all playouts that use this collection
|
|
|
|
|
foreach (int playoutId in await _mediaCollectionRepository |
|
|
|
|
.PlayoutIdsUsingCollection(request.CollectionId)) |
|
|
|
|
{ |
|
|
|
|
await _channel.WriteAsync(new BuildPlayout(playoutId, true)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Unit.Default; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async Task<Validation<BaseError, Unit>> Validate(AddSeasonToCollection request) => |
|
|
|
|
(await CollectionMustExist(request), await ValidateSeason(request)) |
|
|
|
|
|