From ee3cfec50a36f5ce5bccdd6921e59af9bc770fe0 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 13 Nov 2021 19:23:48 -0600 Subject: [PATCH] refactor: use correct container types through code base From Sonarcloud code smell list --- src/Common/Extensions/CollectionExtensions.cs | 38 +++++++++++++++++++ .../Radarr/CustomFormat/CachePersisterTest.cs | 9 +++-- .../GuideSteps/CustomFormatStepTest.cs | 11 +++--- .../GuideSteps/QualityProfileStepTest.cs | 8 ++-- .../Radarr/RadarrConfigurationTest.cs | 9 +++-- .../ExceptionTypes/VersionException.cs | 7 ++++ .../Radarr/Config/RadarrConfiguration.cs | 8 ++-- .../Radarr/CustomFormat/CachePersister.cs | 1 + .../CustomFormat/CustomFormatUpdater.cs | 2 +- .../Models/Cache/CustomFormatCache.cs | 4 +- .../Models/ProcessedConfigData.cs | 7 +++- .../QualityProfileCustomFormatScoreMapping.cs | 2 +- .../CustomFormat/Processors/GuideProcessor.cs | 6 +-- .../Processors/GuideSteps/ConfigStep.cs | 19 +++++----- .../Processors/GuideSteps/CustomFormatStep.cs | 30 ++++++++------- .../Processors/GuideSteps/IConfigStep.cs | 4 +- .../GuideSteps/ICustomFormatStep.cs | 8 ++-- .../GuideSteps/IQualityProfileStep.cs | 4 +- .../GuideSteps/QualityProfileStep.cs | 9 +++-- .../Processors/IGuideProcessor.cs | 6 +-- .../PersistenceSteps/IJsonTransactionStep.cs | 2 +- .../PersistenceSteps/JsonTransactionStep.cs | 15 +++++--- 22 files changed, 137 insertions(+), 72 deletions(-) create mode 100644 src/Common/Extensions/CollectionExtensions.cs diff --git a/src/Common/Extensions/CollectionExtensions.cs b/src/Common/Extensions/CollectionExtensions.cs new file mode 100644 index 00000000..7951e32d --- /dev/null +++ b/src/Common/Extensions/CollectionExtensions.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Common.Extensions +{ + public static class CollectionExtensions + { + // From: https://stackoverflow.com/a/34362585/157971 + public static IReadOnlyCollection AsReadOnly(this ICollection source) + { + if (source is null) + { + throw new ArgumentNullException(nameof(source)); + } + + return source as IReadOnlyCollection ?? new ReadOnlyCollectionAdapter(source); + } + + // From: https://stackoverflow.com/a/34362585/157971 + private sealed class ReadOnlyCollectionAdapter : IReadOnlyCollection + { + private readonly ICollection _source; + public ReadOnlyCollectionAdapter(ICollection source) => _source = source; + public int Count => _source.Count; + public IEnumerator GetEnumerator() => _source.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + public static void AddRange(this ICollection destination, IEnumerable source) + { + foreach (var item in source) + { + destination.Add(item); + } + } + } +} diff --git a/src/TrashLib.Tests/Radarr/CustomFormat/CachePersisterTest.cs b/src/TrashLib.Tests/Radarr/CustomFormat/CachePersisterTest.cs index e99279a4..aac599da 100644 --- a/src/TrashLib.Tests/Radarr/CustomFormat/CachePersisterTest.cs +++ b/src/TrashLib.Tests/Radarr/CustomFormat/CachePersisterTest.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; using FluentAssertions; using Newtonsoft.Json.Linq; using NSubstitute; @@ -46,7 +47,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat var testCfObj = new CustomFormatCache { Version = versionToTest, - TrashIdMappings = new List {new("", "", 5)} + TrashIdMappings = new Collection {new("", "", 5)} }; ctx.ServiceCache.Load().Returns(testCfObj); ctx.Persister.Load(); @@ -61,7 +62,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat var testCfObj = new CustomFormatCache { Version = CustomFormatCache.LatestVersion, - TrashIdMappings = new List {new("", "", 5)} + TrashIdMappings = new Collection {new("", "", 5)} }; ctx.ServiceCache.Load().Returns(testCfObj); ctx.Persister.Load(); @@ -116,7 +117,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat // Load initial CfCache just to test that it gets replaced var testCfObj = new CustomFormatCache { - TrashIdMappings = new List {new("", "") {CustomFormatId = 5}} + TrashIdMappings = new Collection {new("", "") {CustomFormatId = 5}} }; ctx.ServiceCache.Load().Returns(testCfObj); ctx.Persister.Load(); @@ -133,7 +134,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat ctx.Persister.Update(customFormatData); ctx.Persister.CfCache.Should().BeEquivalentTo(new CustomFormatCache { - TrashIdMappings = new List {customFormatData[0].CacheEntry!} + TrashIdMappings = new Collection {customFormatData[0].CacheEntry!} }); customFormatData.Should().ContainSingle() diff --git a/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs b/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs index 4a76b69c..6c6ab859 100644 --- a/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs +++ b/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using FluentAssertions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -51,7 +52,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps var testCache = new CustomFormatCache { - TrashIdMappings = new List + TrashIdMappings = new Collection { new("id1", "name1") } @@ -97,7 +98,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps var testCache = new CustomFormatCache { - TrashIdMappings = new List + TrashIdMappings = new Collection { new("id1000", "name1") } @@ -183,7 +184,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps var testCache = new CustomFormatCache { - TrashIdMappings = new List {new("id1000", "name1")} + TrashIdMappings = new Collection {new("id1000", "name1")} }; var processor = new CustomFormatStep(); @@ -205,7 +206,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps { var cache = new CustomFormatCache { - TrashIdMappings = new List {new("id1", "3D", 9)} + TrashIdMappings = new Collection {new("id1", "3D", 9)} }; var guideCfs = new List @@ -237,7 +238,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps var testCache = new CustomFormatCache { - TrashIdMappings = new List {new("id1", "name1")} + TrashIdMappings = new Collection {new("id1", "name1")} }; var processor = new CustomFormatStep(); diff --git a/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStepTest.cs b/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStepTest.cs index 7c34f5de..f7dd89a9 100644 --- a/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStepTest.cs +++ b/src/TrashLib.Tests/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStepTest.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using FluentAssertions; using Newtonsoft.Json.Linq; using NUnit.Framework; @@ -61,7 +62,8 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps processor.ProfileScores.Should() .ContainKey("profile1").WhoseValue.Should() - .BeEquivalentTo(CfTestUtils.NewMapping(new FormatMappingEntry(testConfigData[0].CustomFormats[0], 50))); + .BeEquivalentTo( + CfTestUtils.NewMapping(new FormatMappingEntry(testConfigData[0].CustomFormats.First(), 50))); processor.CustomFormatsWithoutScore.Should().BeEmpty(); } @@ -89,7 +91,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps processor.Process(testConfigData); var expectedScoreEntries = - CfTestUtils.NewMapping(new FormatMappingEntry(testConfigData[0].CustomFormats[0], 100)); + CfTestUtils.NewMapping(new FormatMappingEntry(testConfigData[0].CustomFormats.First(), 100)); processor.ProfileScores.Should().BeEquivalentTo( new Dictionary @@ -124,7 +126,7 @@ namespace TrashLib.Tests.Radarr.CustomFormat.Processors.GuideSteps processor.ProfileScores.Should() .ContainKey("profile1").WhoseValue.Should() - .BeEquivalentTo(CfTestUtils.NewMapping(new FormatMappingEntry(testConfigData[0].CustomFormats[0], 0))); + .BeEquivalentTo(CfTestUtils.NewMapping(new FormatMappingEntry(testConfigData[0].CustomFormats.First(), 0))); processor.CustomFormatsWithoutScore.Should().BeEmpty(); } diff --git a/src/TrashLib.Tests/Radarr/RadarrConfigurationTest.cs b/src/TrashLib.Tests/Radarr/RadarrConfigurationTest.cs index ab2c49a5..c26a68ce 100644 --- a/src/TrashLib.Tests/Radarr/RadarrConfigurationTest.cs +++ b/src/TrashLib.Tests/Radarr/RadarrConfigurationTest.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Autofac; using FluentAssertions; @@ -28,13 +29,13 @@ namespace TrashLib.Tests.Radarr private static readonly TestCaseData[] NameOrIdsTestData = { - new(new List {"name"}, new List()), - new(new List(), new List {"trash_id"}) + new(new Collection {"name"}, new Collection()), + new(new Collection(), new Collection {"trash_id"}) }; [TestCaseSource(nameof(NameOrIdsTestData))] - public void Custom_format_is_valid_with_one_of_either_names_or_trash_id(List namesList, - List trashIdsList) + public void Custom_format_is_valid_with_one_of_either_names_or_trash_id(Collection namesList, + Collection trashIdsList) { var config = new RadarrConfiguration { diff --git a/src/TrashLib/ExceptionTypes/VersionException.cs b/src/TrashLib/ExceptionTypes/VersionException.cs index 0f0409af..c24e9980 100644 --- a/src/TrashLib/ExceptionTypes/VersionException.cs +++ b/src/TrashLib/ExceptionTypes/VersionException.cs @@ -1,12 +1,19 @@ using System; +using System.Runtime.Serialization; namespace TrashLib.ExceptionTypes { + [Serializable] public class VersionException : Exception { public VersionException(string msg) : base(msg) { } + + protected VersionException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/TrashLib/Radarr/Config/RadarrConfiguration.cs b/src/TrashLib/Radarr/Config/RadarrConfiguration.cs index 988ec9bd..9d4971be 100644 --- a/src/TrashLib/Radarr/Config/RadarrConfiguration.cs +++ b/src/TrashLib/Radarr/Config/RadarrConfiguration.cs @@ -9,16 +9,16 @@ namespace TrashLib.Radarr.Config public class RadarrConfiguration : ServiceConfiguration { public QualityDefinitionConfig? QualityDefinition { get; init; } - public List CustomFormats { get; init; } = new(); + public ICollection CustomFormats { get; init; } = new List(); public bool DeleteOldCustomFormats { get; init; } } [UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] public class CustomFormatConfig { - public List Names { get; init; } = new(); - public List TrashIds { get; init; } = new(); - public List QualityProfiles { get; init; } = new(); + public ICollection Names { get; init; } = new List(); + public ICollection TrashIds { get; init; } = new List(); + public ICollection QualityProfiles { get; init; } = new List(); } [UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] diff --git a/src/TrashLib/Radarr/CustomFormat/CachePersister.cs b/src/TrashLib/Radarr/CustomFormat/CachePersister.cs index 9246cecb..8551e833 100644 --- a/src/TrashLib/Radarr/CustomFormat/CachePersister.cs +++ b/src/TrashLib/Radarr/CustomFormat/CachePersister.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Common.Extensions; using Serilog; using TrashLib.Cache; using TrashLib.Radarr.CustomFormat.Models; diff --git a/src/TrashLib/Radarr/CustomFormat/CustomFormatUpdater.cs b/src/TrashLib/Radarr/CustomFormat/CustomFormatUpdater.cs index 64247388..86dfc386 100644 --- a/src/TrashLib/Radarr/CustomFormat/CustomFormatUpdater.cs +++ b/src/TrashLib/Radarr/CustomFormat/CustomFormatUpdater.cs @@ -33,7 +33,7 @@ namespace TrashLib.Radarr.CustomFormat { _cache.Load(); - await _guideProcessor.BuildGuideDataAsync(config.CustomFormats, _cache.CfCache); + await _guideProcessor.BuildGuideDataAsync(config.CustomFormats.AsReadOnly(), _cache.CfCache); if (!ValidateGuideDataAndCheckShouldProceed(config)) { diff --git a/src/TrashLib/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs b/src/TrashLib/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs index 990715d4..5231a6aa 100644 --- a/src/TrashLib/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs +++ b/src/TrashLib/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.ObjectModel; using TrashLib.Cache; namespace TrashLib.Radarr.CustomFormat.Models.Cache @@ -9,7 +9,7 @@ namespace TrashLib.Radarr.CustomFormat.Models.Cache public const int LatestVersion = 1; public int Version { get; init; } = LatestVersion; - public List TrashIdMappings { get; init; } = new(); + public Collection TrashIdMappings { get; init; } = new(); } public class TrashIdMapping diff --git a/src/TrashLib/Radarr/CustomFormat/Models/ProcessedConfigData.cs b/src/TrashLib/Radarr/CustomFormat/Models/ProcessedConfigData.cs index 4cc5a06e..832ad755 100644 --- a/src/TrashLib/Radarr/CustomFormat/Models/ProcessedConfigData.cs +++ b/src/TrashLib/Radarr/CustomFormat/Models/ProcessedConfigData.cs @@ -5,7 +5,10 @@ namespace TrashLib.Radarr.CustomFormat.Models { public class ProcessedConfigData { - public List CustomFormats { get; init; } = new(); - public List QualityProfiles { get; init; } = new(); + public ICollection CustomFormats { get; init; } + = new List(); + + public ICollection QualityProfiles { get; init; } + = new List(); } } diff --git a/src/TrashLib/Radarr/CustomFormat/Models/QualityProfileCustomFormatScoreMapping.cs b/src/TrashLib/Radarr/CustomFormat/Models/QualityProfileCustomFormatScoreMapping.cs index 835e4dfc..2db37b26 100644 --- a/src/TrashLib/Radarr/CustomFormat/Models/QualityProfileCustomFormatScoreMapping.cs +++ b/src/TrashLib/Radarr/CustomFormat/Models/QualityProfileCustomFormatScoreMapping.cs @@ -12,6 +12,6 @@ namespace TrashLib.Radarr.CustomFormat.Models } public bool ResetUnmatchedScores { get; } - public List Mapping { get; init; } = new(); + public ICollection Mapping { get; init; } = new List(); } } diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideProcessor.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideProcessor.cs index a486816c..9447c0a9 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideProcessor.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideProcessor.cs @@ -53,13 +53,13 @@ namespace TrashLib.Radarr.CustomFormat.Processors public IReadOnlyCollection DeletedCustomFormatsInCache => _steps.CustomFormat.DeletedCustomFormatsInCache; - public List<(string, string)> CustomFormatsWithOutdatedNames + public IReadOnlyCollection<(string, string)> CustomFormatsWithOutdatedNames => _steps.CustomFormat.CustomFormatsWithOutdatedNames; - public Dictionary> DuplicatedCustomFormats + public IDictionary> DuplicatedCustomFormats => _steps.CustomFormat.DuplicatedCustomFormats; - public async Task BuildGuideDataAsync(IReadOnlyList config, CustomFormatCache? cache) + public async Task BuildGuideDataAsync(IReadOnlyCollection config, CustomFormatCache? cache) { if (_guideCustomFormatJson == null) { diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ConfigStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ConfigStep.cs index 34d975bd..6be2f1ce 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ConfigStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ConfigStep.cs @@ -9,8 +9,11 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { internal class ConfigStep : IConfigStep { - public List CustomFormatsNotInGuide { get; } = new(); - public List ConfigData { get; } = new(); + private readonly List _configData = new(); + private readonly List _customFormatsNotInGuide = new(); + + public IReadOnlyCollection CustomFormatsNotInGuide => _customFormatsNotInGuide; + public IReadOnlyCollection ConfigData => _configData; public void Process(IReadOnlyCollection processedCfs, IEnumerable config) @@ -24,7 +27,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps var match = FindCustomFormatByName(processedCfs, name); if (match == null) { - CustomFormatsNotInGuide.Add(name); + _customFormatsNotInGuide.Add(name); } else { @@ -37,7 +40,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps var match = processedCfs.FirstOrDefault(cf => cf.TrashId.EqualsIgnoreCase(trashId)); if (match == null) { - CustomFormatsNotInGuide.Add(trashId); + _customFormatsNotInGuide.Add(trashId); } else { @@ -45,7 +48,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps } } - ConfigData.Add(new ProcessedConfigData + _configData.Add(new ProcessedConfigData { QualityProfiles = singleConfig.QualityProfiles, CustomFormats = validCfs @@ -58,10 +61,8 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps private static ProcessedCustomFormatData? FindCustomFormatByName( IReadOnlyCollection processedCfs, string name) { - return processedCfs.FirstOrDefault( - cf => cf.CacheEntry?.CustomFormatName.EqualsIgnoreCase(name) ?? false) ?? - processedCfs.FirstOrDefault( - cf => cf.Name.EqualsIgnoreCase(name)); + return processedCfs.FirstOrDefault(cf => cf.CacheEntry?.CustomFormatName.EqualsIgnoreCase(name) ?? false) + ?? processedCfs.FirstOrDefault(cf => cf.Name.EqualsIgnoreCase(name)); } } } diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs index c00f1a07..ffc96596 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs @@ -12,12 +12,15 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { internal class CustomFormatStep : ICustomFormatStep { - public List<(string, string)> CustomFormatsWithOutdatedNames { get; } = new(); - public List ProcessedCustomFormats { get; } = new(); - public List DeletedCustomFormatsInCache { get; } = new(); + private readonly List<(string, string)> _customFormatsWithOutdatedNames = new(); + private readonly List _processedCustomFormats = new(); + private readonly List _deletedCustomFormatsInCache = new(); + private readonly Dictionary> _duplicatedCustomFormats = new(); - public Dictionary> DuplicatedCustomFormats { get; private set; } = - new(); + public IReadOnlyCollection<(string, string)> CustomFormatsWithOutdatedNames => _customFormatsWithOutdatedNames; + public IReadOnlyCollection ProcessedCustomFormats => _processedCustomFormats; + public IReadOnlyCollection DeletedCustomFormatsInCache => _deletedCustomFormatsInCache; + public IDictionary> DuplicatedCustomFormats => _duplicatedCustomFormats; public void Process(IEnumerable customFormatGuideData, IReadOnlyCollection config, CustomFormatCache? cache) @@ -27,7 +30,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps .ToList(); // For each ID listed under the `trash_ids` YML property, match it to an existing CF - ProcessedCustomFormats.AddRange(config + _processedCustomFormats.AddRange(config .SelectMany(c => c.TrashIds) .Distinct(StringComparer.CurrentCultureIgnoreCase) .Join(processedCfs, @@ -60,7 +63,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps cf.CacheEntry.CustomFormatName = cf.Name; } - ProcessedCustomFormats.Add(cf); + _processedCustomFormats.Add(cf); continue; } @@ -69,8 +72,8 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps if (configName != null) { // Config name is out of sync with the guide and should be updated - CustomFormatsWithOutdatedNames.Add((configName, cf.Name)); - ProcessedCustomFormats.Add(cf); + _customFormatsWithOutdatedNames.Add((configName, cf.Name)); + _processedCustomFormats.Add(cf); } // If we get here, we can't find a match in the config using cache or guide name, so the user must have @@ -86,12 +89,13 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps private void ProcessDuplicates() { - DuplicatedCustomFormats = ProcessedCustomFormats + _duplicatedCustomFormats.Clear(); + _duplicatedCustomFormats.AddRange(ProcessedCustomFormats .GroupBy(cf => cf.Name) .Where(grp => grp.Count() > 1) - .ToDictionary(grp => grp.Key, grp => grp.ToList()); + .ToDictionary(grp => grp.Key, grp => grp.ToList())); - ProcessedCustomFormats.RemoveAll(cf => DuplicatedCustomFormats.ContainsKey(cf.Name)); + _processedCustomFormats.RemoveAll(cf => DuplicatedCustomFormats.ContainsKey(cf.Name)); } private static ProcessedCustomFormatData ProcessCustomFormatData(string guideData, CustomFormatCache? cache) @@ -129,7 +133,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps => cf.CacheEntry != null && cf.CacheEntry.TrashId == c.TrashId; // Delete if CF is in cache and not in the guide or config - DeletedCustomFormatsInCache.AddRange(cache.TrashIdMappings + _deletedCustomFormatsInCache.AddRange(cache.TrashIdMappings .Where(c => !ProcessedCustomFormats.Any(cf => MatchCfInCache(cf, c)))); } } diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IConfigStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IConfigStep.cs index 774bbd54..eddc5492 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IConfigStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IConfigStep.cs @@ -6,8 +6,8 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { public interface IConfigStep { - List CustomFormatsNotInGuide { get; } - List ConfigData { get; } + IReadOnlyCollection CustomFormatsNotInGuide { get; } + IReadOnlyCollection ConfigData { get; } void Process(IReadOnlyCollection processedCfs, IEnumerable config); diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ICustomFormatStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ICustomFormatStep.cs index 5ccd1af5..67f72e9a 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ICustomFormatStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/ICustomFormatStep.cs @@ -7,10 +7,10 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { public interface ICustomFormatStep { - List ProcessedCustomFormats { get; } - List DeletedCustomFormatsInCache { get; } - List<(string, string)> CustomFormatsWithOutdatedNames { get; } - Dictionary> DuplicatedCustomFormats { get; } + IReadOnlyCollection ProcessedCustomFormats { get; } + IReadOnlyCollection DeletedCustomFormatsInCache { get; } + IReadOnlyCollection<(string, string)> CustomFormatsWithOutdatedNames { get; } + IDictionary> DuplicatedCustomFormats { get; } void Process(IEnumerable customFormatGuideData, IReadOnlyCollection config, CustomFormatCache? cache); diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IQualityProfileStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IQualityProfileStep.cs index 9ee8de5f..2bf4cd66 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IQualityProfileStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/IQualityProfileStep.cs @@ -5,8 +5,8 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { public interface IQualityProfileStep { - Dictionary ProfileScores { get; } - List<(string name, string trashId, string profileName)> CustomFormatsWithoutScore { get; } + IDictionary ProfileScores { get; } + IReadOnlyCollection<(string name, string trashId, string profileName)> CustomFormatsWithoutScore { get; } void Process(IEnumerable configData); } } diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStep.cs index d2d975d9..8a767896 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/GuideSteps/QualityProfileStep.cs @@ -5,8 +5,11 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { internal class QualityProfileStep : IQualityProfileStep { - public Dictionary ProfileScores { get; } = new(); - public List<(string name, string trashId, string profileName)> CustomFormatsWithoutScore { get; } = new(); + private readonly Dictionary _profileScores = new(); + private readonly List<(string name, string trashId, string profileName)> _customFormatsWithoutScore = new(); + + public IDictionary ProfileScores => _profileScores; + public IReadOnlyCollection<(string name, string trashId, string profileName)> CustomFormatsWithoutScore => _customFormatsWithoutScore; public void Process(IEnumerable configData) { @@ -23,7 +26,7 @@ namespace TrashLib.Radarr.CustomFormat.Processors.GuideSteps { if (cf.Score == null) { - CustomFormatsWithoutScore.Add((cf.Name, cf.TrashId, profile.Name)); + _customFormatsWithoutScore.Add((cf.Name, cf.TrashId, profile.Name)); } else { diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/IGuideProcessor.cs b/src/TrashLib/Radarr/CustomFormat/Processors/IGuideProcessor.cs index d3b10fd4..f979e3aa 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/IGuideProcessor.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/IGuideProcessor.cs @@ -14,10 +14,10 @@ namespace TrashLib.Radarr.CustomFormat.Processors IDictionary ProfileScores { get; } IReadOnlyCollection<(string name, string trashId, string profileName)> CustomFormatsWithoutScore { get; } IReadOnlyCollection DeletedCustomFormatsInCache { get; } - List<(string, string)> CustomFormatsWithOutdatedNames { get; } - Dictionary> DuplicatedCustomFormats { get; } + IReadOnlyCollection<(string, string)> CustomFormatsWithOutdatedNames { get; } + IDictionary> DuplicatedCustomFormats { get; } - Task BuildGuideDataAsync(IReadOnlyList config, CustomFormatCache? cache); + Task BuildGuideDataAsync(IReadOnlyCollection config, CustomFormatCache? cache); void Reset(); } } diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/IJsonTransactionStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/IJsonTransactionStep.cs index edbb0e16..fbebc789 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/IJsonTransactionStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/IJsonTransactionStep.cs @@ -12,6 +12,6 @@ namespace TrashLib.Radarr.CustomFormat.Processors.PersistenceSteps void Process(IEnumerable guideCfs, IReadOnlyCollection radarrCfs); - void RecordDeletions(IEnumerable deletedCfsInCache, List radarrCfs); + void RecordDeletions(IEnumerable deletedCfsInCache, IEnumerable radarrCfs); } } diff --git a/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs b/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs index 9c14d57a..5c79eb90 100644 --- a/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs +++ b/src/TrashLib/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Common.Extensions; using Newtonsoft.Json.Linq; @@ -9,10 +10,10 @@ namespace TrashLib.Radarr.CustomFormat.Processors.PersistenceSteps { public class CustomFormatTransactionData { - public List NewCustomFormats { get; } = new(); - public List UpdatedCustomFormats { get; } = new(); - public List DeletedCustomFormatIds { get; } = new(); - public List UnchangedCustomFormats { get; } = new(); + public Collection NewCustomFormats { get; } = new(); + public Collection UpdatedCustomFormats { get; } = new(); + public Collection DeletedCustomFormatIds { get; } = new(); + public Collection UnchangedCustomFormats { get; } = new(); } internal class JsonTransactionStep : IJsonTransactionStep @@ -59,12 +60,14 @@ namespace TrashLib.Radarr.CustomFormat.Processors.PersistenceSteps } } - public void RecordDeletions(IEnumerable deletedCfsInCache, List radarrCfs) + public void RecordDeletions(IEnumerable deletedCfsInCache, IEnumerable radarrCfs) { + var cfs = radarrCfs.ToList(); + // The 'Where' excludes cached CFs that were deleted manually by the user in Radarr // FindRadarrCf() specifies 'null' for name because we should never delete unless an ID is found foreach (var del in deletedCfsInCache.Where( - del => FindRadarrCf(radarrCfs, del.CustomFormatId, null) != null)) + del => FindRadarrCf(cfs, del.CustomFormatId, null) != null)) { Transactions.DeletedCustomFormatIds.Add(del); }