コード例 #1
0
ファイル: XmlBlogRepository.cs プロジェクト: cnurse/Naif.Blog
        protected override void SavePost(Post post, string file)
        {
            XDocument doc = new XDocument(
                            new XElement("post",
                                new XElement("title", post.Title),
                                new XElement("slug", post.Slug),
                                new XElement("author", post.Author),
                                new XElement("pubDate", post.PubDate.ToString("yyyy-MM-dd HH:mm:ss")),
                                new XElement("lastModified", post.LastModified.ToString("yyyy-MM-dd HH:mm:ss")),
                                new XElement("excerpt", post.Excerpt),
                                new XElement("content", post.Content),
                                new XElement("categories", string.Empty),
                                new XElement("tags", post.Keywords),
                                new XElement("ispublished", post.IsPublished)
                            ));

            XElement categories = doc.Document.Element("post").Element("categories");
            foreach (string category in post.Categories)
            {
                categories.Add(new XElement("category", category));
            }
            using(var stream = new FileStream(file, FileMode.Create))
            {
                doc.Save(stream);
            }
        }
コード例 #2
0
 protected override void SavePost(Post post, string file)
 {
     using (StreamWriter w = File.CreateText(file))
     {
         JsonSerializer serializer = new JsonSerializer();
         serializer.Serialize(w, post);
     }
 }
コード例 #3
0
        public virtual void Delete(Post post)
        {
            var cacheKey = string.Format(PostsCacheKey, post.BlogId);
            var postsFolder = string.Format(PostsFolder, RootFolder, post.BlogId);

            string file = Path.Combine(postsFolder, post.PostId + "." + FileExtension);

            File.Delete(file);

            MemoryCache.Remove(cacheKey);
            Logger.LogInformation($"{cacheKey} cleared.");
        }
コード例 #4
0
ファイル: XmlBlogRepository.cs プロジェクト: cnurse/Naif.Blog
        private static void LoadCategories(Post post, XElement doc)
        {
            XElement categories = doc.Element("categories");
            if (categories == null)
                return;

            List<string> list = new List<string>();

            foreach (var node in categories.Elements("category"))
            {
                list.Add(node.Value);
            }

            post.Categories = list.ToArray();
        }
コード例 #5
0
ファイル: XmlBlogRepository.cs プロジェクト: cnurse/Naif.Blog
        protected override Post GetPost(string file, string blogId)
        {
            XElement doc = XElement.Load(file);

            var post = new Post()
            {
                PostId = Path.GetFileNameWithoutExtension(file),
                BlogId = blogId,
                Title = ReadValue(doc, "title"),
                Author = ReadValue(doc, "author"),
                Excerpt = ReadValue(doc, "excerpt"),
                Content = ReadValue(doc, "content"),
                Keywords = ReadValue(doc, "tags"),
                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")),
            };

            LoadCategories(post, doc);
            return post;
        }
コード例 #6
0
        public IActionResult EditPost(string postid, string username, string password, Post post, bool publish)
        {
            Post match = BlogRepository.GetAll(Blog.Id).FirstOrDefault(p => p.PostId == postid);

            if (match != null)
            {
                match.Title = post.Title;
                match.Excerpt = post.Excerpt;
                match.Content = post.Content;

                if (!string.Equals(match.Slug, post.Slug, StringComparison.OrdinalIgnoreCase))
                {
                    match.Slug = CreateSlug(post.Slug);
                }

                match.Categories = post.Categories;
                match.Keywords = post.Keywords;
                match.IsPublished = publish;

                BlogRepository.Save(match);
            }

            return new XmlRpcResult(match != null);
        }
コード例 #7
0
 public async Task<IViewComponentResult> InvokeAsync(Post post)
 {
     return View(post);
 }
コード例 #8
0
        public IActionResult NewPost(string blogid, string username, string password, Post post, bool publish)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(post.Slug))
                {
                    post.Slug = CreateSlug(post.Slug);
                }
                else
                {
                    post.Slug = CreateSlug(post.Title);
                }
            }
            catch (Exception exc)
            {
                return new XmlRpcResult(exc);
            }

            post.IsPublished = publish;
            post.BlogId = blogid;
            BlogRepository.Save(post);

            return new XmlRpcResult(post.PostId);
        }
コード例 #9
0
 protected abstract void SavePost(Post post, string file);
コード例 #10
0
        public void Save(Post post)
        {
            var cacheKey = string.Format(PostsCacheKey, post.BlogId);
            var postsFolder = string.Format(PostsFolder, RootFolder, post.BlogId);

            string file = Path.Combine(postsFolder, post.PostId + "." + FileExtension);
            post.LastModified = DateTime.UtcNow;

            SavePost(post, file);

            MemoryCache.Remove(cacheKey);

            Logger.LogInformation(!File.Exists(file)
                ? $"New Post - {post.PostId} created."
                : $"Post - {post.PostId} updated.");

            Logger.LogInformation($"{cacheKey} cleared.");
        }