sonarr-repo-only/NzbDrone.Core/Tv/EpisodeService.cs

211 lines
7.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Configuration;
2013-05-02 05:50:34 +00:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.Tv
{
public interface IEpisodeService
{
Episode GetEpisode(int id);
Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useScene = false);
Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useScene = false);
Episode GetEpisode(int seriesId, DateTime date);
Episode FindEpisode(int seriesId, DateTime date);
2013-03-02 19:13:23 +00:00
List<Episode> GetEpisodeBySeries(int seriesId);
List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber);
2013-05-13 06:12:19 +00:00
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec);
List<Episode> GetEpisodesByFileId(int episodeFileId);
List<Episode> EpisodesWithFiles();
void UpdateEpisode(Episode episode);
List<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber);
void SetEpisodeMonitored(int episodeId, bool monitored);
bool IsFirstOrLastEpisodeOfSeason(int episodeId);
void UpdateEpisodes(List<Episode> episodes);
2013-02-26 03:30:24 +00:00
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
2013-06-02 19:29:00 +00:00
void InsertMany(List<Episode> episodes);
void UpdateMany(List<Episode> episodes);
2013-08-01 02:02:36 +00:00
void DeleteMany(List<Episode> episodes);
void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
}
public class EpisodeService : IEpisodeService,
IHandle<EpisodeFileDeletedEvent>,
IHandle<EpisodeFileAddedEvent>,
2013-06-02 19:29:00 +00:00
IHandleAsync<SeriesDeletedEvent>
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2013-02-24 20:24:31 +00:00
private readonly IEpisodeRepository _episodeRepository;
private readonly IConfigService _configService;
private readonly Logger _logger;
2013-06-02 19:29:00 +00:00
public EpisodeService(IEpisodeRepository episodeRepository, IConfigService configService, Logger logger)
{
_episodeRepository = episodeRepository;
_configService = configService;
_logger = logger;
}
2013-03-02 19:13:23 +00:00
public Episode GetEpisode(int id)
{
return _episodeRepository.Get(id);
}
public Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useSceneNumbering = false)
{
if (useSceneNumbering)
{
return _episodeRepository.GetEpisodeBySceneNumbering(seriesId, seasonNumber, episodeNumber);
}
return _episodeRepository.Find(seriesId, seasonNumber, episodeNumber);
}
public Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useSceneNumbering = false)
{
if (useSceneNumbering)
{
return _episodeRepository.FindEpisodeBySceneNumbering(seriesId, seasonNumber, episodeNumber);
}
return _episodeRepository.Find(seriesId, seasonNumber, episodeNumber);
}
2013-03-02 19:13:23 +00:00
public Episode GetEpisode(int seriesId, DateTime date)
{
return _episodeRepository.Get(seriesId, date);
}
public Episode FindEpisode(int seriesId, DateTime date)
{
return _episodeRepository.Find(seriesId, date);
}
2013-03-02 19:13:23 +00:00
public List<Episode> GetEpisodeBySeries(int seriesId)
{
2013-03-02 19:13:23 +00:00
return _episodeRepository.GetEpisodes(seriesId).ToList();
}
public List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber)
{
return _episodeRepository.GetEpisodes(seriesId, seasonNumber);
}
2013-05-13 06:12:19 +00:00
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec)
{
2013-05-13 06:12:19 +00:00
var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false);
2013-05-02 05:50:34 +00:00
return episodeResult;
}
public List<Episode> GetEpisodesByFileId(int episodeFileId)
{
return _episodeRepository.GetEpisodeByFileId(episodeFileId);
}
public List<Episode> EpisodesWithFiles()
{
return _episodeRepository.EpisodesWithFiles();
}
2013-03-02 19:13:23 +00:00
public void UpdateEpisode(Episode episode)
{
_episodeRepository.Update(episode);
}
public List<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber)
{
return GetEpisodesBySeason(seriesId, seasonNumber).Select(c => c.Id).ToList();
}
public void SetEpisodeMonitored(int episodeId, bool monitored)
{
var episode = _episodeRepository.Get(episodeId);
_episodeRepository.SetMonitoredFlat(episode, monitored);
logger.Info("Monitored flag for Episode:{0} was set to {1}", episodeId, monitored);
}
2013-07-10 02:11:00 +00:00
public void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored)
{
_episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored);
}
public bool IsFirstOrLastEpisodeOfSeason(int episodeId)
{
var episode = GetEpisode(episodeId);
var seasonEpisodes = GetEpisodesBySeason(episode.SeriesId, episode.SeasonNumber);
//Ensure that this is either the first episode
//or is the last episode in a season that has 10 or more episodes
if (seasonEpisodes.First().EpisodeNumber == episode.EpisodeNumber || (seasonEpisodes.Count() >= 10 && seasonEpisodes.Last().EpisodeNumber == episode.EpisodeNumber))
return true;
return false;
}
2013-03-02 19:13:23 +00:00
public void UpdateEpisodes(List<Episode> episodes)
{
_episodeRepository.UpdateMany(episodes);
}
2013-02-26 03:30:24 +00:00
public List<Episode> EpisodesBetweenDates(DateTime start, DateTime end)
{
var episodes = _episodeRepository.EpisodesBetweenDates(start.ToUniversalTime(), end.ToUniversalTime());
2013-05-30 04:06:24 +00:00
return episodes;
}
2013-06-02 19:29:00 +00:00
public void InsertMany(List<Episode> episodes)
{
_episodeRepository.InsertMany(episodes);
}
public void UpdateMany(List<Episode> episodes)
{
2013-06-03 03:56:17 +00:00
_episodeRepository.UpdateMany(episodes);
2013-06-02 19:29:00 +00:00
}
2013-08-01 02:02:36 +00:00
public void DeleteMany(List<Episode> episodes)
{
_episodeRepository.DeleteMany(episodes);
}
public void HandleAsync(SeriesDeletedEvent message)
{
var episodes = GetEpisodeBySeries(message.Series.Id);
_episodeRepository.DeleteMany(episodes);
}
public void Handle(EpisodeFileDeletedEvent message)
{
foreach (var episode in GetEpisodesByFileId(message.EpisodeFile.Id))
{
_logger.Trace("Detaching episode {0} from file.", episode.Id);
episode.EpisodeFileId = 0;
if (!message.ForUpgrade && _configService.AutoUnmonitorPreviouslyDownloadedEpisodes)
{
episode.Monitored = false;
}
UpdateEpisode(episode);
}
}
public void Handle(EpisodeFileAddedEvent message)
{
foreach (var episode in message.EpisodeFile.Episodes.Value)
{
_episodeRepository.SetFileId(episode.Id, message.EpisodeFile.Id);
_logger.Debug("Linking [{0}] > [{1}]", message.EpisodeFile.Path, episode);
}
}
}
}