Exemplo n.º 1
0
        public IActionResult ViewBlogPost(int year, int month, string slug)
        {
            var blogPost = _blogPostsConfig.Blogs.SingleOrDefault(b => b.Slug == slug);

            if (blogPost == null)
            {
                return(this.NotFound());
            }
            blogPost.Content = BlogPostContentReader.GetContent(blogPost, _cache, _logger);
            return(View(blogPost));
        }
        public IActionResult Index(string searchText)
        {
            List <BlogPost> result = new List <BlogPost>();

            if (!string.IsNullOrEmpty(searchText) && searchText.Length > 2)
            {
                foreach (var post in _blogPostsConfig.Blogs)
                {
                    // Add if a category of the post matches the search text
                    if (post.Categories.Any(c => c.Equals(searchText, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Add(post);
                        continue;
                    }

                    // Add if a tag of the post matches the search text
                    if (post.Tags.Any(c => c.Equals(searchText, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Add(post);
                        continue;
                    }

                    if (string.IsNullOrEmpty(post.Content))
                    {
                        post.Content = BlogPostContentReader.GetContent(post, _cache, _logger);
                    }

                    if (post.Content.Contains(searchText, StringComparison.OrdinalIgnoreCase))
                    {
                        result.Add(post);
                    }
                }
            }
            var model = new BlogPostsViewModel(_blogPostsConfig)
            {
                SearchText      = searchText,
                PageOfBlogPosts = result
            };

            return(View(model));
        }