Exemple #1
0
        public static Mutator ToMutator <T>(this IQueryCollection source) where T : Data <T>
        {
            var modifier = new Mutator {
                Transform = new QueryTransform()
            };

            if (Info <T> .Settings?.Pipelines?.Before != null)
            {
                // Transform it only once per request.
                var stringQueryCollection = source.ToDictionary(i => i.Key, i => i.Value.ToList());

                foreach (var pipelineMember in Info <T> .Settings.Pipelines.Before)
                {
                    modifier.AddPipelineMetadata <T>(pipelineMember, stringQueryCollection);
                }
            }

            if (source.ContainsKey("sort"))
            {
                modifier.Transform.OrderBy = source["sort"];
            }

            if (source.ContainsKey("page") || source.ContainsKey("size") || source.ContainsKey("limit"))
            {
                modifier.Transform.Pagination = new Pagination
                {
                    Index = source.ContainsKey("page") ? Convert.ToInt32(source["page"]) : 0,
                    Size  = source.ContainsKey("size") ? Convert.ToInt32(source["size"]) : source.ContainsKey("limit") ? Convert.ToInt32(source["limit"]) : 50
                }
            }
            ;

            if (source.ContainsKey("output"))
            {
                modifier.Transform.OutputFormat = source["output"];
            }

            if (source.ContainsKey("filter"))
            {
                modifier.Transform.Filter = source["filter"];
            }

            if (source.ContainsKey("q"))
            {
                modifier.Transform.OmniQuery = source["q"];
            }

            return(modifier);
        }