using System.Threading.Channels; using ErsatzTV.Application.Playouts; using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Repositories; using ErsatzTV.Core.Scheduling; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Infrastructure.Extensions; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Application.MediaCollections; public class AddSongToCollectionHandler : IRequestHandler> { private readonly ChannelWriter _channel; private readonly IDbContextFactory _dbContextFactory; private readonly IMediaCollectionRepository _mediaCollectionRepository; public AddSongToCollectionHandler( IDbContextFactory dbContextFactory, IMediaCollectionRepository mediaCollectionRepository, ChannelWriter channel) { _dbContextFactory = dbContextFactory; _mediaCollectionRepository = mediaCollectionRepository; _channel = channel; } public async Task> Handle( AddSongToCollection request, CancellationToken cancellationToken) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); Validation validation = await Validate(dbContext, request); return await validation.Apply(parameters => ApplyAddSongRequest(dbContext, parameters)); } private async Task ApplyAddSongRequest(TvContext dbContext, Parameters parameters) { parameters.Collection.MediaItems.Add(parameters.Song); if (await dbContext.SaveChangesAsync() > 0) { // refresh all playouts that use this collection foreach (int playoutId in await _mediaCollectionRepository .PlayoutIdsUsingCollection(parameters.Collection.Id)) { await _channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh)); } } return Unit.Default; } private static async Task> Validate( TvContext dbContext, AddSongToCollection request) => (await CollectionMustExist(dbContext, request), await ValidateSong(dbContext, request)) .Apply((collection, episode) => new Parameters(collection, episode)); private static Task> CollectionMustExist( TvContext dbContext, AddSongToCollection request) => dbContext.Collections .Include(c => c.MediaItems) .SelectOneAsync(c => c.Id, c => c.Id == request.CollectionId) .Map(o => o.ToValidation("Collection does not exist.")); private static Task> ValidateSong( TvContext dbContext, AddSongToCollection request) => dbContext.Songs .SelectOneAsync(m => m.Id, e => e.Id == request.SongId) .Map(o => o.ToValidation("Song does not exist")); private sealed record Parameters(Collection Collection, Song Song); }