/*==========================================================================================================================
    | MAP QUERY RESULT
    \-------------------------------------------------------------------------------------------------------------------------*/
    /// <summary>
    ///   Private helper function that maps a successfully validated <see cref="Topic"/> to a <see
    ///   cref="QueryResultTopicViewModel"/>.
    /// </summary>
    private void MapQueryResult(
      Collection<QueryResultTopicViewModel> topicList,
      Topic topic,
      TopicQueryOptions options,
      ref int remainingResults,
      ReadOnlyTopicCollection related
    )
    {

      /*------------------------------------------------------------------------------------------------------------------------
      | Loop through children
      \-----------------------------------------------------------------------------------------------------------------------*/
      var isValid = IsValidTopic(topic, options, remainingResults);

      /*------------------------------------------------------------------------------------------------------------------------
      | Map topic
      \-----------------------------------------------------------------------------------------------------------------------*/
      if (isValid) {

        //Decrement counter
        remainingResults--;

        //Map topic
        var mappedTopic = new QueryResultTopicViewModel(
          topic.Id,
          topic.Key,
          options.UseKeyAsText ? topic.Key : topic.Title,
          topic.GetUniqueKey(),
          topic.GetWebPath(),
          options.EnableCheckboxes ? (!options.MarkRelated || related.Contains(topic)) : new bool?(),
          !topic.Attributes.GetBoolean("DisableDelete") && !topic.Attributes.GetBoolean("IsProtected"),
          options.ExpandRelated && related.Any(r => r.GetUniqueKey().StartsWith(topic.GetUniqueKey(), StringComparison.Ordinal))
        );

        //Add topic to topic list
        topicList.Add(mappedTopic);

        //Handle recursion, if appropriate
        topicList = options.FlattenStructure ? topicList : mappedTopic.Children;

      }

      /*------------------------------------------------------------------------------------------------------------------------
      | Loop through children (asynchronously)
      \-----------------------------------------------------------------------------------------------------------------------*/
      if (isValid && options.IsRecursive || options.FlattenStructure ) {
        foreach (var childTopic in topic.Children) {
          MapQueryResult(
            topicList,
            childTopic,
            options,
            ref remainingResults,
            related
          );
        }
      }

    }
Пример #2
0
 /*==========================================================================================================================
 | REPLACE TOKENS
 >---------------------------------------------------------------------------------------------------------------------------
 | Replaces tokenized parameters (e.g., {Key}) in the source string based on the source Topic's properties.
 \-------------------------------------------------------------------------------------------------------------------------*/
 private static string ReplaceTokens(QueryResultTopicViewModel topic, string source) {
   if (topic is not null && !String.IsNullOrEmpty(source)) {
     source = source
       .Replace("{TopicId}", topic.Id.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase)
       .Replace("{Key}", topic.Key, StringComparison.OrdinalIgnoreCase)
       .Replace("{UniqueKey}", topic.UniqueKey, StringComparison.OrdinalIgnoreCase)
       .Replace("{WebPath}", topic.WebPath, StringComparison.OrdinalIgnoreCase)
       .Replace("{Title}", topic.Title, StringComparison.OrdinalIgnoreCase);
   }
   return source;
 }