1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/History/HistoryService.cs

97 lines
3.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-02-23 22:29:22 +01:00
using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
2013-05-11 00:33:04 +02:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download;
2013-06-09 08:20:38 +02:00
using NzbDrone.Core.MediaFiles.Events;
2013-02-23 22:29:22 +01:00
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.History
{
public interface IHistoryService
{
List<History> All();
void Purge();
void Trim();
2013-03-25 02:38:11 +01:00
QualityModel GetBestQualityInHistory(int episodeId);
2013-05-11 00:33:04 +02:00
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
}
2013-02-23 22:29:22 +01:00
2013-06-09 08:20:38 +02:00
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent>
2013-02-23 22:29:22 +01:00
{
private readonly IHistoryRepository _historyRepository;
private readonly Logger _logger;
public HistoryService(IHistoryRepository historyRepository, Logger logger)
{
_historyRepository = historyRepository;
_logger = logger;
}
public List<History> All()
{
return _historyRepository.All().ToList();
}
2013-05-11 00:33:04 +02:00
public PagingSpec<History> Paged(PagingSpec<History> pagingSpec)
{
2013-06-05 02:49:53 +02:00
return _historyRepository.GetPaged(pagingSpec);
2013-05-11 00:33:04 +02:00
}
2013-02-23 22:29:22 +01:00
public void Purge()
{
_historyRepository.Purge();
}
public virtual void Trim()
{
_historyRepository.Trim();
}
2013-03-25 02:38:11 +01:00
public virtual QualityModel GetBestQualityInHistory(int episodeId)
2013-02-23 22:29:22 +01:00
{
2013-05-14 17:27:33 +02:00
return _historyRepository.GetEpisodeHistory(episodeId).OrderByDescending(q => q).FirstOrDefault();
2013-02-23 22:29:22 +01:00
}
public void Handle(EpisodeGrabbedEvent message)
{
foreach (var episode in message.Episode.Episodes)
{
var history = new History
{
Date = DateTime.UtcNow,
Quality = message.Episode.ParsedEpisodeInfo.Quality,
2013-06-09 08:20:38 +02:00
SourceTitle = message.Episode.Report.Title,
2013-05-14 07:47:53 +02:00
SeriesId = episode.SeriesId,
2013-03-25 02:38:11 +01:00
EpisodeId = episode.Id,
};
2013-06-09 08:20:38 +02:00
history.Data.Add("Indexer", message.Episode.Report.Indexer);
history.Data.Add("NzbInfoUrl", message.Episode.Report.NzbInfoUrl);
history.Data.Add("ReleaseGroup", message.Episode.Report.ReleaseGroup);
history.Data.Add("Age", message.Episode.Report.Age.ToString());
_historyRepository.Insert(history);
}
}
public void Handle(EpisodeImportedEvent message)
{
foreach (var episode in message.EpisodeFile.Episodes.Value)
{
var history = new History
{
Date = DateTime.UtcNow,
2013-06-09 08:20:38 +02:00
Quality = message.EpisodeFile.Quality,
SourceTitle = message.EpisodeFile.Path,
SeriesId = message.EpisodeFile.SeriesId,
EpisodeId = episode.Id,
};
_historyRepository.Insert(history);
}
}
2013-02-23 22:29:22 +01:00
}
}