コード例 #1
0
        public async Task <IActionResult> Get([FromQuery] LoverAlbumParameters parameters)
        {
            PaginatedList <LoverAlbum> albums = await _albumRepository.GetLoverAlbumsAsync(this.GetUserId(), parameters);

            IQueryable <LoverAlbum> sortedAlbums = albums.AsQueryable()
                                                   .ApplySort(
                parameters.OrderBy,
                _propertyMappingContainer.Resolve <LoverAlbumResource, LoverAlbum>());

            IEnumerable <LoverAlbumResource> albumResources =
                _mapper.Map <IEnumerable <LoverAlbumResource> >(sortedAlbums);

            foreach (LoverAlbumResource source in albumResources)
            {
                source.PhotosCount = await _albumRepository.GetPhotosCount(source.Id);

                LoverPhoto converImage = await _albumRepository.GetCoverImage(source.Id);

                if (converImage != null)
                {
                    source.CoverImageUrl = Url.LinkRelative("GetPhoto", new { id = converImage.Id });
                }
            }

            IEnumerable <ExpandoObject> shapedAlbumResources = albumResources
                                                               .ToDynamicObject(parameters.Fields)
                                                               .AddLinks(
                this, parameters.Fields, "album",
                "GetLoverAlbum", "DeleteLoverAlbum", "PartiallyUpdateLoverAlbum");

            var result = new
            {
                value = shapedAlbumResources,
                links = this.CreatePaginationLinks("GetLoverAlbums", parameters, albums.HasPrevious, albums.HasNext)
            };

            this.AddPaginationHeaderToResponse(albums);

            return(Ok(result));
        }
コード例 #2
0
        public async Task <PaginatedList <LoverAlbum> > GetLoverAlbumsAsync(string userId, LoverAlbumParameters parameters)
        {
            IQueryable <LoverAlbum> query = _dbContext.LoverAlbums
                                            .Include(x => x.Tags)
                                            .Include(x => x.Lover).ThenInclude(x => x.LoverCloudUsers)
                                            .Where(x => x.Lover.LoverCloudUsers.Any(user => user.Id == userId));

            IQueryable <LoverAlbum> paginatedQuery = query.Skip((parameters.PageIndex - 1) * parameters.PageSize)
                                                     .Take(parameters.PageSize);

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