private async Task <Unit> ApplyAddItemsRequest( TvContext dbContext, Collection collection, AddItemsToCollection request) { var allItems = request.MovieIds .Append(request.ShowIds) .Append(request.SeasonIds) .Append(request.EpisodeIds) .Append(request.ArtistIds) .Append(request.MusicVideoIds) .Append(request.OtherVideoIds) .Append(request.SongIds) .ToList(); var toAddIds = allItems.Where(item => collection.MediaItems.All(mi => mi.Id != item)).ToList(); List <MediaItem> toAdd = await dbContext.MediaItems .Filter(mi => toAddIds.Contains(mi.Id)) .ToListAsync(); collection.MediaItems.AddRange(toAdd); if (await dbContext.SaveChangesAsync() > 0) { // refresh all playouts that use this collection foreach (int playoutId in await _mediaCollectionRepository .PlayoutIdsUsingCollection(request.CollectionId)) { await _channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh)); } } return(Unit.Default); }
public async Task <Either <BaseError, Unit> > Handle( AddItemsToCollection request, CancellationToken cancellationToken) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); Validation <BaseError, Collection> validation = await Validate(dbContext, request); return(await LanguageExtensions.Apply(validation, c => ApplyAddItemsRequest(dbContext, c, request))); }
protected async Task AddItemsToCollection( List <int> movieIds, List <int> showIds, List <int> seasonIds, List <int> episodeIds, List <int> artistIds, List <int> musicVideoIds, List <int> otherVideoIds, List <int> songIds, string entityName = "selected items") { int count = movieIds.Count + showIds.Count + seasonIds.Count + episodeIds.Count + artistIds.Count + musicVideoIds.Count + otherVideoIds.Count + songIds.Count; var parameters = new DialogParameters { { "EntityType", count.ToString() }, { "EntityName", entityName } }; var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; IDialogReference dialog = Dialog.Show <AddToCollectionDialog>("Add To Collection", parameters, options); DialogResult result = await dialog.Result; if (!result.Cancelled && result.Data is MediaCollectionViewModel collection) { var request = new AddItemsToCollection( collection.Id, movieIds, showIds, seasonIds, episodeIds, artistIds, musicVideoIds, otherVideoIds, songIds); Either <BaseError, Unit> addResult = await Mediator.Send(request, CancellationToken); addResult.Match( Left: error => { Snackbar.Add($"Unexpected error adding items to collection: {error.Value}"); Logger.LogError("Unexpected error adding items to collection: {Error}", error.Value); }, Right: _ => { Snackbar.Add( $"Added {count} items to collection {collection.Name}", Severity.Success); ClearSelection(); }); } }
private async Task <Unit> ApplyAddItemsRequest(AddItemsToCollection request) { if (await _mediaCollectionRepository.AddMediaItems( request.CollectionId, request.MovieIds.Append(request.ShowIds).ToList())) { // 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); }
protected async Task AddSelectionToCollection() { var parameters = new DialogParameters { { "EntityType", _selectedItems.Count.ToString() }, { "EntityName", "selected items" } }; var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; IDialogReference dialog = Dialog.Show <AddToCollectionDialog>("Add To Collection", parameters, options); DialogResult result = await dialog.Result; if (!result.Cancelled && result.Data is MediaCollectionViewModel collection) { var request = new AddItemsToCollection( collection.Id, _selectedItems.OfType <MovieCardViewModel>().Map(m => m.MovieId).ToList(), _selectedItems.OfType <TelevisionShowCardViewModel>().Map(s => s.TelevisionShowId).ToList()); Either <BaseError, Unit> addResult = await Mediator.Send(request); addResult.Match( Left: error => { Snackbar.Add($"Unexpected error adding items to collection: {error.Value}"); Logger.LogError("Unexpected error adding items to collection: {Error}", error.Value); }, Right: _ => { Snackbar.Add( $"Added {_selectedItems.Count} items to collection {collection.Name}", Severity.Success); ClearSelection(); }); } }
private async Task <Validation <BaseError, Unit> > Validate(AddItemsToCollection request) => (await CollectionMustExist(request), await ValidateMovies(request), await ValidateShows(request))
public Task <Either <BaseError, Unit> > Handle( AddItemsToCollection request, CancellationToken cancellationToken) => Validate(request) .MapT(_ => ApplyAddItemsRequest(request)) .Bind(v => v.ToEitherAsync());
private async Task <Validation <BaseError, Collection> > Validate( TvContext dbContext, AddItemsToCollection request) => (await CollectionMustExist(dbContext, request),