using System.Collections.Generic; using Marr.Data.QGen; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Music; namespace NzbDrone.Core.MediaFiles { public interface IMediaFileRepository : IBasicRepository { List GetFilesByArtist(int artistId); List GetFilesByAlbum(int albumId); List GetFilesByRelease(int releaseId); List GetFilesWithRelativePath(int artistId, string relativePath); } public class MediaFileRepository : BasicRepository, IMediaFileRepository { public MediaFileRepository(IMainDatabase database, IEventAggregator eventAggregator) : base(database, eventAggregator) { } // always join with all the other good stuff // needed more often than not so better to load it all now protected override QueryBuilder Query => DataMapper.Query() .Join(JoinType.Left, t => t.Tracks, (t, x) => t.Id == x.TrackFileId) .Join(JoinType.Left, t => t.Album, (t, a) => t.AlbumId == a.Id) .Join(JoinType.Left, t => t.Artist, (t, a) => t.Album.Value.ArtistMetadataId == a.ArtistMetadataId) .Join(JoinType.Left, a => a.Metadata, (a, m) => a.ArtistMetadataId == m.Id); public List GetFilesByArtist(int artistId) { return Query .Join(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id) .Where(r => r.Monitored == true) .AndWhere(t => t.Artist.Value.Id == artistId) .ToList(); } public List GetFilesByAlbum(int albumId) { return Query .Join(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id) .Where(r => r.Monitored == true) .AndWhere(f => f.AlbumId == albumId) .ToList(); } public List GetFilesByRelease(int releaseId) { return Query .Where(x => x.AlbumReleaseId == releaseId) .ToList(); } public List GetFilesWithRelativePath(int artistId, string relativePath) { return Query .Join(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id) .Where(r => r.Monitored == true) .AndWhere(t => t.Artist.Value.Id == artistId && t.RelativePath == relativePath) .ToList(); } } }