diff --git a/src/NzbDrone.Core/History/HistoryRepository.cs b/src/NzbDrone.Core/History/HistoryRepository.cs index 4c17175f2..48b31cc75 100644 --- a/src/NzbDrone.Core/History/HistoryRepository.cs +++ b/src/NzbDrone.Core/History/HistoryRepository.cs @@ -16,6 +16,7 @@ namespace NzbDrone.Core.History List FindByDownloadId(string downloadId); List GetByArtist(int artistId, HistoryEventType? eventType); List GetByAlbum(int albumId, HistoryEventType? eventType); + List GetByTrack(int trackId, HistoryEventType? eventType); List FindDownloadHistory(int idArtistId, QualityModel quality); void DeleteForArtist(int artistId); void DeleteForAlbum(int albumId); @@ -83,6 +84,21 @@ namespace NzbDrone.Core.History return query; } + public List GetByTrack(int trackId, HistoryEventType? eventType) + { + var query = Query.Join(JoinType.Inner, h => h.Track, (h, e) => h.TrackId == e.Id) + .Where(h => h.TrackId == trackId); + + if (eventType.HasValue) + { + query.AndWhere(h => h.EventType == eventType); + } + + query.OrderByDescending(h => h.Date); + + return query; + } + public List FindDownloadHistory(int idArtistId, QualityModel quality) { return Query.Where(h => diff --git a/src/NzbDrone.Core/History/HistoryService.cs b/src/NzbDrone.Core/History/HistoryService.cs index 21ea8798c..4b7d4d632 100644 --- a/src/NzbDrone.Core/History/HistoryService.cs +++ b/src/NzbDrone.Core/History/HistoryService.cs @@ -24,6 +24,7 @@ namespace NzbDrone.Core.History History Get(int historyId); List GetByArtist(int artistId, HistoryEventType? eventType); List GetByAlbum(int albumId, HistoryEventType? eventType); + List GetByTrack(int trackId, HistoryEventType? eventType); List Find(string downloadId, HistoryEventType eventType); List FindByDownloadId(string downloadId); List Since(DateTime date, HistoryEventType? eventType); @@ -82,6 +83,11 @@ namespace NzbDrone.Core.History return _historyRepository.GetByAlbum(albumId, eventType); } + public List GetByTrack(int trackId, HistoryEventType? eventType) + { + return _historyRepository.GetByTrack(trackId, eventType); + } + public List Find(string downloadId, HistoryEventType eventType) { return _historyRepository.FindByDownloadId(downloadId).Where(c => c.EventType == eventType).ToList(); diff --git a/src/NzbDrone.Core/Music/Services/RefreshTrackService.cs b/src/NzbDrone.Core/Music/Services/RefreshTrackService.cs index c89cc3ed1..dade3b26f 100644 --- a/src/NzbDrone.Core/Music/Services/RefreshTrackService.cs +++ b/src/NzbDrone.Core/Music/Services/RefreshTrackService.cs @@ -1,4 +1,5 @@ using NLog; +using NzbDrone.Core.History; using NzbDrone.Core.MediaFiles; using System; using System.Collections.Generic; @@ -15,14 +16,17 @@ namespace NzbDrone.Core.Music { private readonly ITrackService _trackService; private readonly IAudioTagService _audioTagService; + private readonly IHistoryService _historyService; private readonly Logger _logger; public RefreshTrackService(ITrackService trackService, IAudioTagService audioTagService, + IHistoryService historyService, Logger logger) { _trackService = trackService; _audioTagService = audioTagService; + _historyService = historyService; _logger = logger; } @@ -52,6 +56,10 @@ namespace NzbDrone.Core.Music mergeTarget.TrackFileId = trackToMerge.TrackFileId; } + var items = _historyService.GetByTrack(trackToMerge.Id, null); + items.ForEach(x => x.TrackId = mergeTarget.Id); + _historyService.UpdateMany(items); + if (!updateList.Contains(mergeTarget)) { updateList.Add(mergeTarget);