Exemplo n.º 1
0
        protected void gvBrands_Sorting(object sender, GridViewSortEventArgs e)
        {
            BrandSortingType orderBy = BrandSortingType.IdAsc;

            switch (e.SortExpression)
            {
            case "Id":
            default:
                orderBy = BrandSortingType.IdDesc;
                if (e.SortDirection == SortDirection.Ascending)
                {
                    orderBy = BrandSortingType.IdAsc;
                }
                break;

            case "Name":
                orderBy = BrandSortingType.NameDesc;
                if (e.SortDirection == SortDirection.Ascending)
                {
                    orderBy = BrandSortingType.NameAsc;
                }
                break;
            }

            SetState("Brand-OrderBy", (int)orderBy);
            LoadBrands();
            SetState(DISPLAY, VIEW_BRAND);
        }
Exemplo n.º 2
0
        public PagedList <BrandOverviewModel> GetPagedBrandOverviewModel(
            int pageIndex                = 0,
            int pageSize                 = 2147483647,
            IList <int> brandIds         = null,
            string name                  = null,
            bool?isCategoryFeaturedBrand = null,
            int?categoryId               = null,
            bool?hideDisabled            = null,
            BrandSortingType orderBy     = BrandSortingType.IdAsc)
        {
            var query = _brandRepository.Table;

            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(b => b.Name.Contains(name));
            }

            if (brandIds != null && brandIds.Count > 0)
            {
                query = query.Where(b => brandIds.Contains(b.Id));
            }

            if (hideDisabled.HasValue)
            {
                query = query.Where(b => b.Enabled == true);
            }

            if (isCategoryFeaturedBrand.HasValue && categoryId.HasValue && categoryId.Value != 0)
            {
                var temp = query.GroupJoin(_categoryFeaturedBrandRepository.Table, b => b.Id, cfb => cfb.BrandId, (b, cfb) => new { b, cfb })
                           .SelectMany(b_cfb => b_cfb.cfb.DefaultIfEmpty(), (b_cfb, cfb) => new { b_cfb.b, cfb });

                if (isCategoryFeaturedBrand.Value == true)
                {
                    temp = temp.Where(b_cfb => b_cfb.cfb.CategoryId == categoryId.Value);
                }
                else
                {
                    temp = temp.Where(b_cfb => b_cfb.cfb.CategoryId != categoryId.Value);
                }

                query = temp.Select(b_cfb => b_cfb.b);
            }

            int totalRecords = query.Count();

            switch (orderBy)
            {
            case BrandSortingType.IdAsc:
                query = query.OrderBy(b => b.Id);
                break;

            case BrandSortingType.IdDesc:
                query = query.OrderByDescending(b => b.Id);
                break;

            case BrandSortingType.NameAsc:
                query = query.OrderBy(b => b.Name);
                break;

            case BrandSortingType.NameDesc:
            default:
                query = query.OrderByDescending(b => b.Name);
                break;
            }

            query = query.Skip(pageIndex * pageSize).Take(pageSize);

            var brands = query.ToList()
                         .Select(b => new BrandOverviewModel
            {
                Id      = b.Id,
                Name    = b.Name,
                UrlKey  = b.UrlRewrite,
                Enabled = b.Enabled,
                AssignedCategoryIdForFeaturedBrand = _categoryFeaturedBrandRepository.Table.Where(c => c.BrandId == b.Id).Select(c => c.CategoryId).ToList()
            }
                                 );

            return(new PagedList <BrandOverviewModel>(brands, pageIndex, pageSize, totalRecords));
        }