Exemplo n.º 1
0
        private List <Category> GetRecursiveCats(ref CatWithSubs cur)
        {
            var result     = new List <Category>();
            var subs       = new List <CatWithSubs>();
            var curSubCats = GetSubCategories(cur.Cat.Id);

            if (curSubCats.Any())
            {
                foreach (var cat in curSubCats)
                {
                    result.Add(cat);
                    var tmp = new CatWithSubs()
                    {
                        Cat = cat
                    };
                    result.AddRange(GetRecursiveCats(ref tmp));
                    var tmpClone = new CatWithSubs()
                    {
                        Cat = tmp.Cat, Subs = tmp.Subs
                    };
                    subs.Add(tmpClone);
                }
            }

            cur.Subs = subs;
            return(result);
        }
Exemplo n.º 2
0
        public IEnumerable <CatWithSubs> GetAllCategories()
        {
            var mainCatWithSubs = new CatWithSubs()
            {
                Cat = Categories.FirstOrDefault(x => x.BaseCategory == null)
            };

            GetRecursiveCats(ref mainCatWithSubs);
            _logger.LogInformation($"Got all categories. Root is {mainCatWithSubs.Cat.Name}");
            return(mainCatWithSubs.Subs);
        }
Exemplo n.º 3
0
        private IQueryable <Product> FilterByCategory(IQueryable <Product> products, Guid categoryId)
        {
            var curCat = new CatWithSubs()
            {
                Cat = cats.FirstOrDefault(x => x.Id == categoryId)
            };
            var apropriateCats = GetRecursiveCats(ref curCat);

            apropriateCats.Add(curCat.Cat);
            return(products.Where(p => apropriateCats.Contains(p.Category)));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets categories in recursive way.
        /// </summary>
        /// <param name="cur">The current category with subs. It will be filled with Subcategories</param>
        private void GetRecursiveCats(ref CatWithSubs cur)
        {
            var subs       = new List <CatWithSubs>();
            var curSubCats = GetSubCategories(cur.Cat.Id);

            if (curSubCats.Any())
            {
                foreach (var cat in curSubCats)
                {
                    var tmp = new CatWithSubs()
                    {
                        Cat = cat
                    };
                    GetRecursiveCats(ref tmp);
                    var tmpClone = new CatWithSubs()
                    {
                        Cat = tmp.Cat, Subs = tmp.Subs
                    };
                    subs.Add(tmpClone);
                }
            }

            cur.Subs = subs;
        }