private XElement BuildPost(MetaPost post) { var stru = new XElement("struct"); stru.Add(new XElement("member", new XElement("name", "postid"), new XElement("value", new XElement("string", post.PostId)))); stru.Add(new XElement("member", new XElement("name", "title"), new XElement("value", new XElement("string", post.Title)))); stru.Add(new XElement("member", new XElement("name", "description"), new XElement("value", new XElement("string", post.Description)))); stru.Add(new XElement("member", new XElement("name", "link"), new XElement("value", new XElement("string", post.Link)))); stru.Add(new XElement("member", new XElement("name", "wp_slug"), new XElement("value", new XElement("string", post.Slug)))); stru.Add(new XElement("member", new XElement("name", "mt_excerpt"), new XElement("value", new XElement("string", post.Excerpt)))); stru.Add(new XElement("member", new XElement("name", "mt_allow_comments"), new XElement("value", new XElement("int", post.CommentPolicy)))); stru.Add(new XElement("member", new XElement("name", "dateCreated"), new XElement("value", new XElement("dateTime.iso8601", ConvertDatetoISO8601(post.PostDate))))); stru.Add(new XElement("member", new XElement("name", "publish"), new XElement("value", new XElement("boolean", post.Publish ? "1" : "0")))); if (post.Tags != null && post.Tags.Count > 0) { stru.Add(new XElement("member", new XElement("name", "mt_keywords"), new XElement("value", new XElement("string", string.Join(",", post.Tags))))); } if (post.Categories != null && post.Categories.Count > 0) { XElement data = new XElement("data"); foreach (var cat in post.Categories) { data.Add(new XElement("value", new XElement("string", cat))); } stru.Add(new XElement("member", new XElement("name", "categories"), new XElement("value", new XElement("array", data)))); } return(stru); }
public async Task <bool> EditPostAsync(string postId, string userName, string password, MetaPost post, bool publish) { var user = await ValidUserAsync(userName, password); try { var blogPost = new BlogPost { Id = Convert.ToInt32(postId), UserId = user.Id, Title = post.Title, Slug = post.Slug, Body = post.Description, Excerpt = post.Excerpt, CreatedOn = post.PostDate, CategoryTitle = (post.Categories == null || post.Categories.Count <= 0) ? null : post.Categories[0], TagTitles = post.Tags, Status = publish ? EPostStatus.Published : EPostStatus.Draft, CommentStatus = await GetPostCommentStatusAsync(post.CommentPolicy), }; await _blogSvc.UpdatePostAsync(blogPost); return(true); } catch (Exception ex) { throw new MetaWeblogException(EMetaWeblogCode.EditPost, ex.Message); } }
/// <summary> /// Returns <see cref="MetaPost"/> given the param that holds the list of members. /// </summary> /// <param name="ele"></param> /// <returns></returns> private MetaPost ParseMetaPost(XElement ele) { var post = new MetaPost(); var memberList = ele.Element("value").Element("struct").Elements("member"); // title var title = memberList.SingleOrDefault(m => m.Element("name").Value == "title"); post.Title = title?.Element("value").Value; // description var description = memberList.SingleOrDefault(m => m.Element("name").Value == "description"); post.Description = description?.Element("value").Value; // link var link = memberList.SingleOrDefault(m => m.Element("name").Value == "link"); post.Link = link?.Element("value").Value; // allowComments var allowComments = memberList.SingleOrDefault(m => m.Element("name").Value == "mt_allow_comments"); post.CommentPolicy = allowComments?.Element("value").Value; // excerpt var excerpt = memberList.SingleOrDefault(m => m.Element("name").Value == "mt_excerpt"); post.Excerpt = excerpt?.Element("value").Value; // slug var slug = memberList.SingleOrDefault(m => m.Element("name").Value == "wp_slug"); post.Slug = slug?.Element("value").Value; // author var authorId = memberList.SingleOrDefault(m => m.Element("name").Value == "wp_author_id"); post.AuthorId = authorId?.Element("value").Value; // categories var categories = memberList.SingleOrDefault(m => m.Element("name").Value == "categories"); if (categories != null) { var str = categories.Descendants("string").FirstOrDefault(); if (str != null) { post.Categories.Add(str.Value); } } // postDate try { var dateCreated = memberList.SingleOrDefault(m => m.Element("name").Value == "dateCreated"); var pubDate = memberList.SingleOrDefault(m => m.Element("name").Value == "pubDate"); if (dateCreated != null) { post.PostDate = DateTimeOffset.ParseExact(dateCreated.Element("value").Value, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } else if (pubDate != null) { post.PostDate = DateTimeOffset.ParseExact(pubDate.Element("value").Value, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } } catch (Exception ex) { post.PostDate = DateTimeOffset.MinValue; _logger.LogError(ex.Message); } // tags var keywords = memberList.SingleOrDefault(m => m.Element("name").Value == "mt_keywords"); if (keywords != null) { var tagsList = keywords.Element("value").Value; // comma-delimited foreach (var item in tagsList.Split(',').Where(item => string.IsNullOrEmpty(post.Tags.Find(t => t.Equals(item.Trim(), StringComparison.OrdinalIgnoreCase))))) { post.Tags.Add(item.Trim()); } } _logger.LogDebug("ParseMetaPost {@MetaPost}", post); return(post); }