public IActionResult GetTreeview([FromQuery] NewsCategoryRequestModel requestModel) { #region predicate Expression <Func <NewsCategory, bool> > where = x => true; if (!string.IsNullOrWhiteSpace(requestModel.Name)) { where = a => a.Name.Contains(requestModel.Name); } if (requestModel.IsGetParent.HasValue && requestModel.IsGetParent.Value) { where = ExpressionHelpers.CombineAnd <NewsCategory>(where, x => x.ParentId == null || x.ParentId == 0); } bool ascending = false; if (requestModel.Ascending.HasValue) { ascending = requestModel.Ascending.Value; } #endregion var categories = _newsCategoryService.GetPagedList( where, null, ascending, requestModel.Page - 1, requestModel.Count); var newsCategoryModels = categories.ToList().ToParentModels(); return(RespondSuccess(newsCategoryModels, newsCategoryModels.Count)); }
public async Task <IActionResult> Get([FromQuery] NewsCategoryRequestModel requestModel) { #region predicate if (requestModel == null) { return(BadRequest()); } if (requestModel.Page < 1) { requestModel.Page = 1; } if (requestModel.Count < 1) { requestModel.Count = 10; } Expression <Func <NewsCategory, bool> > where = x => true; if (!string.IsNullOrWhiteSpace(requestModel.Name)) { where = a => a.Name.Contains(requestModel.Name); } if (requestModel.IsGetParent.HasValue) { where = ExpressionHelpers.CombineAnd <NewsCategory>(where, x => x.ParentId == null || x.ParentId == 0); } bool ascending = true; if (requestModel.Ascending.HasValue) { ascending = requestModel.Ascending.Value; } #endregion IPagedList <NewsCategory> categories; switch (requestModel.OrderBy) { case "Name": categories = await _newsCategoryService.GetPagedListAsync( where, x => x.Name, ascending, requestModel.Page - 1, requestModel.Count); break; default: categories = await _newsCategoryService.GetPagedListAsync( where, x => x.DisplayOrder, ascending, requestModel.Page - 1, requestModel.Count); break; } var model = categories.Select(x => x.ToModel()).ToList(); return(RespondSuccess(model, categories.TotalCount)); }