示例#1
0
        private TwitterCardContent ConstructTwitterCardContent(PostReadModel post)
        {
            var postImageUrl = ExtractPostImageUrl(post);

            return(new TwitterCardContent(
                       post.Title,
                       post.GeneratePostAbsoluteUrl(),
                       !string.IsNullOrWhiteSpace(post.Abstract) ? post.Abstract : null,
                       postImageUrl,
                       HardcodedConstants.BlogTwitterHandle,
                       HardcodedConstants.BlogTwitterHandle));
        }
        public async Task <ActionResult <PostDTO> > CreatePost(PostReadModel postReadModel)
        {
            var post = new Post
            {
                Question = postReadModel.Question,
                Images   = new List <Image>()
            };

            _context.Posts.Add(post);
            await _context.SaveChangesAsync();


            return(CreatedAtAction(
                       nameof(GetPost),
                       new { id = post.PostId },
                       PostToPostDTO(post)));
        }
示例#3
0
        private string ExtractPostImageUrl(PostReadModel post)
        {
            try
            {
                var htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(post.Content);

                // see https://stackoverflow.com/a/4835960/463785
                var imageNodes = htmlDoc.DocumentNode.SelectNodes("//img[@src]");
                if (imageNodes != null && imageNodes.Any())
                {
                    return(imageNodes.First().Attributes["src"].Value);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "Error while extracting an image for blog post with Id '{BlogPostId}' and Title '{BlogPostTitle}'",
                                 post.Id, post.Title);
            }

            return(null);
        }
 public static string GeneratePostAbsoluteUrl(this PostReadModel post)
 {
     return(string.Format(HardcodedConstants.BlogPostUriFormat, post.DefaultSlug.Path));
 }
        private static PostReadModel GetSamplePost2()
        {
            const string path    = "sample-post-html-2.txt";
            string       content = GetPostContent(path);

            var user = new UserReadModel
            {
                Id   = "djidiweh",
                Name = "Tugberk Ugurlu"
            };

            var samplePost = new PostReadModel
            {
                Id                   = "y9h9huj9u",
                Title                = "ASP.NET Core Authentication in a Load Balanced Environment with HAProxy and Redis",
                Abstract             = "Token based authentication is a fairly common way of authenticating a user for an HTTP application. However, handling this in a load balanced environment has always involved extra caring. In this post, I will show you how this is handled in ASP.NET Core by demonstrating it with HAProxy and Redis through the help of Docker.",
                Language             = "en-US",
                Content              = content,
                Authors              = new[] { user },
                CommentStatusActions = new[]
                {
                    new CommentStatusActionRecordReadModel
                    {
                        Status     = CommentableStatusReadModel.Enabled,
                        RecordedBy = user,
                        RecordedOn = DateTime.UtcNow
                    }
                },
                ApprovalStatus = ApprovalStatusReadModel.Approved,
                CreationRecord = new ChangeRecordReadModel
                {
                    RecordedBy = user,
                    RecordedOn = DateTime.UtcNow,
                    IpAddress  = "127.0.0.1"
                },
                Slugs = new[]
                {
                    new SlugReadModel
                    {
                        Path      = "asp-net-core-authentication-in-a-load-balanced-environment-with-haproxy-and-redis",
                        IsDefault = true,
                        CreatedOn = DateTime.UtcNow
                    }
                },
                Tags = new[]
                {
                    new TagReadModel
                    {
                        Name  = "ASP.NET Core",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "asp-net-core",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    },

                    new TagReadModel
                    {
                        Name  = "ASP.NET",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "asp-net",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    },

                    new TagReadModel
                    {
                        Name  = "Security",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "security",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    },

                    new TagReadModel
                    {
                        Name  = "HTTP",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "http",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    },

                    new TagReadModel
                    {
                        Name  = "Docker",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "docker",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    }
                }
            };

            return(samplePost);
        }
        private static PostReadModel GetSamplePost1()
        {
            const string path    = "sample-post-html-1.txt";
            string       content = GetPostContent(path);

            var user = new UserReadModel
            {
                Id   = "djidiweh",
                Name = "Tugberk Ugurlu"
            };

            var samplePost = new PostReadModel
            {
                Id                   = "ydy982d",
                Title                = "Defining What Good Looks Like for a Software Engineer",
                Abstract             = "What does good look like for a software engineer? This is a question you might be asking frequently to yourself and I tried to share my thoughts on the topic with this blog post.",
                Language             = "en-US",
                Content              = content,
                Authors              = (new[] { user }),
                CommentStatusActions = new[]
                {
                    new CommentStatusActionRecordReadModel
                    {
                        Status     = CommentableStatusReadModel.Enabled,
                        RecordedBy = user,
                        RecordedOn = DateTime.UtcNow
                    }
                },
                ApprovalStatus = ApprovalStatusReadModel.Approved,
                CreationRecord = new ChangeRecordReadModel
                {
                    RecordedBy = user,
                    RecordedOn = DateTime.UtcNow,
                    IpAddress  = "127.0.0.1"
                },
                Slugs = new[]
                {
                    new SlugReadModel
                    {
                        Path      = "defining-what-good-looks-like-for-a-software-engineer",
                        IsDefault = true,
                        CreatedOn = DateTime.UtcNow
                    }
                },
                Tags = new[]
                {
                    new TagReadModel
                    {
                        Name  = "Software Engineer",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "software-engineer",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    },

                    new TagReadModel
                    {
                        Name  = "Software Development",
                        Slugs = new[]
                        {
                            new SlugReadModel
                            {
                                Path      = "software-development",
                                IsDefault = true,
                                CreatedOn = DateTime.UtcNow
                            }
                        }
                    }
                }
            };

            return(samplePost);
        }