sonarr-repo-only/NzbDrone.Core/History/HistoryRepository.cs

82 lines
2.9 KiB
C#
Raw Normal View History

2013-02-23 21:29:22 +00:00
using System;
2013-05-07 02:32:43 +00:00
using System.Collections.Generic;
2013-02-23 21:29:22 +00:00
using System.Linq;
2013-05-10 22:33:04 +00:00
using Marr.Data.QGen;
2013-02-23 21:29:22 +00:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
2013-02-23 21:29:22 +00:00
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.History
{
public interface IHistoryRepository : IBasicRepository<History>
{
void Trim();
2013-05-14 14:36:23 +00:00
List<QualityModel> GetEpisodeHistory(int episodeId);
void CleanupOrphanedBySeries();
void CleanupOrphanedByEpisode();
2013-02-23 21:29:22 +00:00
}
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
{
private readonly IDatabase _database;
public HistoryRepository(IDatabase database, IEventAggregator eventAggregator)
: base(database, eventAggregator)
2013-02-23 21:29:22 +00:00
{
_database = database;
2013-02-23 21:29:22 +00:00
}
public void Trim()
{
var cutoff = DateTime.UtcNow.AddDays(-30).Date;
2013-03-27 03:44:52 +00:00
Delete(c=> c.Date < cutoff);
2013-02-23 21:29:22 +00:00
}
2013-05-14 14:36:23 +00:00
public List<QualityModel> GetEpisodeHistory(int episodeId)
2013-02-23 21:29:22 +00:00
{
var history = Query.Where(c => c.EpisodeId == episodeId);
2013-02-23 21:29:22 +00:00
return history.Select(h => h.Quality).ToList();
2013-02-23 21:29:22 +00:00
}
2013-05-07 02:32:43 +00:00
public void CleanupOrphanedBySeries()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Series
ON History.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
public void CleanupOrphanedByEpisode()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Episodes
ON History.EpisodeId = Episodes.Id
WHERE Episodes.Id IS NULL)");
}
2013-06-05 00:49:53 +00:00
public override PagingSpec<History> GetPaged(PagingSpec<History> pagingSpec)
2013-05-10 22:33:04 +00:00
{
var pagingQuery = Query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id)
.Join<History, Episode>(JoinType.Inner, h => h.Episode, (h, e) => h.EpisodeId == e.Id)
.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
.Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize);
pagingSpec.Records = pagingQuery.ToList();
//TODO: Use the same query for count and records
pagingSpec.TotalRecords = Count();
return pagingSpec;
}
2013-02-23 21:29:22 +00:00
}
}