diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
index fda4bcc49..d2b866928 100644
--- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
+++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
@@ -210,6 +210,7 @@
+
diff --git a/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithFilesFixture.cs b/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithFilesFixture.cs
new file mode 100644
index 000000000..7beb912b1
--- /dev/null
+++ b/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithFilesFixture.cs
@@ -0,0 +1,78 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using FizzWare.NBuilder;
+using FluentAssertions;
+using NUnit.Framework;
+using NzbDrone.Core.MediaFiles;
+using NzbDrone.Core.Test.Framework;
+using NzbDrone.Core.Tv;
+
+namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests
+{
+ [TestFixture]
+ public class EpisodesWithFilesFixture : DbTest
+ {
+ private const int SERIES_ID = 1;
+ private List _episodes;
+ private List _episodeFiles;
+
+ [SetUp]
+ public void Setup()
+ {
+ _episodeFiles = Builder.CreateListOfSize(5)
+ .BuildListOfNew()
+ .ToList();
+
+ Db.InsertMany(_episodeFiles);
+
+ _episodes = Builder.CreateListOfSize(10)
+ .All()
+ .With(e => e.EpisodeFileId = 0)
+ .With(e => e.SeriesId = SERIES_ID)
+ .BuildListOfNew()
+ .ToList();
+
+ for (int i = 0; i < _episodeFiles.Count; i++)
+ {
+ _episodes[i].EpisodeFileId = _episodeFiles[i].Id;
+ }
+
+ Db.InsertMany(_episodes);
+ }
+
+
+ [Test]
+ public void should_only_get_files_that_have_episode_files()
+ {
+ var result = Subject.EpisodesWithFiles(SERIES_ID);
+
+ result.Should().OnlyContain(e => e.EpisodeFileId > 0);
+ result.Should().HaveCount(_episodeFiles.Count);
+ }
+
+ [Test]
+ public void should_only_contain_episodes_for_the_given_series()
+ {
+ var episodeFile = Builder.CreateNew()
+ .BuildNew();
+
+ Db.Insert(episodeFile);
+
+ var episode = Builder.CreateNew()
+ .With(e => e.SeriesId = SERIES_ID + 10)
+ .With(e => e.EpisodeFileId = episodeFile.Id)
+ .BuildNew();
+
+ Db.Insert(episode);
+
+ Subject.EpisodesWithFiles(episode.SeriesId).Should().OnlyContain(e => e.SeriesId == episode.SeriesId);
+ }
+
+ [Test]
+ public void should_have_episode_file_loaded()
+ {
+ Subject.EpisodesWithFiles(SERIES_ID).Should().OnlyContain(e => e.EpisodeFile.IsLoaded);
+ }
+ }
+}
diff --git a/src/NzbDrone.Core/Tv/EpisodeRepository.cs b/src/NzbDrone.Core/Tv/EpisodeRepository.cs
index 43cdb26b5..ed750848f 100644
--- a/src/NzbDrone.Core/Tv/EpisodeRepository.cs
+++ b/src/NzbDrone.Core/Tv/EpisodeRepository.cs
@@ -19,6 +19,7 @@ namespace NzbDrone.Core.Tv
List GetEpisodes(int seriesId);
List GetEpisodes(int seriesId, int seasonNumber);
List GetEpisodeByFileId(int fileId);
+ List EpisodesWithFiles(int seriesId);
PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec, bool includeSpecials);
PagingSpec EpisodesWhereCutoffUnmet(PagingSpec pagingSpec, List qualitiesBelowCutoff, bool includeSpecials);
List FindEpisodesBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
@@ -84,6 +85,12 @@ namespace NzbDrone.Core.Tv
return Query.Where(e => e.EpisodeFileId == fileId).ToList();
}
+ public List EpisodesWithFiles(int seriesId)
+ {
+ return Query.Join(JoinType.Inner, e => e.EpisodeFile, (e, ef) => e.EpisodeFileId == ef.Id)
+ .Where(e => e.SeriesId == seriesId);
+ }
+
public PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec, bool includeSpecials)
{
var currentTime = DateTime.UtcNow;
diff --git a/src/NzbDrone.Core/Tv/EpisodeService.cs b/src/NzbDrone.Core/Tv/EpisodeService.cs
index b94cb7f00..f5a8f0c0e 100644
--- a/src/NzbDrone.Core/Tv/EpisodeService.cs
+++ b/src/NzbDrone.Core/Tv/EpisodeService.cs
@@ -22,6 +22,7 @@ namespace NzbDrone.Core.Tv
Episode FindEpisode(int seriesId, String date);
List GetEpisodeBySeries(int seriesId);
List GetEpisodesBySeason(int seriesId, int seasonNumber);
+ List EpisodesWithFiles(int seriesId);
PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec);
List GetEpisodesByFileId(int episodeFileId);
void UpdateEpisode(Episode episode);
@@ -105,6 +106,11 @@ namespace NzbDrone.Core.Tv
});
}
+ public List EpisodesWithFiles(int seriesId)
+ {
+ return _episodeRepository.EpisodesWithFiles(seriesId);
+ }
+
public PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec)
{
var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false);