2017-09-24 19:44:25 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2017-05-07 22:50:07 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Music
|
|
|
|
{
|
|
|
|
public interface ITrackRepository : IBasicRepository<Track>
|
|
|
|
{
|
2017-06-20 00:57:07 +00:00
|
|
|
List<Track> GetTracks(int artistId);
|
2017-09-24 19:44:25 +00:00
|
|
|
List<Track> GetTracksByAlbum(int albumId);
|
2018-12-15 00:02:43 +00:00
|
|
|
List<Track> GetTracksByRelease(int albumReleaseId);
|
2019-02-16 14:49:24 +00:00
|
|
|
List<Track> GetTracksByReleases(List<int> albumReleaseId);
|
2019-03-15 12:10:45 +00:00
|
|
|
List<Track> GetTracksForRefresh(int albumReleaseId, IEnumerable<string> foreignTrackIds);
|
2017-05-07 22:50:07 +00:00
|
|
|
List<Track> GetTracksByFileId(int fileId);
|
2019-08-25 15:49:30 +00:00
|
|
|
List<Track> GetTracksByFileId(IEnumerable<int> ids);
|
2017-06-20 00:57:07 +00:00
|
|
|
List<Track> TracksWithFiles(int artistId);
|
2019-02-16 14:49:24 +00:00
|
|
|
List<Track> TracksWithoutFiles(int albumId);
|
|
|
|
void SetFileId(List<Track> tracks);
|
|
|
|
void DetachTrackFile(int trackFileId);
|
2017-05-07 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class TrackRepository : BasicRepository<Track>, ITrackRepository
|
|
|
|
{
|
|
|
|
private readonly IMainDatabase _database;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public TrackRepository(IMainDatabase database, IEventAggregator eventAggregator, Logger logger)
|
|
|
|
: base(database, eventAggregator)
|
|
|
|
{
|
|
|
|
_database = database;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-06-20 00:57:07 +00:00
|
|
|
public List<Track> GetTracks(int artistId)
|
2017-05-07 22:50:07 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
string query = string.Format("SELECT Tracks.* " +
|
|
|
|
"FROM Artists " +
|
|
|
|
"JOIN Albums ON Albums.ArtistMetadataId == Artists.ArtistMetadataId " +
|
|
|
|
"JOIN AlbumReleases ON AlbumReleases.AlbumId == Albums.Id " +
|
|
|
|
"JOIN Tracks ON Tracks.AlbumReleaseId == AlbumReleases.Id " +
|
|
|
|
"WHERE Artists.Id = {0} " +
|
|
|
|
"AND AlbumReleases.Monitored = 1",
|
|
|
|
artistId);
|
|
|
|
|
|
|
|
return Query.QueryText(query).ToList();
|
2017-05-07 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 19:44:25 +00:00
|
|
|
public List<Track> GetTracksByAlbum(int albumId)
|
2017-05-07 22:50:07 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
string query = string.Format("SELECT Tracks.* " +
|
|
|
|
"FROM Albums " +
|
|
|
|
"JOIN AlbumReleases ON AlbumReleases.AlbumId == Albums.Id " +
|
|
|
|
"JOIN Tracks ON Tracks.AlbumReleaseId == AlbumReleases.Id " +
|
|
|
|
"WHERE Albums.Id = {0} " +
|
|
|
|
"AND AlbumReleases.Monitored = 1",
|
|
|
|
albumId);
|
|
|
|
|
|
|
|
return Query.QueryText(query).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Track> GetTracksByRelease(int albumReleaseId)
|
|
|
|
{
|
|
|
|
return Query.Where(t => t.AlbumReleaseId == albumReleaseId).ToList();
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
public List<Track> GetTracksByReleases(List<int> albumReleaseIds)
|
|
|
|
{
|
|
|
|
// this will populate the artist metadata also
|
|
|
|
return Query
|
|
|
|
.Join<Track, ArtistMetadata>(Marr.Data.QGen.JoinType.Inner, t => t.ArtistMetadata, (l, r) => l.ArtistMetadataId == r.Id)
|
|
|
|
.Where($"[AlbumReleaseId] IN ({string.Join(", ", albumReleaseIds)})")
|
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:10:45 +00:00
|
|
|
public List<Track> GetTracksForRefresh(int albumReleaseId, IEnumerable<string> foreignTrackIds)
|
2018-12-15 00:02:43 +00:00
|
|
|
{
|
2019-03-15 12:10:45 +00:00
|
|
|
return Query
|
|
|
|
.Where(t => t.AlbumReleaseId == albumReleaseId)
|
|
|
|
.OrWhere($"[ForeignTrackId] IN ('{string.Join("', '", foreignTrackIds)}')")
|
|
|
|
.ToList();
|
2019-01-12 16:56:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 22:50:07 +00:00
|
|
|
public List<Track> GetTracksByFileId(int fileId)
|
|
|
|
{
|
|
|
|
return Query.Where(e => e.TrackFileId == fileId).ToList();
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:49:30 +00:00
|
|
|
public List<Track> GetTracksByFileId(IEnumerable<int> ids)
|
|
|
|
{
|
|
|
|
return Query.Where($"[TrackFileId] IN ({string.Join(", ", ids)})").ToList();
|
|
|
|
}
|
|
|
|
|
2017-06-20 00:57:07 +00:00
|
|
|
public List<Track> TracksWithFiles(int artistId)
|
2017-05-07 22:50:07 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
string query = string.Format("SELECT Tracks.* " +
|
|
|
|
"FROM Artists " +
|
|
|
|
"JOIN Albums ON Albums.ArtistMetadataId = Artists.ArtistMetadataId " +
|
|
|
|
"JOIN AlbumReleases ON AlbumReleases.AlbumId == Albums.Id " +
|
|
|
|
"JOIN Tracks ON Tracks.AlbumReleaseId == AlbumReleases.Id " +
|
|
|
|
"JOIN TrackFiles ON TrackFiles.Id == Tracks.TrackFileId " +
|
|
|
|
"WHERE Artists.Id == {0} " +
|
|
|
|
"AND AlbumReleases.Monitored = 1 ",
|
|
|
|
artistId);
|
2017-05-07 22:50:07 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
return Query.QueryText(query).ToList();
|
2017-05-07 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
public List<Track> TracksWithoutFiles(int albumId)
|
|
|
|
{
|
|
|
|
string query = string.Format("SELECT Tracks.* " +
|
|
|
|
"FROM Albums " +
|
|
|
|
"JOIN AlbumReleases ON AlbumReleases.AlbumId == Albums.Id " +
|
|
|
|
"JOIN Tracks ON Tracks.AlbumReleaseId == AlbumReleases.Id " +
|
|
|
|
"LEFT OUTER JOIN TrackFiles ON TrackFiles.Id == Tracks.TrackFileId " +
|
|
|
|
"WHERE Albums.Id == {0} " +
|
|
|
|
"AND AlbumReleases.Monitored = 1 " +
|
|
|
|
"AND TrackFiles.Id IS NULL",
|
|
|
|
albumId);
|
|
|
|
|
|
|
|
return Query.QueryText(query).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFileId(List<Track> tracks)
|
|
|
|
{
|
|
|
|
SetFields(tracks, t => t.TrackFileId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DetachTrackFile(int trackFileId)
|
2017-05-07 22:50:07 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
DataMapper.Update<Track>()
|
|
|
|
.Where(x => x.TrackFileId == trackFileId)
|
|
|
|
.ColumnsIncluding(x => x.TrackFileId)
|
|
|
|
.Entity(new Track { TrackFileId = 0 })
|
|
|
|
.Execute();
|
2017-05-07 22:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|