From f4e4bae465c79b36f76826d0687017241f25cd4a Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 22 Sep 2019 17:50:14 -0400 Subject: [PATCH] Fixed: Artist FindById Should Find OldIds --- .../ArtistRepositoryFixture.cs | 41 +++++++++++++++---- .../ImportLists/ImportListSyncService.cs | 27 ++++++++---- src/NzbDrone.Core/Music/ArtistRepository.cs | 11 ++++- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/NzbDrone.Core.Test/MusicTests/ArtistRepositoryTests/ArtistRepositoryFixture.cs b/src/NzbDrone.Core.Test/MusicTests/ArtistRepositoryTests/ArtistRepositoryFixture.cs index 3768c1078..2338c917a 100644 --- a/src/NzbDrone.Core.Test/MusicTests/ArtistRepositoryTests/ArtistRepositoryFixture.cs +++ b/src/NzbDrone.Core.Test/MusicTests/ArtistRepositoryTests/ArtistRepositoryFixture.cs @@ -19,7 +19,6 @@ namespace NzbDrone.Core.Test.MusicTests.ArtistRepositoryTests { private ArtistRepository _artistRepo; private ArtistMetadataRepository _artistMetadataRepo; - private int _id = 1; [SetUp] public void Setup() @@ -28,20 +27,25 @@ namespace NzbDrone.Core.Test.MusicTests.ArtistRepositoryTests _artistMetadataRepo = Mocker.Resolve(); } - private void AddArtist(string name) + private void AddArtist(string name, string foreignId, List oldIds = null) { + if (oldIds == null) + { + oldIds = new List(); + } + var metadata = Builder.CreateNew() .With(a => a.Id = 0) .With(a => a.Name = name) + .With(a=> a.OldForeignArtistIds = oldIds) .BuildNew(); var artist = Builder.CreateNew() .With(a => a.Id = 0) .With(a => a.Metadata = metadata) .With(a => a.CleanName = Parser.Parser.CleanArtistName(name)) - .With(a => a.ForeignArtistId = _id.ToString()) + .With(a => a.ForeignArtistId = foreignId) .BuildNew(); - _id++; _artistMetadataRepo.Insert(metadata); artist.ArtistMetadataId = metadata.Id; @@ -50,8 +54,8 @@ namespace NzbDrone.Core.Test.MusicTests.ArtistRepositoryTests private void GivenArtists() { - AddArtist("The Black Eyed Peas"); - AddArtist("The Black Keys"); + AddArtist("The Black Eyed Peas", "d5be5333-4171-427e-8e12-732087c6b78e"); + AddArtist("The Black Keys", "d15721d8-56b4-453d-b506-fc915b14cba2", new List { "6f2ed437-825c-4cea-bb58-bf7688c6317a" }); } [Test] @@ -100,14 +104,35 @@ namespace NzbDrone.Core.Test.MusicTests.ArtistRepositoryTests artist.Name.Should().Be(name); } + [Test] + public void should_find_artist_in_by_id() + { + GivenArtists(); + var artist = _artistRepo.FindById("d5be5333-4171-427e-8e12-732087c6b78e"); + + artist.Should().NotBeNull(); + artist.ForeignArtistId.Should().Be("d5be5333-4171-427e-8e12-732087c6b78e"); + } + + [Test] + public void should_find_artist_in_by_old_id() + { + GivenArtists(); + var artist = _artistRepo.FindById("6f2ed437-825c-4cea-bb58-bf7688c6317a"); + + artist.Should().NotBeNull(); + artist.Name.Should().Be("The Black Keys"); + artist.ForeignArtistId.Should().Be("d15721d8-56b4-453d-b506-fc915b14cba2"); + } + [Test] public void should_not_find_artist_if_multiple_artists_have_same_name() { GivenArtists(); string name = "Alice Cooper"; - AddArtist(name); - AddArtist(name); + AddArtist(name, "ee58c59f-8e7f-4430-b8ca-236c4d3745ae"); + AddArtist(name, "4d7928cd-7ed2-4282-8c29-c0c9f966f1bd"); _artistRepo.All().Should().HaveCount(4); diff --git a/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs b/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs index f5cd4dff1..ba284096f 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs @@ -14,7 +14,6 @@ namespace NzbDrone.Core.ImportLists { public class ImportListSyncService : IExecute { - private readonly IImportListStatusService _importListStatusService; private readonly IImportListFactory _importListFactory; private readonly IImportListExclusionService _importListExclusionService; private readonly IFetchAndParseImportList _listFetcherAndParser; @@ -25,8 +24,7 @@ namespace NzbDrone.Core.ImportLists private readonly IEventAggregator _eventAggregator; private readonly Logger _logger; - public ImportListSyncService(IImportListStatusService importListStatusService, - IImportListFactory importListFactory, + public ImportListSyncService(IImportListFactory importListFactory, IImportListExclusionService importListExclusionService, IFetchAndParseImportList listFetcherAndParser, ISearchForNewAlbum albumSearchService, @@ -36,7 +34,6 @@ namespace NzbDrone.Core.ImportLists IEventAggregator eventAggregator, Logger logger) { - _importListStatusService = importListStatusService; _importListFactory = importListFactory; _importListExclusionService = importListExclusionService; _listFetcherAndParser = listFetcherAndParser; @@ -118,22 +115,33 @@ namespace NzbDrone.Core.ImportLists // Check to see if artist in DB var existingArtist = _artistService.FindById(report.ArtistMusicBrainzId); - + + // TODO: Rework this for albums when we can add albums seperate from Artists + // (If list contains albums we should not break for an existing artist, we should add new albums that are not in DB) + if (existingArtist != null) + { + _logger.Debug("{0} [{1}] Rejected, Artist Exists in DB", report.ArtistMusicBrainzId, report.Artist); + continue; + } + // Check to see if artist excluded var excludedArtist = listExclusions.Where(s => s.ForeignId == report.ArtistMusicBrainzId).SingleOrDefault(); if (excludedArtist != null) { _logger.Debug("{0} [{1}] Rejected due to list exlcusion", report.ArtistMusicBrainzId, report.Artist); + continue; } // Append Artist if not already in DB or already on add list - if (existingArtist == null && excludedArtist == null && artistsToAdd.All(s => s.Metadata.Value.ForeignArtistId != report.ArtistMusicBrainzId)) + if (artistsToAdd.All(s => s.Metadata.Value.ForeignArtistId != report.ArtistMusicBrainzId)) { var monitored = importList.ShouldMonitor != ImportListMonitorType.None; + artistsToAdd.Add(new Artist { - Metadata = new ArtistMetadata { + Metadata = new ArtistMetadata + { ForeignArtistId = report.ArtistMusicBrainzId, Name = report.Artist }, @@ -143,7 +151,8 @@ namespace NzbDrone.Core.ImportLists MetadataProfileId = importList.MetadataProfileId, Tags = importList.Tags, AlbumFolder = true, - AddOptions = new AddArtistOptions { + AddOptions = new AddArtistOptions + { SearchForMissingAlbums = monitored, Monitored = monitored, Monitor = monitored ? MonitorTypes.All : MonitorTypes.None @@ -160,7 +169,7 @@ namespace NzbDrone.Core.ImportLists _addArtistService.AddArtists(artistsToAdd); - var message = string.Format("Import List Sync Completed. Reports found: {0}, Reports grabbed: {1}", reports.Count, processed.Count); + var message = string.Format("Import List Sync Completed. Items found: {0}, Artists added: {1}", reports.Count, artistsToAdd.Count); _logger.ProgressInfo(message); diff --git a/src/NzbDrone.Core/Music/ArtistRepository.cs b/src/NzbDrone.Core/Music/ArtistRepository.cs index 21326f124..c5bee6ecd 100644 --- a/src/NzbDrone.Core/Music/ArtistRepository.cs +++ b/src/NzbDrone.Core/Music/ArtistRepository.cs @@ -31,7 +31,16 @@ namespace NzbDrone.Core.Music public Artist FindById(string foreignArtistId) { - return Query.Where(m => m.ForeignArtistId == foreignArtistId).SingleOrDefault(); + var artist = Query.Where(m => m.ForeignArtistId == foreignArtistId).SingleOrDefault(); + + if (artist == null) + { + var id = "\"" + foreignArtistId + "\""; + artist = Query.Where(x => x.OldForeignArtistIds.Contains(id)) + .SingleOrDefault(); + } + + return artist; } public Artist FindByName(string cleanName)