2013-03-04 01:26:41 +03:00
|
|
|
using System.Collections.Generic;
|
2012-02-21 06:25:19 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-03-04 01:26:41 +03:00
|
|
|
using NzbDrone.Common.Eventing;
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-02-19 09:01:03 +03:00
|
|
|
namespace NzbDrone.Core.Tv
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
public interface ISeasonService
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
void SetIgnore(int seriesId, int seasonNumber, bool isIgnored);
|
2013-03-04 01:26:41 +03:00
|
|
|
List<Season> GetSeasonsBySeries(int seriesId);
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-03-04 01:26:41 +03:00
|
|
|
public class SeasonService : ISeasonService,
|
|
|
|
IHandle<EpisodeInfoAddedEvent>,
|
2013-03-05 22:49:34 +03:00
|
|
|
IHandle<EpisodeInfoUpdatedEvent>,
|
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
|
|
|
private readonly Logger _logger;
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-03-04 01:26:41 +03:00
|
|
|
public SeasonService(ISeasonRepository seasonRepository, Logger logger)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
_seasonRepository = seasonRepository;
|
|
|
|
_logger = logger;
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
public void SetIgnore(int seriesId, int seasonNumber, bool isIgnored)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
var season = _seasonRepository.Get(seriesId, seasonNumber);
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
_logger.Trace("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);
|
2012-02-28 08:50:56 +03:00
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
season.Ignored = isIgnored;
|
2013-03-04 01:26:41 +03:00
|
|
|
season.Episodes.ForEach(e => e.Ignored = isIgnored);
|
2012-02-28 08:50:56 +03:00
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
_seasonRepository.Update(season);
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
_logger.Info("Ignore flag for Series:{0} Season:{1} successfully set to {2}", seriesId, seasonNumber, isIgnored);
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
2013-03-04 01:26:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
public List<Season> GetSeasonsBySeries(int seriesId)
|
|
|
|
{
|
|
|
|
return _seasonRepository.GetSeasonBySeries(seriesId);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Ignored = false
|
|
|
|
});
|
|
|
|
|
|
|
|
_seasonRepository.InsertMany(seasonToAdd.ToList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 22:49:34 +03:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
{
|
|
|
|
var seasons = GetSeasonsBySeries(message.Series.Id);
|
|
|
|
_seasonRepository.DeleteMany(seasons);
|
|
|
|
}
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
2012-02-21 09:50:38 +03:00
|
|
|
|
2012-02-28 08:50:56 +03:00
|
|
|
|
2012-02-21 06:25:19 +03:00
|
|
|
} |