LCOV - code coverage report
Current view: top level - Cqrs.Mongo/Factories - IndexKeysBuilderExtension.cs Hit Total Coverage
Test: doc-coverage.info Lines: 0 4 0.0 %
Date: 2017-07-26

          Line data    Source code
       1             : using System;
       2             : using System.Collections.Generic;
       3             : using System.Linq;
       4             : using System.Linq.Expressions;
       5             : using MongoDB.Driver.Builders;
       6             : 
       7             : namespace Cqrs.Mongo.Factories
       8             : {
       9             :         public static class IndexKeysBuilderExtension
      10           0 :         {
      11           0 :                 public static IndexKeysBuilder Ascending<T>(this IndexKeysBuilder mongoIndexKeys, params Expression<Func<T, object>>[] selectors)
      12             :                 {
      13             :                         var properties = new List<string>();
      14             :                         foreach (Expression<Func<T, object>> selector in selectors)
      15             :                         {
      16             :                                 var selectorUnaryExpression = selector.Body as UnaryExpression;
      17             :                                 MemberExpression selectorMemberExpression;
      18             :                                 if (selectorUnaryExpression != null)
      19             :                                 {
      20             :                                         selectorMemberExpression = (MemberExpression)selectorUnaryExpression.Operand;
      21             :                                 }
      22             :                                 else
      23             :                                 {
      24             :                                         selectorMemberExpression = (MemberExpression) selector.Body;
      25             :                                 }
      26             :                                 string memberName = CheckForChildProperty(selectorMemberExpression);
      27             :                                 properties.Add(memberName);
      28             :                         }
      29             :                         return mongoIndexKeys.Ascending(properties.ToArray());
      30             :                 }
      31             : 
      32           0 :                 public static IndexKeysBuilder Descending<T>(this IndexKeysBuilder mongoIndexKeys, params Expression<Func<T, object>>[] selectors)
      33             :                 {
      34             :                         var properties = new List<string>();
      35             :                         foreach (Expression<Func<T, object>> selector in selectors)
      36             :                         {
      37             :                                 var selectorUnaryExpression = selector.Body as UnaryExpression;
      38             :                                 MemberExpression selectorMemberExpression;
      39             :                                 if (selectorUnaryExpression != null)
      40             :                                 {
      41             :                                         selectorMemberExpression = (MemberExpression)selectorUnaryExpression.Operand;
      42             :                                 }
      43             :                                 else
      44             :                                 {
      45             :                                         selectorMemberExpression = (MemberExpression) selector.Body;
      46             :                                 }
      47             :                                 string memberName = CheckForChildProperty(selectorMemberExpression);
      48             :                                 properties.Add(memberName);
      49             :                         }
      50             :                         return mongoIndexKeys.Descending(properties.ToArray());
      51             :                 }
      52             : 
      53           0 :                 public static string CheckForChildProperty(MemberExpression selectorMemberExpression)
      54             :                 {
      55             :                         string memberName = selectorMemberExpression.Member.Name;
      56             :                         var selectorMethodCallExpression = selectorMemberExpression.Expression as MethodCallExpression;
      57             :                         if (selectorMethodCallExpression != null)
      58             :                         {
      59             :                                 var selectorMethodCallExpressionArgument = selectorMethodCallExpression.Arguments.FirstOrDefault() as MemberExpression;
      60             :                                 if (selectorMethodCallExpressionArgument != null)
      61             :                                         memberName = string.Format("{1}.{0}", memberName, selectorMethodCallExpressionArgument.Member.Name);
      62             :                         }
      63             :                         else
      64             :                         {
      65             :                                 if (selectorMemberExpression.Expression.GetType().Name == "PropertyExpression")
      66             :                                 {
      67             :                                         dynamic propertyExpression = selectorMemberExpression.Expression;
      68             :                                         if (propertyExpression.Expression.NodeType == ExpressionType.MemberAccess)
      69             :                                                 memberName = string.Format("{2}.{1}.{0}", memberName, propertyExpression.Member.Name, propertyExpression.Expression.Member.Name);
      70             :                                         else
      71             :                                                 memberName = string.Format("{1}.{0}", memberName, propertyExpression.Member.Name);
      72             :                                 }
      73             :                         }
      74             :                         return memberName;
      75             :                 }
      76             :         }
      77             : }

Generated by: LCOV version 1.10