Beispiel #1
0
        public async Task SavePost(Post post)
        {
            string filePath  = GetFilePath(post);
            var    postSaved = PostModule.UpdatedNow(post);

            XDocument doc = new XDocument(
                new XElement("post",
                             new XElement("title", postSaved.Title),
                             new XElement("slug", postSaved.Slug),
                             new XElement("pubDate", postSaved.PubDate.ToString("yyyy-MM-dd HH:mm:ss")),
                             new XElement("lastModified", postSaved.LastModified.ToString("yyyy-MM-dd HH:mm:ss")),
                             new XElement("excerpt", postSaved.Excerpt),
                             new XElement("content", postSaved.Content),
                             new XElement("ispublished", postSaved.IsPublished),
                             new XElement("categories", string.Empty),
                             new XElement("comments", string.Empty)
                             ));

            XElement categories = doc.XPathSelectElement("post/categories");

            foreach (string category in post.Categories)
            {
                categories.Add(new XElement("category", category));
            }

            XElement comments = doc.XPathSelectElement("post/comments");

            foreach (Comment comment in post.Comments)
            {
                comments.Add(
                    new XElement("comment",
                                 new XElement("author", comment.Author),
                                 new XElement("email", comment.Email),
                                 new XElement("date", comment.PubDate.ToString("yyyy-MM-dd HH:m:ss")),
                                 new XElement("content", comment.Content),
                                 //new XAttribute("isAdmin", comment.IsAdmin),
                                 new XAttribute("id", comment.ID)
                                 ));
            }

            using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
            {
                await doc.SaveAsync(fs, SaveOptions.None, CancellationToken.None).ConfigureAwait(false);
            }

            // not equality !!!
            if (!_cache.Exists(p => p.ID == post.ID))
            {
                _cache.Add(post);
                SortCache();
            }
            else
            {
                var toUpdate = _cache.Find(p => p.ID == post.ID);
                _cache.Remove(toUpdate);
                _cache.Add(post);
                SortCache();
            }
        }