public async Task <IActionResult> Get([FromQuery] LoverCloudUserParameters parameters)
        {
            string allowedFields = "id, username, profileImageUrl, sex, spouse";
            string fields        = string.IsNullOrEmpty(parameters.Fields) ? allowedFields : parameters.Fields;

            // 鉴权
            AuthorizationResult authorizationResult = await _authorizationService.AuthorizeAsync(
                User, fields, new LoverCloudUserFieldsRequirement(allowedFields));

            if (!authorizationResult.Succeeded)
            {
                return(Forbid());
            }

            PaginatedList <LoverCloudUser> users = await _repository.GetAsync(
                parameters, i => i.Include(x => x.Lover).ThenInclude(x => x.LoverCloudUsers));

            IEnumerable <LoverCloudUserResource> userResources = _mapper.Map <IEnumerable <LoverCloudUserResource> >(users);

            userResources.GetProfileImageUrl(Url);
            IEnumerable <ExpandoObject> shapedUserResources = userResources.ToDynamicObject(
                fields).Select(d =>
            {
                var dict = d as IDictionary <string, object>;
                if (dict.ContainsKey("Spouse"))
                {
                    if (dict["Spouse"] is LoverCloudUserResource r)
                    {
                        r.Spouse = null;
                        r.GetProfileImageUrl(Url);
                        dict["Spouse"] = r.ToDynamicObject(fields.Replace("spouse", string.Empty));
                    }
                }
                return(d);
            });

            this.AddPaginationHeaderToResponse(users);

            var result = new
            {
                links = this.CreatePaginationLinks("GetUsers", parameters, users.HasPrevious, users.HasNext),
                value = shapedUserResources
            };

            return(Ok(result));
        }
Beispiel #2
0
        public async Task <PaginatedList <LoverCloudUser> > GetAsync(
            LoverCloudUserParameters parameters, Func <IQueryable <LoverCloudUser>, IQueryable <LoverCloudUser> > configIncludeable = null)
        {
            IQueryable <LoverCloudUser> users = configIncludeable?.Invoke(
                _dbContext.Users) ?? _dbContext.Users;

            users = users.Where(
                u => string.IsNullOrEmpty(parameters.UserName)
                ? true
                : u.UserName.Contains(parameters.UserName));

            IQueryable <LoverCloudUser> paginatedUsers = users.Skip(parameters.PageSize * (parameters.PageIndex - 1))
                                                         .Take(parameters.PageSize);

            return(new PaginatedList <LoverCloudUser>(
                       parameters.PageIndex,
                       parameters.PageSize,
                       await users.CountAsync(),
                       await paginatedUsers.ToListAsync()));
        }