예제 #1
0
        /// <summary>
        /// Returns a clone copy of the stored query steps. We have to return a new instance
        /// given the QueryStep itself stored here is singleton.
        /// </summary>
        /// <param name="context">Context for getting parameter value(s).</param>
        /// <returns></returns>
        internal List <QueryStep> GetQuerySteps(IResolveFieldContext <object> context)
        {
            if (_queryStep.QueryParameters.Count > 0)
            {
                var queryStep = _queryStep.CloneQueryStep();

                queryStep.QueryParameters.ForEach(qsqp => SetQueryParameterContextValue(qsqp, context));

                return(new List <QueryStep> {
                    queryStep
                });
            }

            var clonedList = _querySteps.Select(x => x.CloneQueryStep()).ToList();

            clonedList.ToList().ForEach(queryStep =>
            {
                queryStep.QueryParameters.ForEach(queryParameter =>
                {
                    bool skipSetContextValue = queryStep.StepMapper != null && queryParameter.Rule != null && !queryParameter.Rule.ForceCreateContextValueIfNull;

                    if (!skipSetContextValue && queryParameter.ContextValue == null)
                    {
                        SetQueryParameterContextValue(queryParameter, context);
                    }
                });

                if (queryStep.InMemoryFilterQueryParameters != null)
                {
                    queryStep.InMemoryFilterQueryParameters.ForEach(queryParameter =>
                                                                    SetQueryParameterContextValue(queryParameter, context));
                }
            });

            if (clonedList.Count == 0)
            {
                var qs = NewQueryStep();

                var ctxValue = new ContextValue();
                ctxValue.PopulateSelectValues(context);
                Type t  = typeof(TSource);
                var  ta = TypeAccessor.Create(t);

                qs.QueryParameters = new List <QueryParameter>
                {
                    new QueryParameter
                    {
                        MemberModel  = new ModelMember(t, ta, null, true),
                        ContextValue = ctxValue
                    }
                };

                clonedList.Add(qs);
            }

            return(clonedList);
        }
예제 #2
0
        public static ContextValue GetContextValue(this ResolveFieldContext <object> context, ModelMember modelMember)
        {
            var name = modelMember.Name;

            var contextValue = new ContextValue();

            contextValue.SelectValues = context.SubFields.Select(x =>
                                                                 CreateSelectValue(x.Value)).ToList();

            var args = context.Arguments;

            if (args.ContainsKey(name))
            {
                Dictionary <string, object> arg = (Dictionary <string, object>)args[name];

                if (modelMember.IsGuid)
                {
                    contextValue.Values = new List <object> {
                        Guid.Parse(arg.First().Value.ToString())
                    };
                }
                else
                {
                    contextValue.Values = new List <object> {
                        arg.First().Value
                    };
                }

                if (contextValue.Values == null)
                {
                    throw new ArgumentNullException($"{name}.Value");
                }

                string comparison = arg.First().Key;
                if (comparison == "equal")
                {
                    contextValue.Comparison = Comparisons.Equal;
                    return(contextValue);
                }

                if (comparison == "contains" && contextValue.GetFirstValue() is string)
                {
                    contextValue.Comparison = Comparisons.StringContains;
                    return(contextValue);
                }

                if (comparison == "startsWith" && contextValue.GetFirstValue() is string)
                {
                    contextValue.Comparison = Comparisons.StringStartsWith;
                    return(contextValue);
                }

                if (comparison == "endsWith" && contextValue.GetFirstValue() is string)
                {
                    contextValue.Comparison = Comparisons.StringEndsWith;
                    return(contextValue);
                }

                if (comparison == "notEqual" && (
                        contextValue.GetFirstValue() is int ||
                        contextValue.GetFirstValue() is DateTime))
                {
                    contextValue.Comparison = Comparisons.NotEqual;
                    return(contextValue);
                }

                if (comparison == "greaterThan" && (
                        contextValue.GetFirstValue() is int ||
                        contextValue.GetFirstValue() is DateTime))
                {
                    contextValue.Comparison = Comparisons.GreaterThan;
                    return(contextValue);
                }

                if (comparison == "greaterEqualThan" && (
                        contextValue.GetFirstValue() is int ||
                        contextValue.GetFirstValue() is DateTime))
                {
                    contextValue.Comparison = Comparisons.GreaterEqualThan;
                    return(contextValue);
                }

                if (comparison == "lessThan" && (
                        contextValue.GetFirstValue() is int ||
                        contextValue.GetFirstValue() is DateTime))
                {
                    contextValue.Comparison = Comparisons.LessThan;
                    return(contextValue);
                }

                if (comparison == "lessEqualThan" && (
                        contextValue.GetFirstValue() is int ||
                        contextValue.GetFirstValue() is DateTime))
                {
                    contextValue.Comparison = Comparisons.LessEqualThan;
                    return(contextValue);
                }
                throw new NotImplementedException($"Comparison: {comparison} is not implemented for type {contextValue.GetFirstValue().GetType().Name}.");
            }

            return(contextValue);
        }
 public static void PopulateSelectValues(this ContextValue contextValue, ResolveFieldContext <object> context)
 {
     contextValue.SelectValues = context.SubFields.Select(x => CreateSelectValue(x.Value)).ToList();
 }