From 527d3c6e4bdb49c839622b558fa24b6651672b6e Mon Sep 17 00:00:00 2001 From: Jason Dove Date: Sun, 21 Mar 2021 20:57:32 -0500 Subject: [PATCH] attach existing episodes to correct show and season when adding nfo metadata (#97) --- .../Data/Repositories/TelevisionRepository.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs index c45e215a3..308754900 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs @@ -285,9 +285,24 @@ namespace ErsatzTV.Infrastructure.Data.Repositories .OrderBy(i => i.MediaVersions.First().MediaFiles.First().Path) .SingleOrDefaultAsync(i => i.MediaVersions.First().MediaFiles.First().Path == path); - return await maybeExisting.Match( - episode => Right(episode).AsTask(), - () => AddEpisode(dbContext, season, libraryPath.Id, path)); + return await maybeExisting.Match>>( + async episode => + { + // move the file to the new season if needed + // this can happen when adding NFO metadata to existing content + if (episode.SeasonId != season.Id) + { + episode.SeasonId = season.Id; + episode.Season = season; + + await _dbConnection.ExecuteAsync( + @"UPDATE Episode SET SeasonId = @SeasonId WHERE Id = @EpisodeId", + new { SeasonId = season.Id, EpisodeId = episode.Id }); + } + + return episode; + }, + async () => await AddEpisode(dbContext, season, libraryPath.Id, path)); } public Task> FindEpisodePaths(LibraryPath libraryPath) =>