public async Task <Result <PostModel> > Handle(GetPostQuery request, CancellationToken cancellationToken) { var post = await _uow.Posts.SingleOrDefaultAsync(p => p.Published != null && p.Slug.Equals(request.Slug), cancellationToken); if (post == null) { return(Result <PostModel> .Fail(new NotFoundException <Post>($"Could not find post for slug: \"{request.Slug}\""))); } var model = new PostModel { Blog = new BlogModel(_blogOptions.Value), Post = post, Next = null, Previous = null }; if (string.IsNullOrWhiteSpace(model.Post.CoverUrl)) { model.Post.CoverUrl = model.Blog.CoverUrl; model.Post.CoverCaption = model.Blog.CoverCaption; model.Post.CoverLink = model.Blog.CoverLink; } model.Next = await _uow.Posts.GetNextAsync(post.Published.Value, cancellationToken); model.Previous = await _uow.Posts.GetPreviousAsync(post.Published.Value, cancellationToken); model.Post = _postUrlHelper.ReplaceUrlFormatWithBaseUrl(model.Post); model.Next = _postUrlHelper.ReplaceUrlFormatWithBaseUrl(model.Next); model.Previous = _postUrlHelper.ReplaceUrlFormatWithBaseUrl(model.Previous); return(Result <PostModel> .Success(model)); }
public void ReplaceUrlFormatWithBaseUrl_Content() { var result = _postUrlHelper.ReplaceUrlFormatWithBaseUrl(_postWithUrlFormat); result.Should().NotBeNull(); result.Content.Should().Be("content with an url: http://localhost/azure-blob-storage/container-name/pineblog-tests/content-url-1. nice isn't it?"); }
/// <summary> /// Handle the GetPostByIdQuery request. /// </summary> /// <param name="request">The GetPostByIdQuery request.</param> /// <param name="cancellationToken">A cancellation token.</param> public async Task <Result <Post> > Handle(GetPostByIdQuery request, CancellationToken cancellationToken) { var post = await _uow.Posts.SingleOrDefaultAsync(p => p.Id.Equals(request.Id), cancellationToken); if (post == null) { return(Result <Post> .Fail(new NotFoundException <Post>($"Could not find post, id: \"{request.Id}\""))); } post = _postUrlHelper.ReplaceUrlFormatWithBaseUrl(post); return(Result <Post> .Success(post)); }
/// <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 = RankPosts(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 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)); }