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

114 lines
4.2 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 NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies;
2019-12-18 21:56:41 +00:00
using NzbDrone.Core.Profiles;
2019-12-22 22:08:53 +00:00
using NzbDrone.Core.Qualities;
2013-02-23 21:29:22 +00:00
namespace NzbDrone.Core.History
{
public interface IHistoryRepository : IBasicRepository<MovieHistory>
2013-02-23 21:29:22 +00:00
{
2018-02-26 15:08:44 +00:00
List<QualityModel> GetBestQualityInHistory(int movieId);
MovieHistory MostRecentForDownloadId(string downloadId);
List<MovieHistory> FindByDownloadId(string downloadId);
List<MovieHistory> FindDownloadHistory(int movieId, QualityModel quality);
List<MovieHistory> GetByMovieId(int movieId, MovieHistoryEventType? eventType);
void DeleteForMovies(List<int> movieIds);
MovieHistory MostRecentForMovie(int movieId);
List<MovieHistory> Since(DateTime date, MovieHistoryEventType? eventType);
2013-02-23 21:29:22 +00:00
}
public class HistoryRepository : BasicRepository<MovieHistory>, IHistoryRepository
2013-02-23 21:29:22 +00:00
{
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
{
2019-12-18 21:56:41 +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 MovieHistory MostRecentForDownloadId(string downloadId)
{
2019-12-18 21:56:41 +00:00
return FindByDownloadId(downloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
public List<MovieHistory> FindByDownloadId(string downloadId)
{
2019-12-18 21:56:41 +00:00
return Query(x => x.DownloadId == downloadId);
}
public List<MovieHistory> FindDownloadHistory(int movieId, QualityModel quality)
{
var allowed = new[] { (int)MovieHistoryEventType.Grabbed, (int)MovieHistoryEventType.DownloadFailed, (int)MovieHistoryEventType.DownloadFolderImported };
2019-12-18 21:56:41 +00:00
return Query(h => h.MovieId == movieId &&
h.Quality == quality &&
allowed.Contains((int)h.EventType));
}
public List<MovieHistory> GetByMovieId(int movieId, MovieHistoryEventType? eventType)
{
var builder = new SqlBuilder(_database.DatabaseType)
.Join<MovieHistory, Movie>((h, m) => h.MovieId == m.Id)
.Join<Movie, Profile>((m, p) => m.ProfileId == p.Id)
.Where<MovieHistory>(h => h.MovieId == movieId);
2018-11-23 07:03:32 +00:00
if (eventType.HasValue)
{
builder.Where<MovieHistory>(h => h.EventType == eventType);
2018-11-23 07:03:32 +00:00
}
return PagedQuery(builder).OrderByDescending(h => h.Date).ToList();
}
public void DeleteForMovies(List<int> movieIds)
{
Delete(c => movieIds.Contains(c.MovieId));
}
protected override SqlBuilder PagedBuilder() => new SqlBuilder(_database.DatabaseType)
.Join<MovieHistory, Movie>((h, m) => h.MovieId == m.Id)
.Join<Movie, Profile>((m, p) => m.ProfileId == p.Id);
protected override IEnumerable<MovieHistory> PagedQuery(SqlBuilder sql) =>
_database.QueryJoined<MovieHistory, Movie, Profile>(sql, (hist, movie, profile) =>
{
hist.Movie = movie;
hist.Movie.Profile = profile;
return hist;
});
2019-12-18 21:56:41 +00:00
public MovieHistory MostRecentForMovie(int movieId)
{
2019-12-18 21:56:41 +00:00
return Query(x => x.MovieId == movieId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
2018-11-23 07:03:32 +00:00
public List<MovieHistory> Since(DateTime date, MovieHistoryEventType? eventType)
2018-11-23 07:03:32 +00:00
{
var builder = new SqlBuilder(_database.DatabaseType)
.Join<MovieHistory, Movie>((h, m) => h.MovieId == m.Id)
.Join<Movie, Profile>((m, p) => m.ProfileId == p.Id)
.Where<MovieHistory>(x => x.Date >= date);
2018-11-23 07:03:32 +00:00
if (eventType.HasValue)
{
builder.Where<MovieHistory>(h => h.EventType == eventType);
2018-11-23 07:03:32 +00:00
}
return PagedQuery(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
}