BudgetAnalyser.Engine.Services.TransactionRuleService.AddRule C# (CSharp) Метод

AddRule() приватный Метод

private AddRule ( MatchingRule ruleToAdd ) : void
ruleToAdd MatchingRule
Результат void
        private void AddRule(MatchingRule ruleToAdd)
        {
            if (ruleToAdd == null)
            {
                throw new ArgumentNullException(nameof(ruleToAdd));
            }

            if (string.IsNullOrWhiteSpace(this.rulesStorageKey))
            {
                throw new InvalidOperationException("Unable to add a matching rule at this time, the service has not yet loaded a matching rule set.");
            }

            // Make sure no rule already exists with this id:
            if (MatchingRules.Any(r => r.RuleId == ruleToAdd.RuleId))
            {
                throw new DuplicateNameException($"Unable to add new matching rule: Rule ID {ruleToAdd.RuleId} already exists in the collection.");
            }

            // Check to see if an existing group object for the desired bucket already exists.
            var existingGroup = MatchingRulesGroupedByBucket.FirstOrDefault(group => group.Bucket == ruleToAdd.Bucket);
            if (existingGroup == null)
            {
                // Create a new group object for this bucket.
                var addNewGroup = new RulesGroupedByBucket(ruleToAdd.Bucket, new[] { ruleToAdd });
                this.matchingRulesGroupedByBucket.Add(addNewGroup);
                this.matchingRules.Add(ruleToAdd);
            }
            else
            {
                // Add to existing group object.
                if (existingGroup.Rules.Contains(ruleToAdd))
                {
                    this.logger.LogWarning(l => "Attempt to add new rule failed. Rule already exists in Grouped collection. " + ruleToAdd);
                    return;
                }
                existingGroup.Rules.Add(ruleToAdd);
                if (MatchingRules.Contains(ruleToAdd))
                {
                    this.logger.LogWarning(l => "Attempt to add new rule failed. Rule already exists in main collection. " + ruleToAdd);
                    return;
                }

                this.matchingRules.Add(ruleToAdd);
            }

            this.logger.LogInfo(_ => "Matching Rule Added: " + ruleToAdd);
        }