/// <summary> /// Maps a <see cref="LoggingRule" /> entity to a <see cref="RuleResult" /> domain object. /// </summary> /// <param name="rule">The rule entity to map to a domain object.</param> /// <returns> /// The <see cref="RuleResult" /> domain object. /// </returns> public static RuleItem ToRuleItem(LoggingRule rule) { Debug.Assert(rule != null, "The rule cannot be null."); var result = new RuleItem { Id = rule.Id }; var options = LoggingRuleOptionsHelpers.Deserialize(rule.Options); if (options.Conditions != null) { foreach (var condition in options.Conditions) { result.Conditions.Add(condition); } } if (options.Actions != null) { foreach (var action in options.Actions) { result.Actions.Add(action); } } return(result); }
/// <summary> /// Inserts a rule. /// </summary> /// <param name="rule">The rule to insert.</param> /// <param name="user">The user who owns the rule.</param> /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous operation. /// </returns> public virtual async Task <ValidationResult> CreateAsync(RuleItem rule, AccountItem user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (rule == null) { throw new ArgumentNullException("rule"); } if (user == null) { throw new ArgumentNullException(nameof(user)); } var ruleEntity = new LoggingRule { UserId = user.Id, Options = LoggingRuleOptionsHelpers.Serialize(new LoggingRuleOptions { Conditions = rule.Conditions, Actions = rule.Actions }) }; Context.Add(ruleEntity); await SaveChangesAsync(cancellationToken); rule.Id = ruleEntity.Id; return(ValidationResult.Success); }
/// <summary> /// Updates a rule. /// </summary> /// <param name="rule">The rule to update.</param> /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous operation. /// </returns> public virtual async Task <ValidationResult> UpdateAsync(RuleItem rule, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (rule == null) { throw new ArgumentNullException("rule"); } var ruleEntity = await Rules.FindAsync(cancellationToken, rule.Id); if (ruleEntity == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, LoggingResources.RuleNotFound, rule.Id)); } ruleEntity.Options = LoggingRuleOptionsHelpers.Serialize(new LoggingRuleOptions { Conditions = rule.Conditions, Actions = rule.Actions }); Context.Patch(ruleEntity, rule); await SaveChangesAsync(cancellationToken); return(ValidationResult.Success); }