2011-01-29 06:10:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2012-07-28 06:37:47 +00:00
|
|
|
|
using DataTables.Mvc.Core.Helpers;
|
|
|
|
|
using DataTables.Mvc.Core.Models;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
using NLog;
|
2011-05-28 19:23:35 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2012-02-07 05:08:07 +00:00
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
2011-06-17 06:59:13 +00:00
|
|
|
|
using PetaPoco;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-04-08 23:58:46 +00:00
|
|
|
|
public class HistoryProvider
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-17 06:59:13 +00:00
|
|
|
|
private readonly IDatabase _database;
|
2012-02-07 05:08:07 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 06:59:13 +00:00
|
|
|
|
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
2011-06-17 06:59:13 +00:00
|
|
|
|
public HistoryProvider(IDatabase database)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-17 06:59:13 +00:00
|
|
|
|
_database = database;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-10 00:14:51 +00:00
|
|
|
|
public HistoryProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-19 23:44:45 +00:00
|
|
|
|
public virtual List<History> AllItems()
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-19 23:44:45 +00:00
|
|
|
|
return _database.Fetch<History>("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual List<History> AllItemsWithRelationships()
|
|
|
|
|
{
|
|
|
|
|
return _database.Fetch<History, Episode>(@"
|
|
|
|
|
SELECT History.*, Series.Title as SeriesTitle, Episodes.* FROM History
|
|
|
|
|
INNER JOIN Series ON History.SeriesId = Series.SeriesId
|
|
|
|
|
INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId
|
|
|
|
|
");
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-08 23:58:46 +00:00
|
|
|
|
public virtual void Purge()
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-17 06:59:13 +00:00
|
|
|
|
_database.Delete<History>("");
|
2012-02-07 05:08:07 +00:00
|
|
|
|
logger.Info("History has been Purged");
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-08 23:58:46 +00:00
|
|
|
|
public virtual void Trim()
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-17 06:59:13 +00:00
|
|
|
|
_database.Delete<History>("WHERE Date < @0", DateTime.Now.AddDays(-30).Date);
|
2012-02-07 05:08:07 +00:00
|
|
|
|
logger.Info("History has been trimmed, items older than 30 days have been removed");
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-22 20:14:02 +00:00
|
|
|
|
public virtual void Add(History item)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-17 06:59:13 +00:00
|
|
|
|
_database.Insert(item);
|
2012-02-07 05:08:07 +00:00
|
|
|
|
logger.Debug("Item added to history: {0}", item.NzbTitle);
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2012-10-13 21:15:21 +00:00
|
|
|
|
public virtual QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2013-01-11 01:15:36 +00:00
|
|
|
|
var quality = _database.Fetch<QualityModel>(@"SELECT History.Quality , History.IsProper as Proper FROM History
|
|
|
|
|
INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId
|
|
|
|
|
WHERE Episodes.seriesId = @0 AND
|
2012-02-07 05:08:07 +00:00
|
|
|
|
Episodes.SeasonNumber = @1 AND
|
2013-01-11 01:15:36 +00:00
|
|
|
|
Episodes.EpisodeNumber = @2"
|
2012-02-07 05:08:07 +00:00
|
|
|
|
, seriesId, seasonNumber, episodeNumber);
|
2011-06-05 19:42:53 +00:00
|
|
|
|
|
2013-01-11 01:15:36 +00:00
|
|
|
|
var best = quality.OrderByDescending(q => q).FirstOrDefault();
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
2013-01-11 01:15:36 +00:00
|
|
|
|
return best;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-08-27 00:59:51 +00:00
|
|
|
|
|
|
|
|
|
public virtual void Delete(int historyId)
|
|
|
|
|
{
|
|
|
|
|
_database.Delete<History>(historyId);
|
|
|
|
|
}
|
2012-07-28 06:37:47 +00:00
|
|
|
|
|
|
|
|
|
public virtual Page<HistoryQueryModel> GetPagedItems(DataTablesPageRequest pageRequest)
|
|
|
|
|
{
|
|
|
|
|
var query = Sql.Builder
|
|
|
|
|
.Select(@"History.*, Series.Title as SeriesTitle, Episodes.Title as EpisodeTitle,
|
|
|
|
|
Episodes.SeasonNumber as SeasonNumber, Episodes.EpisodeNumber as EpisodeNumber,
|
|
|
|
|
Episodes.Overview as EpisodeOverview")
|
|
|
|
|
.From("History")
|
|
|
|
|
.InnerJoin("Series")
|
|
|
|
|
.On("History.SeriesId = Series.SeriesId")
|
|
|
|
|
.InnerJoin("Episodes")
|
|
|
|
|
.On("History.EpisodeId = Episodes.EpisodeId");
|
|
|
|
|
|
|
|
|
|
var startPage = (pageRequest.DisplayLength == 0) ? 1 : pageRequest.DisplayStart / pageRequest.DisplayLength + 1;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(pageRequest.Search))
|
|
|
|
|
{
|
|
|
|
|
var whereClause = string.Join(" OR ", SqlBuilderHelper.GetSearchClause(pageRequest));
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(whereClause))
|
|
|
|
|
query.Append("WHERE " + whereClause, "%" + pageRequest.Search + "%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var orderBy = string.Join(",", SqlBuilderHelper.GetOrderByClause(pageRequest));
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(orderBy))
|
|
|
|
|
{
|
|
|
|
|
query.Append("ORDER BY " + orderBy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _database.Page<HistoryQueryModel>(startPage, pageRequest.DisplayLength, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual long Count()
|
|
|
|
|
{
|
|
|
|
|
return _database.Single<long>(@"SELECT COUNT(*) from History");
|
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|