private async Task CreateNewProfile(string title, FilteredProfileData profile, List <int> tagIds)
        {
            var newProfile = new SonarrReleaseProfile
            {
                Name    = title,
                Enabled = true
            };

            SetupProfileRequestObject(newProfile, profile, tagIds);
            await _api.CreateReleaseProfile(newProfile);
        }
        private async Task ProcessReleaseProfiles(IDictionary <string, ProfileData> profiles,
                                                  ReleaseProfileConfig config)
        {
            await DoVersionEnforcement();

            List <int> tagIds = new();

            // If tags were provided, ensure they exist. Tags that do not exist are added first, so that we
            // may specify them with the release profile request payload.
            if (config.Tags.Count > 0)
            {
                var sonarrTags = await _api.GetTags();
                await CreateMissingTags(sonarrTags, config.Tags);

                tagIds = sonarrTags.Where(t => config.Tags.Any(ct => ct.EqualsIgnoreCase(t.Label)))
                         .Select(t => t.Id)
                         .ToList();
            }

            // Obtain all of the existing release profiles first. If any were previously created by our program
            // here, we favor replacing those instead of creating new ones, which would just be mostly duplicates
            // (but with some differences, since there have likely been updates since the last run).
            var existingProfiles = await _api.GetReleaseProfiles();

            foreach (var(name, profileData) in profiles)
            {
                var filteredProfileData = new FilteredProfileData(profileData, config);
                var title           = BuildProfileTitle(config.Type, name);
                var profileToUpdate = GetProfileToUpdate(existingProfiles, title);
                if (profileToUpdate != null)
                {
                    Log.Information("Update existing profile: {ProfileName}", title);
                    await UpdateExistingProfile(profileToUpdate, filteredProfileData, tagIds);
                }
                else
                {
                    Log.Information("Create new profile: {ProfileName}", title);
                    await CreateNewProfile(title, filteredProfileData, tagIds);
                }
            }
        }
        private static void SetupProfileRequestObject(SonarrReleaseProfile profileToUpdate, FilteredProfileData profile,
                                                      List <int> tagIds)
        {
            profileToUpdate.Preferred = profile.Preferred
                                        .SelectMany(kvp => kvp.Value.Select(term => new SonarrPreferredTerm(kvp.Key, term)))
                                        .ToList();

            profileToUpdate.Ignored  = string.Join(',', profile.Ignored);
            profileToUpdate.Required = string.Join(',', profile.Required);

            // Null means the guide didn't specify a value for this, so we leave the existing setting intact.
            if (profile.IncludePreferredWhenRenaming != null)
            {
                profileToUpdate.IncludePreferredWhenRenaming = profile.IncludePreferredWhenRenaming.Value;
            }

            profileToUpdate.Tags = tagIds;
        }
 private async Task UpdateExistingProfile(SonarrReleaseProfile profileToUpdate, FilteredProfileData profile,
                                          List <int> tagIds)
 {
     Log.Debug("Update existing profile with id {ProfileId}", profileToUpdate.Id);
     SetupProfileRequestObject(profileToUpdate, profile, tagIds);
     await _api.UpdateReleaseProfile(profileToUpdate);
 }