2013-03-03 23:26:41 +01:00
|
|
|
|
using System.Collections.Generic;
|
2012-02-21 04:25:19 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-08-05 11:09:41 +02:00
|
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2013-03-03 23:26:41 +01:00
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
2012-02-21 04:25:19 +01:00
|
|
|
|
|
2013-02-19 07:01:03 +01:00
|
|
|
|
namespace NzbDrone.Core.Tv
|
2012-02-21 04:25:19 +01:00
|
|
|
|
{
|
2013-02-20 03:05:15 +01:00
|
|
|
|
public interface ISeasonService
|
2012-02-21 04:25:19 +01:00
|
|
|
|
{
|
2013-07-09 03:22:02 +02:00
|
|
|
|
void SetMonitored(int seriesId, int seasonNumber, bool monitored);
|
2013-08-05 11:09:41 +02:00
|
|
|
|
List<Season> SetSeasonPass(int seriesId, int seasonNumber);
|
2013-03-03 23:26:41 +01:00
|
|
|
|
List<Season> GetSeasonsBySeries(int seriesId);
|
2013-08-05 11:09:41 +02:00
|
|
|
|
List<Season> GetAllSeasons();
|
2013-02-20 03:05:15 +01:00
|
|
|
|
}
|
2012-02-21 04:25:19 +01:00
|
|
|
|
|
2013-03-03 23:26:41 +01:00
|
|
|
|
public class SeasonService : ISeasonService,
|
|
|
|
|
IHandle<EpisodeInfoAddedEvent>,
|
2013-03-05 20:49:34 +01:00
|
|
|
|
IHandle<EpisodeInfoUpdatedEvent>,
|
|
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-02-20 03:05:15 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
2013-07-10 04:11:00 +02:00
|
|
|
|
private readonly EpisodeService _episodeService;
|
2013-02-20 03:05:15 +01:00
|
|
|
|
private readonly Logger _logger;
|
2012-02-21 04:25:19 +01:00
|
|
|
|
|
2013-07-10 04:11:00 +02:00
|
|
|
|
public SeasonService(ISeasonRepository seasonRepository, EpisodeService episodeService, Logger logger)
|
2012-02-21 04:25:19 +01:00
|
|
|
|
{
|
2013-02-20 03:05:15 +01:00
|
|
|
|
_seasonRepository = seasonRepository;
|
2013-07-10 04:11:00 +02:00
|
|
|
|
_episodeService = episodeService;
|
2013-02-20 03:05:15 +01:00
|
|
|
|
_logger = logger;
|
2012-02-21 04:25:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-07-09 03:22:02 +02:00
|
|
|
|
public void SetMonitored(int seriesId, int seasonNumber, bool monitored)
|
2012-02-21 04:25:19 +01:00
|
|
|
|
{
|
2013-02-20 03:05:15 +01:00
|
|
|
|
var season = _seasonRepository.Get(seriesId, seasonNumber);
|
2012-02-21 04:25:19 +01:00
|
|
|
|
|
2013-07-09 03:22:02 +02:00
|
|
|
|
_logger.Trace("Setting monitored flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, monitored);
|
2012-02-28 06:50:56 +01:00
|
|
|
|
|
2013-07-09 03:22:02 +02:00
|
|
|
|
season.Monitored = monitored;
|
2013-07-10 04:11:00 +02:00
|
|
|
|
_episodeService.SetEpisodeMonitoredBySeason(seriesId, seasonNumber, monitored);
|
2013-02-20 03:05:15 +01:00
|
|
|
|
_seasonRepository.Update(season);
|
2012-02-21 04:25:19 +01:00
|
|
|
|
|
2013-07-09 03:22:02 +02:00
|
|
|
|
_logger.Info("Monitored flag for Series:{0} Season:{1} successfully set to {2}", seriesId, seasonNumber, monitored);
|
2012-02-21 04:25:19 +01:00
|
|
|
|
}
|
2013-03-03 23:26:41 +01:00
|
|
|
|
|
2013-08-05 11:09:41 +02:00
|
|
|
|
public List<Season> SetSeasonPass(int seriesId, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Setting up Season Pass for {0} starting with season: {0}", seriesId, seasonNumber);
|
|
|
|
|
|
|
|
|
|
var seasons = GetSeasonsBySeries(seriesId);
|
|
|
|
|
|
|
|
|
|
foreach (var season in seasons)
|
|
|
|
|
{
|
|
|
|
|
if (season.SeasonNumber >= seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Setting monitored flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, true);
|
|
|
|
|
season.Monitored = true;
|
2013-08-05 22:02:25 +02:00
|
|
|
|
_episodeService.SetEpisodeMonitoredBySeason(seriesId, seasonNumber, true);
|
2013-08-05 11:09:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Setting monitored flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, false);
|
|
|
|
|
season.Monitored = false;
|
2013-08-05 22:02:25 +02:00
|
|
|
|
_episodeService.SetEpisodeMonitoredBySeason(seriesId, seasonNumber, false);
|
2013-08-05 11:09:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_seasonRepository.UpdateMany(seasons);
|
|
|
|
|
_logger.Trace("Season Pass set for {0} starting with season: {0}", seriesId, seasonNumber);
|
|
|
|
|
|
|
|
|
|
return seasons;
|
|
|
|
|
}
|
2013-03-03 23:26:41 +01:00
|
|
|
|
|
|
|
|
|
public List<Season> GetSeasonsBySeries(int seriesId)
|
|
|
|
|
{
|
|
|
|
|
return _seasonRepository.GetSeasonBySeries(seriesId);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 11:09:41 +02:00
|
|
|
|
public List<Season> GetAllSeasons()
|
|
|
|
|
{
|
|
|
|
|
return _seasonRepository.All().ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 23:26:41 +01:00
|
|
|
|
public void Handle(EpisodeInfoAddedEvent message)
|
|
|
|
|
{
|
|
|
|
|
EnsureSeasons(message.Episodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeInfoUpdatedEvent message)
|
|
|
|
|
{
|
|
|
|
|
EnsureSeasons(message.Episodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureSeasons(IEnumerable<Episode> episodes)
|
|
|
|
|
{
|
|
|
|
|
var seriesGroup = episodes.GroupBy(c => c.SeriesId);
|
|
|
|
|
|
|
|
|
|
foreach (var group in seriesGroup)
|
|
|
|
|
{
|
|
|
|
|
var seriesId = group.Key;
|
|
|
|
|
|
|
|
|
|
var existingSeasons = _seasonRepository.GetSeasonNumbers(seriesId);
|
|
|
|
|
var seasonNumbers = group.Select(c => c.SeasonNumber).Distinct();
|
|
|
|
|
var missingSeasons = seasonNumbers.Where(seasonNumber => !existingSeasons.Contains(seasonNumber));
|
|
|
|
|
|
|
|
|
|
var seasonToAdd = missingSeasons.Select(c => new Season()
|
|
|
|
|
{
|
|
|
|
|
SeriesId = seriesId,
|
|
|
|
|
SeasonNumber = c,
|
2013-07-09 03:22:02 +02:00
|
|
|
|
Monitored = true
|
2013-03-03 23:26:41 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_seasonRepository.InsertMany(seasonToAdd.ToList());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-05 20:49:34 +01:00
|
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
|
{
|
|
|
|
|
var seasons = GetSeasonsBySeries(message.Series.Id);
|
|
|
|
|
_seasonRepository.DeleteMany(seasons);
|
|
|
|
|
}
|
2013-02-20 03:05:15 +01:00
|
|
|
|
}
|
2012-02-21 07:50:38 +01:00
|
|
|
|
|
2012-02-28 06:50:56 +01:00
|
|
|
|
|
2012-02-21 04:25:19 +01:00
|
|
|
|
} |