diff --git a/ErsatzTV.Infrastructure/Data/Repositories/MovieRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/MovieRepository.cs index 6bae72bea..f6b007c8a 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/MovieRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/MovieRepository.cs @@ -58,7 +58,9 @@ namespace ErsatzTV.Infrastructure.Data.Repositories public async Task> GetOrAdd(PlexLibrary library, PlexMovie item) { - Option maybeExisting = await _dbContext.PlexMovies + await using TvContext context = _dbContextFactory.CreateDbContext(); + Option maybeExisting = await context.PlexMovies + .AsNoTracking() .Include(i => i.MovieMetadata) .Include(i => i.MediaVersions) .ThenInclude(mv => mv.MediaFiles) @@ -67,7 +69,7 @@ namespace ErsatzTV.Infrastructure.Data.Repositories return await maybeExisting.Match( plexMovie => Right(plexMovie).AsTask(), - async () => await AddPlexMovie(library, item)); + async () => await AddPlexMovie(context, library, item)); } public async Task Update(Movie movie) @@ -120,15 +122,15 @@ namespace ErsatzTV.Infrastructure.Data.Repositories } } - private async Task> AddPlexMovie(PlexLibrary library, PlexMovie item) + private async Task> AddPlexMovie(TvContext context, PlexLibrary library, PlexMovie item) { try { item.LibraryPathId = library.Paths.Head().Id; - await _dbContext.PlexMovies.AddAsync(item); - await _dbContext.SaveChangesAsync(); - await _dbContext.Entry(item).Reference(i => i.LibraryPath).LoadAsync(); + await context.PlexMovies.AddAsync(item); + await context.SaveChangesAsync(); + await context.Entry(item).Reference(i => i.LibraryPath).LoadAsync(); return item; } catch (Exception ex) diff --git a/ErsatzTV/Pages/TelevisionSeasonList.razor b/ErsatzTV/Pages/TelevisionSeasonList.razor index 4701d4551..64dfd2569 100644 --- a/ErsatzTV/Pages/TelevisionSeasonList.razor +++ b/ErsatzTV/Pages/TelevisionSeasonList.razor @@ -7,7 +7,10 @@ @using ErsatzTV.Application.MediaCollections.Commands @using ErsatzTV.Application.ProgramSchedules @using ErsatzTV.Application.ProgramSchedules.Commands +@using Unit=LanguageExt.Unit; @inject IMediator Mediator +@inject ILogger Logger +@inject ISnackbar Snackbar @inject IDialogService Dialog @inject NavigationManager NavigationManager @@ -52,7 +55,7 @@ { + AddToCollectionClicked="@AddSeasonToCollection"/> } @@ -113,5 +116,29 @@ NavigationManager.NavigateTo($"/schedules/{schedule.Id}/items"); } } + + private async Task AddSeasonToCollection(MediaCardViewModel card) + { + if (card is TelevisionSeasonCardViewModel season) + { + var parameters = new DialogParameters { { "EntityType", "season" }, { "EntityName", season.Title } }; + var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; + + IDialogReference dialog = Dialog.Show("Add To Collection", parameters, options); + DialogResult result = await dialog.Result; + if (!result.Cancelled && result.Data is MediaCollectionViewModel collection) + { + var request = new AddSeasonToCollection(collection.Id, season.TelevisionSeasonId); + Either addResult = await Mediator.Send(request); + addResult.Match( + Left: error => + { + Snackbar.Add($"Unexpected error adding season to collection: {error.Value}"); + Logger.LogError("Unexpected error adding season to collection: {Error}", error.Value); + }, + Right: _ => Snackbar.Add($"Added {season.Title} to collection {collection.Name}", Severity.Success)); + } + } + } } \ No newline at end of file diff --git a/ErsatzTV/Pages/TelevisionShowList.razor b/ErsatzTV/Pages/TelevisionShowList.razor index 78424d02e..2924ce570 100644 --- a/ErsatzTV/Pages/TelevisionShowList.razor +++ b/ErsatzTV/Pages/TelevisionShowList.razor @@ -1,7 +1,13 @@ @page "/media/tv/shows" @using ErsatzTV.Application.MediaCards @using ErsatzTV.Application.MediaCards.Queries +@using ErsatzTV.Application.MediaCollections +@using ErsatzTV.Application.MediaCollections.Commands +@using Unit = LanguageExt.Unit +@inject ILogger Logger +@inject ISnackbar Snackbar @inject IMediator Mediator +@inject IDialogService Dialog @@ -22,7 +28,9 @@ @foreach (TelevisionShowCardViewModel card in _data.Cards) { - + } @@ -49,4 +57,28 @@ await RefreshData(); } + private async Task AddToCollection(MediaCardViewModel card) + { + if (card is TelevisionShowCardViewModel show) + { + var parameters = new DialogParameters { { "EntityType", "show" }, { "EntityName", show.Title } }; + var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; + + IDialogReference dialog = Dialog.Show("Add To Collection", parameters, options); + DialogResult result = await dialog.Result; + if (!result.Cancelled && result.Data is MediaCollectionViewModel collection) + { + var request = new AddShowToCollection(collection.Id, show.TelevisionShowId); + Either addResult = await Mediator.Send(request); + addResult.Match( + Left: error => + { + Snackbar.Add($"Unexpected error adding show to collection: {error.Value}"); + Logger.LogError("Unexpected error adding show to collection: {Error}", error.Value); + }, + Right: _ => Snackbar.Add($"Added {show.Title} to collection {collection.Name}", Severity.Success)); + } + } + } + } \ No newline at end of file