public async Task SitemapXml()
        {
            string host = Request.Scheme + "://" + Request.Host;

            Response.ContentType = "application/xml";

            using (var xml = XmlWriter.Create(Response.Body, new XmlWriterSettings {
                Indent = true
            }))
            {
                xml.WriteStartDocument();
                xml.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

                var posts = await _blog.GetPosts(int.MaxValue);

                foreach (Models.Post post in posts)
                {
                    var lastMod = new[] { post.PubDate, post.LastModified };

                    xml.WriteStartElement("url");
                    xml.WriteElementString("loc", host + PostModule.GetLink(post));
                    xml.WriteElementString("lastmod", lastMod.Max().ToString("yyyy-MM-ddThh:mmzzz"));
                    xml.WriteEndElement();
                }

                xml.WriteEndElement();
            }
        }
        public async Task <IActionResult> AddComment(string postId, CommentVM comment)
        {
            var post = await _blog.GetPostById(postId);

            if (!ModelState.IsValid)
            {
                return(View("Post", post));
            }

            if (post == null || !PostModule.AreCommentsOpen(post, _settings.Value.CommentsCloseAfterDays))
            {
                return(NotFound());
            }
            var cleanComment = CommentVMModule.toComment(comment);

            // the website form key should have been removed by javascript
            // unless the comment was posted by a spam robot
            if (!Request.Form.ContainsKey("website"))
            {
                post = PostModule.AddComment(cleanComment, post);
                await _blog.SavePost(post);
            }

            return(Redirect(PostModule.GetLink(post) + "#" + cleanComment.ID));
        }
        private WilderMinds.MetaWeblog.Post ToMetaWebLogPost(Models.Post post)
        {
            var    request = _context.HttpContext.Request;
            string url     = request.Scheme + "://" + request.Host;

            return(new WilderMinds.MetaWeblog.Post
            {
                postid = post.ID,
                title = post.Title,
                wp_slug = post.Slug,
                permalink = url + PostModule.GetLink(post),
                dateCreated = post.PubDate,
                description = post.Content,
                categories = post.Categories.ToArray()
            });
        }
        public async Task <IActionResult> UpdatePost(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(View("Edit", post));
            }

            var existing = await _blog.GetPostById(post.ID) ?? post;

            string categories = Request.Form["categories"];

            existing = PostModule.UpdateWith(existing, post);
            existing = PostModule.WithCategories(categories, post);

            await _blog.SavePost(existing);

            await SaveFilesToDisk(existing);

            return(Redirect(PostModule.GetLink(existing)));
        }
        public async Task <IActionResult> DeleteComment(string postId, string commentId)
        {
            var post = await _blog.GetPostById(postId);

            if (post == null)
            {
                return(NotFound());
            }

            var comment = post.Comments.FirstOrDefault(c => c.ID.Equals(commentId, StringComparison.OrdinalIgnoreCase));

            if (comment == null)
            {
                return(NotFound());
            }
            post = PostModule.RemoveComment(comment, post);
            await _blog.SavePost(post);

            return(Redirect(PostModule.GetLink(post) + "#comments"));
        }
        public async Task Rss(string type)
        {
            Response.ContentType = "application/xml";
            string host = Request.Scheme + "://" + Request.Host;

            using (XmlWriter xmlWriter = XmlWriter.Create(Response.Body, new XmlWriterSettings()
            {
                Async = true, Indent = true
            }))
            {
                var posts = await _blog.GetPosts(10);

                var writer = await GetWriter(type, xmlWriter, posts.Max(p => p.PubDate));

                foreach (Models.Post post in posts)
                {
                    var item = new AtomEntry
                    {
                        Title       = post.Title,
                        Description = post.Content,
                        Id          = host + PostModule.GetLink(post),
                        Published   = post.PubDate,
                        LastUpdated = post.LastModified,
                        ContentType = "html",
                    };

                    foreach (string category in post.Categories)
                    {
                        item.AddCategory(new SyndicationCategory(category));
                    }

                    item.AddContributor(new SyndicationPerson(_settings.Value.Owner, "*****@*****.**"));
                    item.AddLink(new SyndicationLink(new Uri(item.Id)));

                    await writer.Write(item);
                }
            }
        }