Esempio n. 1
0
        /// <summary>
        /// Returns a new string in which all occurrences of the UrlFormat replaced with the BaseUrl.
        /// </summary>
        public Post ReplaceUrlFormatWithBaseUrl(Post post)
        {
            if (post == null)
            {
                return(post);
            }

            post.Content  = _fileUrlHelper.ReplaceUrlFormatWithBaseUrl(post.Content);
            post.CoverUrl = _fileUrlHelper.ReplaceUrlFormatWithBaseUrl(post.CoverUrl);
            return(post);
        }
Esempio n. 2
0
            /// <summary>
            /// Handle the SearchPostsQuery request.
            /// </summary>
            /// <param name="request">The SearchPostsQuery request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result <PostListModel> > Handle(SearchPostsQuery request, CancellationToken cancellationToken)
            {
                var itemsPerPage = (request.ItemsPerPage.HasValue) ? request.ItemsPerPage : _blogOptions.Value.ItemsPerPage;
                var pager        = new Pager(request.Page, itemsPerPage.Value);

                var pagingUrlPartFormat = _blogOptions.Value.PagingUrlPartFormat;

                var predicates = new List <Expression <Func <Post, bool> > >();

                predicates.Add(p => p.Published != null);

                IEnumerable <Post> posts;

                if (!string.IsNullOrWhiteSpace(request.SearchQuery))
                {
                    predicates.Add(BuildSearchExpression(request.SearchQuery));
                    pagingUrlPartFormat += "&" + string.Format(_blogOptions.Value.SearchQueryUrlPartFormat, HttpUtility.UrlEncode(request.SearchQuery));

                    posts = await _uow.Posts.GetAsync(predicates, 0, int.MaxValue, cancellationToken);

                    posts = _postRanker.Rank(posts, request.SearchQuery);
                    posts = await GetPagedListAsync(posts, predicates, pager, pagingUrlPartFormat, cancellationToken);
                }
                else
                {
                    posts = await GetPagedListAsync(predicates, pager, pagingUrlPartFormat, cancellationToken);
                }

                posts = posts.Select(p => _postUrlHelper.ReplaceUrlFormatWithBaseUrl(p));

                var model = new PostListModel
                {
                    Blog         = new BlogModel(_blogOptions.Value),
                    PostListType = PostListType.Blog,
                    Posts        = posts,
                    Pager        = pager
                };

                model.Blog.CoverUrl = _fileUrlHelper.ReplaceUrlFormatWithBaseUrl(model.Blog.CoverUrl);

                if (!string.IsNullOrWhiteSpace(request.SearchQuery))
                {
                    model.PostListType = PostListType.Search;
                    model.SearchQuery  = request.SearchQuery;
                }

                return(Result <PostListModel> .Success(model));
            }
            /// <summary>
            /// Handle the GetBlogSettigsQuery request.
            /// </summary>
            /// <param name="request">The GetBlogSettigsQuery request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result <BlogSettings> > Handle(GetBlogSettigsQuery request, CancellationToken cancellationToken)
            {
                var blogSettings = await _uow.BlogSettings.SingleOrDefaultAsync(cancellationToken);

                if (blogSettings == null)
                {
                    blogSettings = new BlogSettings
                    {
                        Title        = _blogOptions.Value.Title,
                        Description  = _blogOptions.Value.Description,
                        CoverCaption = _blogOptions.Value.CoverCaption,
                        CoverLink    = _blogOptions.Value.CoverLink,
                        CoverUrl     = _blogOptions.Value.CoverUrl
                    };
                }

                blogSettings.CoverUrl = _fileUrlHelper.ReplaceUrlFormatWithBaseUrl(blogSettings.CoverUrl);

                return(Result <BlogSettings> .Success(blogSettings));
            }
            /// <summary>
            /// Handle the GetPagedPostListQuery request.
            /// </summary>
            /// <param name="request">The GetPagedPostListQuery request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result <PostListModel> > Handle(GetPagedPostListQuery request, CancellationToken cancellationToken)
            {
                var itemsPerPage = (request.ItemsPerPage.HasValue) ? request.ItemsPerPage : _blogOptions.Value.ItemsPerPage;
                var pager        = new Pager(request.Page, itemsPerPage.Value);

                var pagingUrlPartFormat = _blogOptions.Value.PagingUrlPartFormat;

                var predicates = new List <Expression <Func <Post, bool> > >();

                if (!request.IncludeUnpublished)
                {
                    predicates.Add(p => p.Published != null);
                }
                if (!string.IsNullOrWhiteSpace(request.Category))
                {
                    predicates.Add(p => p.Categories.Contains(request.Category));
                    pagingUrlPartFormat += "&" + string.Format(_blogOptions.Value.CategoryUrlPartFormat, request.Category);
                }

                var posts = await GetPagedListAsync(predicates, pager, pagingUrlPartFormat, cancellationToken);

                posts = posts.Select(p => _postUrlHelper.ReplaceUrlFormatWithBaseUrl(p));

                var model = new PostListModel
                {
                    Blog         = new BlogModel(_blogOptions.Value),
                    PostListType = PostListType.Blog,
                    Posts        = posts,
                    Pager        = pager
                };

                model.Blog.CoverUrl = _fileUrlHelper.ReplaceUrlFormatWithBaseUrl(model.Blog.CoverUrl);

                if (!string.IsNullOrWhiteSpace(request.Category))
                {
                    model.PostListType = PostListType.Category;
                    model.Category     = request.Category;
                }

                return(Result <PostListModel> .Success(model));
            }