Beispiel #1
0
        public bool TryGenerate(IPContext context, string input, out ISortParticle <TEntity> particle)
        {
            input = input.Trim();

            bool   isDescending = false;
            string name;

            if (input.StartsWith("-"))
            {
                isDescending = true;
                name         = input.Substring(1).TrimStart();
            }
            else
            {
                name = input;
            }

            if (!TryGetPropertyByName(name, out var property))
            {
                particle = null;
                return(false);
            }

            particle = new ByPropertyParticle <TEntity>(
                property: property,
                descending: isDescending
                );

            return(true);
        }
Beispiel #2
0
        public static bool TryParseCsv <T>(
            this IPContext pContext,
            string input,
            out IEnumerable <ISortParticle <T> > particles
            )
            where T : class
        {
            var particlesRaw = new List <ISortParticle <T> >();
            var generators   = pContext.GetGenerators <T, ISortParticle <T> >();

            var splitInput = input
                             .Split(',');

            bool wasAbleToParse = true;

            foreach (var sort in splitInput)
            {
                ISortParticle <T> particle = null;

                wasAbleToParse &= generators.Any(x => x.TryGenerate(pContext, sort, out particle));

                if (!(particle is null))
                {
                    particlesRaw.Add(particle);
                }
            }

            particles = wasAbleToParse ?
                        particlesRaw :
                        Array.Empty <ISortParticle <T> >();

            return(wasAbleToParse);
        }
        public bool TryGenerate(IPContext context, string input, out ISortParticle <Post> particle)
        {
            var descending = input.StartsWith('-');
            var remaining  = descending ? input.Substring(1) : input;

            if (!remaining.Trim().Equals("Popularity", System.StringComparison.OrdinalIgnoreCase))
            {
                particle = null;
                return(false);
            }

            particle = new SortPostByPopularityParticle(descending);
            return(true);
        }