public async Task <PagedList <PostViewModel> > ListPostAsync(PostRequestListViewModel postRequestListViewModel)
        {
            var list = await GetAll().Where(
                x => (string.IsNullOrEmpty(postRequestListViewModel.Query)) ||
                (x.Title.Contains(postRequestListViewModel.Query)
                )).Select(x => new PostViewModel(x)).ToListAsync();

            var    postViewModelProperties = GetAllPropertyNameOfViewModel();
            var    requestPropertyName     = !string.IsNullOrEmpty(postRequestListViewModel.SortName) ? postRequestListViewModel.SortName.ToLower() : string.Empty;
            string matchedPropertyName     = string.Empty;

            foreach (var postViewModelProperty in postViewModelProperties)
            {
                var lowerTypeViewModelProperty = postViewModelProperty.ToLower();
                if (lowerTypeViewModelProperty.Equals(requestPropertyName))
                {
                    matchedPropertyName = postViewModelProperty;
                    break;
                }
            }

            if (string.IsNullOrEmpty(matchedPropertyName))
            {
                matchedPropertyName = "Title";
            }

            var type         = typeof(PostViewModel);
            var sortProperty = type.GetProperty(matchedPropertyName);

            list = postRequestListViewModel.IsDesc ? list.OrderByDescending(x => sortProperty.GetValue(x, null)).ToList() : list.OrderBy(x => sortProperty.GetValue(x, null)).ToList();

            return(new PagedList <PostViewModel>(list, postRequestListViewModel.Offset ?? CommonConstants.Config.DEFAULT_SKIP, postRequestListViewModel.Limit ?? CommonConstants.Config.DEFAULT_TAKE));
        }
Beispiel #2
0
        public async Task <IActionResult> Get(PostRequestListViewModel postRequestListViewModel)
        {
            var posts = await _postService.ListPostAsync(postRequestListViewModel);

            return(Ok(posts));
        }