Example #1
0
        /// <inheritdoc />
        public async Task <PagedList <User> > GetUsersWithRoles(UserParameters parameters)
        {
            var users = this.GetRolesQueryable();

            return(await PagedList <User> .CreateAsync(users, parameters.PageNumber, parameters.PageSize));
        }
Example #2
0
        /// <inheritdoc />
        public async Task <PagedList <User> > GetAll(UserParameters parameters = null)
        {
            var users = this.GetQueryable();

            if (parameters != null)
            {
                if (parameters.Gender.HasValue == false)
                {
                    var user = await this.UserManager.FindByIdAsync(parameters.UserID.ToString());

                    switch (user?.Gender)
                    {
                    case Gender.Female:
                        parameters.Gender = Gender.Male;
                        break;

                    case Gender.Male:
                        parameters.Gender = Gender.Female;
                        break;

                    case null:
                    case Gender.Undefined:
                        parameters.Gender = Gender.Undefined;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(user.Gender));
                    }
                }

                if (parameters.Gender != Gender.Undefined)
                {
                    users = users.Where(u => u.Gender == parameters.Gender);
                }

                if (parameters.MinimumAge.HasValue)
                {
                    var maximumBirthDate = DateTime.Today.AddYears(-parameters.MinimumAge.Value);

                    users = users.Where(u => u.BirthDate <= maximumBirthDate);
                }

                if (parameters.MaximumAge.HasValue)
                {
                    var minimumBirthDate = DateTime.Today.AddYears(-parameters.MaximumAge.Value - 1);

                    users = users.Where(u => u.BirthDate >= minimumBirthDate);
                }

                if (string.IsNullOrWhiteSpace(parameters.OrderBy) == false)
                {
                    if (parameters.OrderBy == nameof(User.CreatedAt).ToLowerCamelCase())
                    {
                        users = users.OrderByDescending(u => u.CreatedAt);
                    }
                    else if (parameters.OrderBy == nameof(User.LastActiveAt).ToLowerCamelCase())
                    {
                        users = users.OrderByDescending(u => u.LastActiveAt);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(nameof(parameters.OrderBy), parameters.OrderBy, null);
                    }
                }

                users = users.Where(u => u.Id != parameters.UserID);
            }
            else
            {
                parameters = new UserParameters();
            }

            return(await PagedList <User> .CreateAsync(users, parameters.PageNumber, parameters.PageSize));
        }