2018-02-26 15:08:44 +00:00
|
|
|
using System;
|
2017-01-02 19:15:13 +00:00
|
|
|
using System.Collections.Generic;
|
2013-02-23 21:29:22 +00:00
|
|
|
using System.Linq;
|
2019-12-15 15:04:42 +00:00
|
|
|
using Dapper;
|
2013-02-23 21:29:22 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
2019-12-15 15:04:42 +00:00
|
|
|
using NzbDrone.Core.Profiles;
|
|
|
|
using NzbDrone.Core.Qualities;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.History
|
|
|
|
{
|
|
|
|
public interface IHistoryRepository : IBasicRepository<History>
|
|
|
|
{
|
2018-02-26 15:08:44 +00:00
|
|
|
List<QualityModel> GetBestQualityInHistory(int movieId);
|
2014-12-19 00:26:42 +00:00
|
|
|
History MostRecentForDownloadId(string downloadId);
|
|
|
|
List<History> FindByDownloadId(string downloadId);
|
2019-12-15 15:04:42 +00:00
|
|
|
List<History> FindDownloadHistory(int movieId, QualityModel quality);
|
2018-11-23 07:03:32 +00:00
|
|
|
List<History> GetByMovieId(int movieId, HistoryEventType? eventType);
|
2017-03-09 23:45:08 +00:00
|
|
|
void DeleteForMovie(int movieId);
|
2017-01-02 19:15:13 +00:00
|
|
|
History MostRecentForMovie(int movieId);
|
2018-11-23 07:03:32 +00:00
|
|
|
List<History> Since(DateTime date, HistoryEventType? eventType);
|
2013-02-23 21:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
|
|
|
|
{
|
2013-09-18 05:28:05 +00:00
|
|
|
|
2015-05-03 19:46:21 +00:00
|
|
|
public HistoryRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
2013-09-14 06:36:07 +00:00
|
|
|
: base(database, eventAggregator)
|
2013-02-23 21:29:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-26 15:08:44 +00:00
|
|
|
public List<QualityModel> GetBestQualityInHistory(int movieId)
|
2013-02-23 21:29:22 +00:00
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
var history = Query(x => x.MovieId == movieId);
|
2013-02-23 21:29:22 +00:00
|
|
|
|
2013-05-14 05:40:06 +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
|
|
|
|
2014-12-19 00:26:42 +00:00
|
|
|
public History MostRecentForDownloadId(string downloadId)
|
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
return FindByDownloadId(downloadId)
|
|
|
|
.OrderByDescending(h => h.Date)
|
|
|
|
.FirstOrDefault();
|
2014-12-19 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<History> FindByDownloadId(string downloadId)
|
2014-04-01 20:07:41 +00:00
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
return Query(x => x.DownloadId == downloadId);
|
2014-04-01 20:07:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 15:04:42 +00:00
|
|
|
public List<History> FindDownloadHistory(int movieId, QualityModel quality)
|
2014-04-01 20:07:41 +00:00
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
var allowed = new [] { HistoryEventType.Grabbed, HistoryEventType.DownloadFailed, HistoryEventType.DownloadFolderImported };
|
|
|
|
|
|
|
|
return Query(h => h.MovieId == movieId &&
|
|
|
|
h.Quality == quality &&
|
|
|
|
allowed.Contains(h.EventType));
|
2014-04-01 20:07:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
public List<History> GetByMovieId(int movieId, HistoryEventType? eventType)
|
2018-09-10 19:25:10 +00:00
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
var query = Query(x => x.MovieId == movieId);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
if (eventType.HasValue)
|
|
|
|
{
|
|
|
|
query = query.Where(h => h.EventType == eventType).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
query.OrderByDescending(h => h.Date);
|
|
|
|
|
|
|
|
return query;
|
2018-09-10 19:25:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 23:45:08 +00:00
|
|
|
public void DeleteForMovie(int movieId)
|
|
|
|
{
|
|
|
|
Delete(c => c.MovieId == movieId);
|
|
|
|
}
|
|
|
|
|
2019-12-15 15:04:42 +00:00
|
|
|
private IEnumerable<History> SelectJoined(SqlBuilder.Template sql)
|
2013-10-04 06:01:02 +00:00
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
return conn.Query<History, Movie, Profile, History>(
|
|
|
|
sql.RawSql,
|
|
|
|
(hist, movie, profile) => {
|
|
|
|
hist.Movie = movie;
|
|
|
|
hist.Movie.Profile = profile;
|
|
|
|
return hist;
|
|
|
|
},
|
|
|
|
sql.Parameters)
|
|
|
|
.ToList();
|
|
|
|
}
|
2013-05-10 22:33:04 +00:00
|
|
|
}
|
2017-01-02 19:15:13 +00:00
|
|
|
|
2019-12-15 15:04:42 +00:00
|
|
|
protected override SqlBuilder PagedBuilder() => new SqlBuilder()
|
|
|
|
.Join("Movies ON Movies.Id = History.MovieId")
|
|
|
|
.Join("Profiles ON Profiles.Id = Movies.ProfileId");
|
|
|
|
|
|
|
|
protected override IEnumerable<History> PagedSelector(SqlBuilder.Template sql) => SelectJoined(sql);
|
|
|
|
|
2017-01-02 19:15:13 +00:00
|
|
|
public History MostRecentForMovie(int movieId)
|
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
return Query(x => x.MovieId == movieId)
|
|
|
|
.OrderByDescending(h => h.Date)
|
|
|
|
.FirstOrDefault();
|
2017-01-02 19:15:13 +00:00
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
public List<History> Since(DateTime date, HistoryEventType? eventType)
|
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
var builder = Builder().Where<History>(x => x.Date >= date);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
if (eventType.HasValue)
|
|
|
|
{
|
2019-12-15 15:04:42 +00:00
|
|
|
builder.Where<History>(h => h.EventType == eventType);
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 15:04:42 +00:00
|
|
|
return Query(builder).OrderBy(h => h.Date).ToList();
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
}
|
2018-02-26 15:08:44 +00:00
|
|
|
}
|