diff --git a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs index 05d398a7d..88aea4091 100644 --- a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs +++ b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs @@ -286,7 +286,6 @@ namespace PlexRequests.Services.Jobs var episodes = await EpisodeRepo.GetAllAsync(); if (episodes == null) { - Log.Info("Episode cache info is not available."); return new HashSet(); } return episodes; diff --git a/PlexRequests.UI/Content/requests.js b/PlexRequests.UI/Content/requests.js index c5e08ce07..765db5ecb 100644 --- a/PlexRequests.UI/Content/requests.js +++ b/PlexRequests.UI/Content/requests.js @@ -577,14 +577,14 @@ function tvLoad() { var tvObject = new Array(); results.forEach(function (result) { var ep = result.episodes; - ep.forEach(function (episode, index) { - if (!tvObject.find(x => x.seasonNumber === episode.seasonNumber)) { + ep.forEach(function (episode) { + var foundItem = tvObject.find(x => x.seasonNumber === episode.seasonNumber); + if (!foundItem) { var obj = { seasonNumber: episode.seasonNumber, episodes: [] } tvObject.push(obj); - tvObject[index].episodes.push(episode.episodeNumber); + tvObject[tvObject.length - 1].episodes.push(episode.episodeNumber); } else { - var selectedObj =tvObject.find(x => x.seasonNumber === episode.seasonNumber); - selectedObj.episodes.push(episode.episodeNumber); + foundItem.episodes.push(episode.episodeNumber); } }); diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 931734f85..dd85e9737 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -567,9 +567,8 @@ namespace PlexRequests.UI.Modules } // check if the show/episodes have already been requested - var existingRequest = await RequestService.CheckRequestAsync(showId); - var difference = new List(); + var difference = new List(); if (existingRequest != null) { if (episodeRequest) @@ -666,13 +665,20 @@ namespace PlexRequests.UI.Modules { var cachedEpisodesTask = await Checker.GetEpisodes(); var cachedEpisodes = cachedEpisodesTask.ToList(); - foreach (var d in difference) + foreach (var d in difference) // difference is from an existing request { if (cachedEpisodes.Any(x => x.SeasonNumber == d.SeasonNumber && x.EpisodeNumber == d.EpisodeNumber && x.ProviderId == providerId)) { return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {d.SeasonNumber} - {d.EpisodeNumber} {Resources.UI.Search_AlreadyInPlex}" }); } } + + var episodes = await GetEpisodes(showId); + var availableEpisodes = episodes.Where(x => x.Requested).ToList(); + var availble = availableEpisodes.Select(a => new EpisodesModel { EpisodeNumber = a.EpisodeNumber, SeasonNumber = a.SeasonNumber }).ToList(); + + var diff = model.Episodes.Except(availble); + model.Episodes = diff.ToList(); } else { @@ -951,6 +957,13 @@ namespace PlexRequests.UI.Modules private async Task GetEpisodes() { var seriesId = (int)Request.Query.tvId; + var model = await GetEpisodes(seriesId); + + return Response.AsJson(model); + } + + private async Task> GetEpisodes(int providerId) + { var s = await SonarrService.GetSettingsAsync(); var sonarrEnabled = s.Enabled; var allResults = await RequestService.GetAllAsync(); @@ -961,7 +974,7 @@ namespace PlexRequests.UI.Modules if (sonarrEnabled) { var allSeries = SonarrApi.GetSeries(s.ApiKey, s.FullUri); - var selectedSeries = allSeries.FirstOrDefault(x => x.tvdbId == seriesId) ?? new Series(); + var selectedSeries = allSeries.FirstOrDefault(x => x.tvdbId == providerId) ?? new Series(); return selectedSeries; } return new Series(); @@ -971,8 +984,8 @@ namespace PlexRequests.UI.Modules var requests = allResults as RequestedModel[] ?? allResults.ToArray(); - var existingRequest = requests.FirstOrDefault(x => x.Type == RequestType.TvShow && x.TvDbId == seriesId.ToString()); - var show = await Task.Run(() => TvApi.ShowLookupByTheTvDbId(seriesId)); + var existingRequest = requests.FirstOrDefault(x => x.Type == RequestType.TvShow && x.TvDbId == providerId.ToString()); + var show = await Task.Run(() => TvApi.ShowLookupByTheTvDbId(providerId)); var tvMaxeEpisodes = await Task.Run(() => TvApi.EpisodeLookup(show.id)); var sonarrEpisodes = new List(); @@ -983,7 +996,7 @@ namespace PlexRequests.UI.Modules sonarrEpisodes = sonarrEp?.ToList() ?? new List(); } - var plexCacheTask = await Checker.GetEpisodes(seriesId); + var plexCacheTask = await Checker.GetEpisodes(providerId); var plexCache = plexCacheTask.ToList(); foreach (var ep in tvMaxeEpisodes) { @@ -1003,9 +1016,8 @@ namespace PlexRequests.UI.Modules Name = ep.name, EpisodeId = ep.id }); - } + }return model; - return Response.AsJson(model); } public async Task CheckRequestLimit(PlexRequestSettings s, RequestType type)