コード例 #1
0
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task <GalleryDto> GetGalleryAsync(
            IndexParams param,
            int?categoryId   = null,
            string lang      = Language.KEY_fa_IR,
            bool isComponent = false)
        {
            param.CheckArgumentIsNull(nameof(param));
            var postType = await _postTypeRepository
                           .GetBySlugAsync(PostType.GALLERY);

            if (postType == null)
            {
                throw new PostTypeNotFoundException();
            }

            var query = _repository
                        .Query()
                        .IncludeFiles()
                        .Where(_ => _.PostTypeId == postType.Id)
                        .Where(_ => _.Status == PostStatus.Published)
                        .Where(_ => _.PublishDate <= _dateService.UtcNow())
                        .Where(_ => _.Language.LangKey == lang)
                        .Where(_ => _.IsComponent == isComponent);

            if (categoryId.HasValue)
            {
                query = query.Where(_ => _.CategoryId == categoryId);
            }

            query = query.SetOrder(param.OrderBy, !param.OrderDesc);

            if (param.HasPaging)
            {
                query = query
                        .Skip(param.Skip)
                        .Take(param.Take);
            }

            var queryResult = await query.ToListAsync();

            var result = new GalleryDto {
                Posts = queryResult.Adapt <List <PostFileGalleryDto> >()
            };

            foreach (var post in result.Posts)
            {
                post.Files = queryResult.FirstOrDefault(_ => _.Id == post.Id)
                             .PostFiles.OrderBy(_ => _.OrderNum)
                             .Select(_ => _.File)
                             .Adapt <List <PostFileGalleryItemDto> >();
            }

            return(await Task.FromResult(result));
        }
コード例 #2
0
        public async Task <PostIndexDto> GetIndexAsync(
            IndexParams <PostIndexFilter> param
            )
        {
            param.CheckArgumentIsNull(nameof(param));

            var postType = await _postTypeRepository
                           .GetBySlugAsync(param.Filter.PostTypeSlug);

            if (postType == null)
            {
                throw new PostTypeNotFoundException();
            }

            var lang = await _languageRepository.GetByLangKeyAsync(param.LangKey)
                       ?? await _languageRepository.GetDefaultLanguage();

            var query = _repository.Query();

            query = AddIndexFilter(query, param.Filter, lang);

            var result = new PostIndexDto {
                CategoryId        = param.Filter.CategoryId,
                PostTypeId        = postType.Id,
                PostTypeSlug      = postType.Slug,
                LangKey           = lang.LangKey,
                PageSize          = param.PageSize,
                CurrentPageNumber = param.PageNumber,
                TotalCount        = await query.CountAsync(),
                Categories        = await getCategoryItemsAsync(postType.Id, lang.Id)
            };

            if (result.CategoryId.HasValue)
            {
                result.CategoryTitle = (await _categoryRepository
                                        .FindAsync(result.CategoryId.Value)
                                        ).Title;
            }

            result.Items = await query
                           .Include(_ => _.PostType)
                           .Include(_ => _.Category)
                           .Include(_ => _.Comments)
                           .Include(_ => _.Likes)
                           .Include(_ => _.PostTags)
                           .ThenInclude(_ => _.Tag)
                           .Skip(param.Skip)
                           .Take(param.PageSize)
                           .OrderByDescending(_ => _.Id)
                           .ThenByDescending(_ => _.PublishDate)
                           .Select(_ => new PostIndexItemDto {
                CoverPhoto    = _.CoverPhoto,
                Slug          = _.Slug,
                PublishDate   = _.PublishDate,
                Status        = _.Status,
                Summary       = _.Summary,
                Title         = _.Title,
                UserId        = _.CreatorUserId,
                AltTitle      = _.AltTitle,
                Author        = _.CreatorUser.Title,
                Id            = _.Id,
                PostTypeSlug  = _.PostType.Slug,
                Body          = _.Body,
                CategoryTitle = _.Category != null ? _.Category.Title : string.Empty,
                CommentCount  = _.Comments.Count(),
                IconName      = _.IconName,
                LikeCount     = _.Likes.Count(),
                Tags          = _.PostTags.Select(_ => new PostTagItemDto {
                    Title       = _.Tag.Title,
                    Id          = _.Tag.Id,
                    Description = _.Tag.Description,
                    Slug        = _.Tag.Slug
                })
            }).ToListAsync();

            return(await Task.FromResult(result));
        }