Ejemplo n.º 1
0
        public async Task <ActionResult> GetCustomersAsync([FromQuery] Pagination pagination, [FromQuery] SearchUser search)
        {
            var customers = await _userManager.GetUsersInRoleAsync(Roles.Customer);

            var queryableCustomers = customers.AsQueryable();

            // Search by Email:
            if (search.Email != "")
            {
                queryableCustomers = queryableCustomers.Where(a =>
                                                              a.Email.ToLower().Contains(search.Email.ToLower()));
            }

            // Sort Asc:
            if (search.sortAsc.ToLower() == "email")
            {
                queryableCustomers = queryableCustomers.OrderBy(a => a.Email);
            }

            // Sort Desc:
            // sort by any input field:
            // a => a.GetType().GetProperty(search.sortDesc).GetValue(a) !!not work
            if (search.sortDesc.ToLower() != "email")
            {
                queryableCustomers = queryableCustomers.OrderByDescending(a => a.Email);
            }

            var result = queryableCustomers.ToList();

            var paginatedList = PaginatedList <IdentityUser> .Create(
                result,
                pagination.current,
                pagination.pageSize
                );

            // Mapping data
            IList <CustomerResponseDTO> response = new List <CustomerResponseDTO>();

            for (int i = 0; i < paginatedList.Data.Count(); i++)
            {
                IdentityUser item   = (IdentityUser)paginatedList.Data.ElementAt(i);
                var          claims = await _userManager.GetClaimsAsync(item);

                var mapped = _mapper.Map <IdentityUser, CustomerResponseDTO>(
                    source: item,
                    opts: opt => opt.Items["claims"] = claims
                    );
                response.Add(mapped);
            }

            return(Ok(PaginatedList <object> .ApplyWithSource(response, paginatedList)));
        }