public async Task <PagedList <RoleViewModel> > ListRoleAsync(RoleRequestListViewModel roleRequestListViewModel)
        {
            var list = await GetAll().Where(x => (string.IsNullOrEmpty(roleRequestListViewModel.Query) ||
                                                  (x.Name.Contains(roleRequestListViewModel.Query)))
                                            ).Select(x => new RoleViewModel(x)).ToListAsync();

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

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

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

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

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

            return(new PagedList <RoleViewModel>(list, roleRequestListViewModel.Offset ?? CommonConstants.Config.DEFAULT_SKIP, roleRequestListViewModel.Limit ?? CommonConstants.Config.DEFAULT_TAKE));
        }
Пример #2
0
        public async Task <IActionResult> Get(RoleRequestListViewModel roleRequestListViewModel)
        {
            var roles = await _roleService.ListRoleAsync(roleRequestListViewModel);

            return(Ok(roles));
        }