Example #1
0
        public async Task AddComment(string BlogPostID, string comment)
        {
            XDocument doc = new XDocument(
                new XElement("comment",
                             new XElement("BlogPostID", BlogPostID),
                             new XElement("comment", comment)
                             ));

            var id = Directory.GetFiles(ApplicationContext.CommentsFolder).Length + 1;//auto increment

            using (FileStream fs = new FileStream(ApplicationContext.CommentsFolder + @"\" + id + ".xml", FileMode.Create, FileAccess.ReadWrite))
            {
                await doc.SaveAsync(fs, SaveOptions.None, CancellationToken.None).ConfigureAwait(false);
            }

            BlogPostComment blogComment = new BlogPostComment
            {
                ID         = id.ToString(),
                BlogPostID = BlogPostID,
                Comment    = comment
            };

            var posts = GetCachedPosts();

            posts.Where(p => p.ID == BlogPostID)
            .Select(p => { p.Comments.Add(blogComment); return(p); })
            .ToList();

            Sort(ref posts);
            cache.Set(BLOG_POSTS, posts);
        }
Example #2
0
        /*
         * Retrieve blogposts and comments from XML and store them separately in the cachce
         */
        private void LoadFromDisk()
        {
            List <BlogPost>        posts    = new List <BlogPost>();
            List <BlogPostComment> comments = new List <BlogPostComment>();

            foreach (string file in Directory.EnumerateFiles(ApplicationContext.PostsFolder, "*.xml", SearchOption.TopDirectoryOnly))
            {
                XElement doc = XElement.Load(file);

                BlogPost post = new BlogPost
                {
                    ID            = Path.GetFileNameWithoutExtension(file),
                    Title         = ReadValue(doc, "title"),
                    Excerpt       = ReadValue(doc, "excerpt"),
                    Content       = ReadValue(doc, "content"),
                    Slug          = ReadValue(doc, "slug").ToLowerInvariant(),
                    PubDate       = DateTime.Parse(ReadValue(doc, "pubDate")),
                    LastModified  = DateTime.Parse(ReadValue(doc, "lastModified", DateTime.Now.ToString(CultureInfo.InvariantCulture))),
                    IsPublished   = bool.Parse(ReadValue(doc, "ispublished", "true")),
                    NumberOfLikes = Convert.ToInt32(ReadValue(doc, "numberOfLikes")),
                };

                posts.Add(post);
            }

            foreach (string file in Directory.EnumerateFiles(ApplicationContext.CommentsFolder, "*.xml", SearchOption.TopDirectoryOnly))
            {
                XElement doc = XElement.Load(file);


                BlogPostComment comment = new BlogPostComment
                {
                    ID         = Path.GetFileNameWithoutExtension(file),
                    BlogPostID = ReadValue(doc, "BlogPostID"),
                    Comment    = ReadValue(doc, "comment"),
                };

                comments.Add(comment);
            }

            posts = AssignCommentsToPosts(posts, comments);

            Sort(ref posts);

            cache.Set(BLOG_POSTS, posts);
        }