示例#1
0
        public async Task <IActionResult> Get(string searchTerm, int?level, bool?leafOnly, int pageIndex, int itemsPerPage, string orderBy)
        {
            var serviceModel = new GetCategoriesServiceModel
            {
                Level        = level,
                Language     = CultureInfo.CurrentCulture.Name,
                SearchTerm   = searchTerm,
                LeafOnly     = leafOnly,
                PageIndex    = pageIndex,
                ItemsPerPage = itemsPerPage,
                OrderBy      = orderBy
            };

            var validator = new GetCategoriesModelValidator();

            var validationResult = await validator.ValidateAsync(serviceModel);

            if (validationResult.IsValid)
            {
                var categories = await this.categoryService.GetAsync(serviceModel);

                if (categories != null)
                {
                    var response = new PagedResults <IEnumerable <CategoryResponseModel> >(categories.Total, categories.PageSize)
                    {
                        Data = categories.Data.OrEmptyIfNull().Select(x => new CategoryResponseModel
                        {
                            Id                 = x.Id,
                            IsLeaf             = x.IsLeaf,
                            Level              = x.Level,
                            Name               = x.Name,
                            Order              = x.Order,
                            ParentId           = x.ParentId,
                            ThumbnailMediaId   = x.ThumbnailMediaId,
                            ParentCategoryName = x.ParentCategoryName,
                            LastModifiedDate   = x.LastModifiedDate,
                            CreatedDate        = x.CreatedDate
                        })
                    };

                    return(this.StatusCode((int)HttpStatusCode.OK, response));
                }
            }

            throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
        }
示例#2
0
        public async Task <PagedResults <IEnumerable <CategoryServiceModel> > > GetAsync(GetCategoriesServiceModel model)
        {
            var categories = from c in this.context.Categories
                             join t in this.context.CategoryTranslations on c.Id equals t.CategoryId into ct
                             from x in ct.DefaultIfEmpty()
                             join m in this.context.CategoryImages on c.Id equals m.CategoryId into cm
                             from y in cm.DefaultIfEmpty()
                             join pct in this.context.CategoryTranslations on c.Parentid equals pct.CategoryId into pctg
                             from w in pctg.DefaultIfEmpty()
                             where x.Language == model.Language && (w.Language == model.Language || w.Language == null) && c.IsActive
                             select new CategoryServiceModel
            {
                Id                 = c.Id,
                Order              = c.Order,
                Level              = c.Level,
                IsLeaf             = c.IsLeaf,
                ParentId           = c.Parentid,
                ParentCategoryName = w.Name,
                Name               = x.Name,
                ThumbnailMediaId   = y.MediaId,
                LastModifiedDate   = c.LastModifiedDate,
                CreatedDate        = c.CreatedDate
            };

            if (!string.IsNullOrWhiteSpace(model.SearchTerm))
            {
                categories = categories.Where(x => x.Name.StartsWith(model.SearchTerm));
            }

            if (model.Level.HasValue)
            {
                categories = categories.Where(x => x.Level == model.Level.Value);
            }

            if (model.LeafOnly.HasValue)
            {
                categories = categories.Where(x => x.IsLeaf == model.LeafOnly.Value);
            }

            categories = categories.ApplySort(model.OrderBy);

            return(categories.PagedIndex(new Pagination(categories.Count(), model.ItemsPerPage), model.PageIndex));
        }