Radarr/src/NzbDrone.Core/History/HistoryRepository.cs

122 lines
3.9 KiB
C#
Raw Normal View History

2018-02-26 15:08:44 +00:00
using System;
using System.Collections.Generic;
2013-02-23 21:29:22 +00:00
using System.Linq;
using Dapper;
2013-02-23 21:29:22 +00:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies;
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);
History MostRecentForDownloadId(string downloadId);
List<History> FindByDownloadId(string downloadId);
List<History> FindDownloadHistory(int movieId, QualityModel quality);
2018-11-23 07:03:32 +00:00
List<History> GetByMovieId(int movieId, HistoryEventType? eventType);
void DeleteForMovie(int movieId);
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
{
public HistoryRepository(IMainDatabase database, IEventAggregator eventAggregator)
: 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
{
var history = Query(x => x.MovieId == movieId);
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 History MostRecentForDownloadId(string downloadId)
{
return FindByDownloadId(downloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
public List<History> FindByDownloadId(string downloadId)
{
return Query(x => x.DownloadId == downloadId);
}
public List<History> FindDownloadHistory(int movieId, QualityModel quality)
{
var allowed = new [] { HistoryEventType.Grabbed, HistoryEventType.DownloadFailed, HistoryEventType.DownloadFolderImported };
return Query(h => h.MovieId == movieId &&
h.Quality == quality &&
allowed.Contains(h.EventType));
}
2018-11-23 07:03:32 +00:00
public List<History> GetByMovieId(int movieId, HistoryEventType? eventType)
{
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;
}
public void DeleteForMovie(int movieId)
{
Delete(c => c.MovieId == movieId);
}
private IEnumerable<History> SelectJoined(SqlBuilder.Template sql)
{
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
}
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);
public History MostRecentForMovie(int movieId)
{
return Query(x => x.MovieId == movieId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
2018-11-23 07:03:32 +00:00
public List<History> Since(DateTime date, HistoryEventType? eventType)
{
var builder = Builder().Where<History>(x => x.Date >= date);
2018-11-23 07:03:32 +00:00
if (eventType.HasValue)
{
builder.Where<History>(h => h.EventType == eventType);
2018-11-23 07:03:32 +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
}