Пример #1
0
        public async Task <IActionResult> Get([FromQuery] LoverAnniversaryParameters parameters)
        {
            PaginatedList <LoverAnniversary> anniversaries =
                await _anniversaryRepository.GetAsync(this.GetUserId(), parameters);

            IQueryable <LoverAnniversary> sortedAnniversaries = anniversaries.AsQueryable()
                                                                .ApplySort(
                parameters.OrderBy,
                _propertyMappingContainer.Resolve <LoverAnniversaryResource, LoverAnniversary>());

            IEnumerable <LoverAnniversaryResource> loverAnniversaryResources =
                _mapper.Map <IEnumerable <LoverAnniversaryResource> >(sortedAnniversaries);

            IEnumerable <ExpandoObject> shapedAnniversaryResource =
                loverAnniversaryResources.ToDynamicObject(parameters.Fields)
                .Select(x =>
            {
                var dict = x as IDictionary <string, object>;
                dict.Add("links", CreateLinksForAnniversary(dict["Id"] as string, parameters.Fields));
                return(x);
            });

            this.AddPaginationHeaderToResponse(anniversaries);

            var result = new
            {
                value = shapedAnniversaryResource,
                links = this.CreatePaginationLinks("GetLoverAnniversaries", parameters, anniversaries.HasPrevious, anniversaries.HasNext)
            };

            return(Ok(result));
        }
Пример #2
0
        private object CreateLinksForAnniversaries(LoverAnniversaryParameters parameters, bool hasPrevious, bool hasNext)
        {
            var links = new List <LinkResource>
            {
                new LinkResource("current_page", "get", Url.LinkRelative("GetAnniversaries", parameters))
            };

            if (hasPrevious)
            {
                parameters.PageIndex--;
                links.Add(
                    new LinkResource(
                        "previous_page", "get", Url.LinkRelative("GetAnniversaries", parameters)));
                parameters.PageIndex++;
            }
            if (hasNext)
            {
                parameters.PageIndex++;
                links.Add(
                    new LinkResource(
                        "next_page", "get", Url.LinkRelative("GetAnniversaries", parameters)));
                parameters.PageIndex--;
            }

            return(links);
        }
Пример #3
0
        public async Task <PaginatedList <LoverAnniversary> > GetAsync(string userId, LoverAnniversaryParameters parameters)
        {
            IQueryable <LoverAnniversary> paginatedQuery = _dbContext.LoverAnniversaries
                                                           .Where(x => x.Lover.LoverCloudUsers.Any(user => user.Id == userId))
                                                           .Skip(parameters.PageSize * (parameters.PageIndex - 1))
                                                           .Take(parameters.PageSize);

            return(new PaginatedList <LoverAnniversary>(
                       parameters.PageIndex, parameters.PageSize,
                       await paginatedQuery.CountAsync(), await paginatedQuery.ToListAsync()));
        }