using System; using System.Collections.Generic; using System.Data; using System.Linq; using NzbDrone.Core.Datastore; namespace NzbDrone.Core.Tv { public interface IEpisodeRepository : IBasicRepository { Episode Get(int seriesId, int season, int episodeNumber); Episode Get(int seriesId, DateTime date); List GetEpisodes(int seriesId); List GetEpisodes(int seriesId, int seasonNumber); List GetEpisodeByFileId(int fileId); List EpisodesWithoutFiles(bool includeSpecials); Episode GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber); List EpisodesWithFiles(); List EpisodesBetweenDates(DateTime startDate, DateTime endDate); void SetIgnoreFlat(Episode episode, bool ignoreFlag); } public class EpisodeRepository : BasicRepository, IEpisodeRepository { public EpisodeRepository(IDatabase database) : base(database) { } public Episode Get(int seriesId, int season, int episodeNumber) { return Query.Single(s => s.SeriesId == seriesId && s.SeasonNumber == season && s.EpisodeNumber == episodeNumber); } public Episode Get(int seriesId, DateTime date) { return Query.Single(s => s.SeriesId == seriesId && s.AirDate.HasValue && s.AirDate.Value.Date == date.Date); } public List GetEpisodes(int seriesId) { return Query.Where(s => s.SeriesId == seriesId).ToList(); } public List GetEpisodes(int seriesId, int seasonNumber) { return Query.Where(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber).ToList(); } public List GetEpisodeByFileId(int fileId) { return Query.Where(s => s.EpisodeFile != null && s.EpisodeFile.Id == fileId).ToList(); } public List EpisodesWithoutFiles(bool includeSpecials) { if (!includeSpecials) { return Query.Where(s => s.EpisodeFile == null && s.SeasonNumber != 0).ToList(); } return Query.Where(s => s.EpisodeFile == null).ToList(); } public Episode GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber) { return Query.Single(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber && s.SceneEpisodeNumber == episodeNumber); } public List EpisodesWithFiles() { return Query.Where(s => s.EpisodeFile != null).ToList(); } public List EpisodesBetweenDates(DateTime startDate, DateTime endDate) { return Query.Where(s => s.AirDate >= startDate && s.AirDate <= endDate).ToList(); } public void SetIgnoreFlat(Episode episode, bool ignoreFlag) { episode.Ignored = ignoreFlag; UpdateFields(episode, p => p.Ignored); } } }