using System; using System.Collections.Generic; using System.Linq; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Music; using NzbDrone.Core.Qualities; namespace NzbDrone.Core.History { public interface IHistoryRepository : IBasicRepository { EntityHistory MostRecentForAlbum(int albumId); EntityHistory MostRecentForDownloadId(string downloadId); List FindByDownloadId(string downloadId); List GetByArtist(int artistId, EntityHistoryEventType? eventType); List GetByAlbum(int albumId, EntityHistoryEventType? eventType); List FindDownloadHistory(int idArtistId, QualityModel quality); void DeleteForArtists(List artistIds); List Since(DateTime date, EntityHistoryEventType? eventType); } public class EntityHistoryRepository : BasicRepository, IHistoryRepository { public EntityHistoryRepository(IMainDatabase database, IEventAggregator eventAggregator) : base(database, eventAggregator) { } public EntityHistory MostRecentForAlbum(int albumId) { return Query(h => h.AlbumId == albumId) .OrderByDescending(h => h.Date) .FirstOrDefault(); } public EntityHistory MostRecentForDownloadId(string downloadId) { return Query(h => h.DownloadId == downloadId) .OrderByDescending(h => h.Date) .FirstOrDefault(); } public List FindByDownloadId(string downloadId) { return _database.QueryJoined( Builder() .Join((h, a) => h.ArtistId == a.Id) .Join((h, a) => h.AlbumId == a.Id) .Where(h => h.DownloadId == downloadId), (history, artist, album) => { history.Artist = artist; history.Album = album; return history; }).ToList(); } public List GetByArtist(int artistId, EntityHistoryEventType? eventType) { var builder = Builder().Where(h => h.ArtistId == artistId); if (eventType.HasValue) { builder.Where(h => h.EventType == eventType); } return Query(builder).OrderByDescending(h => h.Date).ToList(); } public List GetByAlbum(int albumId, EntityHistoryEventType? eventType) { var builder = Builder() .Join((h, a) => h.AlbumId == a.Id) .Where(h => h.AlbumId == albumId); if (eventType.HasValue) { builder.Where(h => h.EventType == eventType); } return _database.QueryJoined( builder, (history, album) => { history.Album = album; return history; }).OrderByDescending(h => h.Date).ToList(); } public List FindDownloadHistory(int idArtistId, QualityModel quality) { var allowed = new[] { (int)EntityHistoryEventType.Grabbed, (int)EntityHistoryEventType.DownloadFailed, (int)EntityHistoryEventType.TrackFileImported }; return Query(h => h.ArtistId == idArtistId && h.Quality == quality && allowed.Contains((int)h.EventType)); } public void DeleteForArtists(List artistIds) { Delete(c => artistIds.Contains(c.ArtistId)); } protected override SqlBuilder PagedBuilder() => new SqlBuilder(_database.DatabaseType) .Join((h, a) => h.ArtistId == a.Id) .Join((h, a) => h.AlbumId == a.Id) .LeftJoin((h, t) => h.TrackId == t.Id); protected override IEnumerable PagedQuery(SqlBuilder builder) => _database.QueryJoined(builder, (history, artist, album, track) => { history.Artist = artist; history.Album = album; history.Track = track; return history; }); public List Since(DateTime date, EntityHistoryEventType? eventType) { var builder = Builder().Where(x => x.Date >= date); if (eventType.HasValue) { builder.Where(h => h.EventType == eventType); } return Query(builder).OrderBy(h => h.Date).ToList(); } } }