Пример #1
0
        public static void RenderFilter(RenderContext ctx)
        {
            var filter = ctx.Query.Get("Filter");

            if (filter == null)
            {
                return;
            }

            var widgets =
                from property in filter.GetType().GetProperties().Select(x => x.Name)
                let widget = filter.Get(property) as Widget
                             where widget != null
                             select widget;

            if (!widgets.Any())
            {
                return;
            }

            if (ctx.Entity.Actions == null)
            {
                ctx.Entity.Actions = new EntityActionCollection();
            }

            var filterAction = ctx.Entity.Actions.FirstOrDefault(x => x.Name.EqualsIgnoreCase("Filter"));

            if (filterAction == null)
            {
                var filterArgs = Enumerable.Empty <KeyValuePair <string, object> >();
                var href       = QueryPathBuilder.BuildPath(ctx, ctx.Query, ctx.PathTemplate, null, filterArgs);

                // removendo informacao de paginacao
                href = new Route(href).UnsetArgs("limit", "offset");

                filterAction        = new EntityAction();
                filterAction.Name   = "Filters";
                filterAction.Href   = href;
                filterAction.Method = "GET";
                filterAction.Title  = "Filtros";
                ctx.Entity.Actions.Add(filterAction);
            }

            if (filterAction.Fields == null)
            {
                filterAction.Fields = new FieldCollection();
            }

            foreach (var widget in widgets)
            {
                var field = widget.ToMediaField();

                if (field.Title == null)
                {
                    field.Title = field?.Name.ChangeCase(TextCase.ProperCase);
                }

                filterAction.Fields.Add(field);
            }
        }
Пример #2
0
        private static Link CreateLink(QueryLink source, RenderContext ctx)
        {
            var linkedQuery = ctx.Injector.CreateInstance(source.QueryType);

            source.Setter?.Invoke(linkedQuery);

            var targets =
                from renderer
                in ctx.PaperRendererRegistry.FindPaperRenderers(source.QueryType)
                where renderer.PaperRendererType == typeof(QueryRenderer)
                let argNames = UriUtil.GetArgNames(renderer.PathTemplate)
                               orderby argNames.Length descending
                               select new { renderer, argNames };

            foreach (var target in targets)
            {
                var pathTemplate = target.renderer.PathTemplate;
                var argNames     = target.argNames;

                var pathArgs = QueryPathBuilder.SatisfyArgs(linkedQuery, argNames);

                var isAllArgsSatisfied = !pathArgs.Any(x => x.Value == null);
                if (isAllArgsSatisfied)
                {
                    var href = QueryPathBuilder.BuildPath(ctx, linkedQuery, pathTemplate, pathArgs, null);
                    var link = new Link().CopyFrom(source);
                    link.Href = href;
                    return(link);
                }
            }

            return(null);
        }