using System.Collections.Generic; using System.Linq; using NzbDrone.Common.Cache; using NzbDrone.Common.Extensions; using NzbDrone.Core.Music; using NzbDrone.Core.RootFolders; namespace NzbDrone.Core.AutoTagging { public interface IAutoTaggingService { void Update(AutoTag autoTag); AutoTag Insert(AutoTag autoTag); List All(); AutoTag GetById(int id); void Delete(int id); List AllForTag(int tagId); AutoTaggingChanges GetTagChanges(Artist artist); } public class AutoTaggingService : IAutoTaggingService { private readonly IAutoTaggingRepository _repository; private readonly RootFolderService _rootFolderService; private readonly ICached> _cache; public AutoTaggingService(IAutoTaggingRepository repository, RootFolderService rootFolderService, ICacheManager cacheManager) { _repository = repository; _rootFolderService = rootFolderService; _cache = cacheManager.GetCache>(typeof(AutoTag), "autoTags"); } private Dictionary AllDictionary() { return _cache.Get("all", () => _repository.All().ToDictionary(m => m.Id)); } public List All() { return AllDictionary().Values.ToList(); } public AutoTag GetById(int id) { return AllDictionary()[id]; } public void Update(AutoTag autoTag) { _repository.Update(autoTag); _cache.Clear(); } public AutoTag Insert(AutoTag autoTag) { var result = _repository.Insert(autoTag); _cache.Clear(); return result; } public void Delete(int id) { _repository.Delete(id); _cache.Clear(); } public List AllForTag(int tagId) { return All().Where(p => p.Tags.Contains(tagId)) .ToList(); } public AutoTaggingChanges GetTagChanges(Artist artist) { var autoTags = All(); var changes = new AutoTaggingChanges(); if (autoTags.Empty()) { return changes; } // Set the root folder path on the series artist.RootFolderPath = _rootFolderService.GetBestRootFolderPath(artist.Path); foreach (var autoTag in autoTags) { var specificationMatches = autoTag.Specifications .GroupBy(t => t.GetType()) .Select(g => new SpecificationMatchesGroup { Matches = g.ToDictionary(t => t, t => t.IsSatisfiedBy(artist)) }) .ToList(); var allMatch = specificationMatches.All(x => x.DidMatch); var tags = autoTag.Tags; if (allMatch) { foreach (var tag in tags) { if (!artist.Tags.Contains(tag)) { changes.TagsToAdd.Add(tag); } } continue; } if (autoTag.RemoveTagsAutomatically) { foreach (var tag in tags) { changes.TagsToRemove.Add(tag); } } } return changes; } } }