Example #1
0
        protected async Task UpdatePostTags(PostViewModel model, Post entity)
        {
            var tagsIds = await _postManager.GetTagsAsync(entity.Id);

            foreach (var item in tagsIds)
            {
                await _postManager.RemoveTagsAsync(entity, item.TagsId);
            }

            foreach (var name in model.SelectTags)
            {
                string newName = name.Trim();
                if (string.IsNullOrWhiteSpace(newName))
                {
                    continue;
                }

                // add
                var tagEntity = await _tagsManager.CreateOrUpdateAsync(newName);

                entity.Tags.Add(new PostTags()
                {
                    PostId = entity.Id, TagsId = tagEntity.Id
                });
            }

            //if (!string.IsNullOrEmpty(model.TagsString))
            //{
            //    var names = model.TagsString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            //    foreach (var name in names)
            //    {
            //        string newName = name.Trim();
            //        if (string.IsNullOrWhiteSpace(newName))
            //            continue;

            //        // add
            //        var tagEntity = await _tagsManager.CreateOrUpdateAsync(newName);

            //        entity.Tags.Add(new PostTags() { PostId = entity.Id, TagsId = tagEntity.Id });
            //    }

            //}
        }
        private async Task <bool> AddPost(BlogMlExtendedPost extPost)
        {
            var p = new Post();

            p.Title                = extPost.BlogPost.Title;
            p.CreationTime         = extPost.BlogPost.DateCreated;
            p.PublishedTime        = extPost.BlogPost.DateCreated;
            p.LastModificationTime = extPost.BlogPost.DateModified;
            p.Content              = extPost.BlogPost.Content.UncodedText;
            p.Description          = extPost.BlogPost.Excerpt.UncodedText;
            p.IsDraft              = !extPost.BlogPost.Approved;
            p.ViewsCount           = GetValue(extPost.BlogPost.Views);

            if (extPost.BlogPost.HasExcerpt)
            {
                p.Description = extPost.BlogPost.Excerpt.UncodedText;
            }

            if (!string.IsNullOrEmpty(extPost.PostUrl))
            {
                // looking for a Slug with patterns such as:
                //    /some-slug.aspx
                //    /some-slug.html
                //    /some-slug
                //
                Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase);
                if (slugMatch.Success)
                {
                    p.Slug = slugMatch.Groups[1].Value.Trim();
                }
            }

            if (string.IsNullOrEmpty(p.Slug))
            {
                p.Slug = p.GetSeName();
            }

            // skip if exists
            if (await _postManager.FindBySlugAsync(p.Slug) != null)
            {
                return(false);
            }

            if (extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0)
            {
                // p.UserId = extPost.BlogPost.Authors[0].Ref;
                p.UserId = _author.Id;
            }


            if (extPost.Categories != null && extPost.Categories.Count > 0)
            {
                foreach (var item in extPost.Categories)
                {
                    p.Categories.Add(new PostCategory()
                    {
                        CategoryId = item.Id, PostId = item.Id
                    });
                }
            }


            if (extPost.Tags != null && extPost.Tags.Count > 0)
            {
                foreach (var item in extPost.Tags)
                {
                    var tag = await _tagsManager.CreateOrUpdateAsync(item);

                    p.Tags.Add(new PostTags()
                    {
                        PostId = p.Id, TagsId = tag.Id
                    });
                }
            }

            await _postManager.CreateAsync(p);

            if (extPost.Comments != null && extPost.Comments.Count > 0)
            {
                foreach (var comment in extPost.Comments)
                {
                    comment.PostId = p.Id;
                    try
                    {
                        await _commentManager.CreateAsync(comment);
                    }
                    catch (Exception)
                    {
                    }
                }

                p.CommentsCount = extPost.Comments.Count;

                await _postManager.UpdateAsync(p);
            }

            return(true);
        }
        /// <summary>
        /// metaWeblog.editPost method
        /// </summary>
        /// <param name="postId">
        /// post guid in string format
        /// </param>
        /// <param name="userName">
        /// login username
        /// </param>
        /// <param name="password">
        /// login password
        /// </param>
        /// <param name="sentPost">
        /// struct with post details
        /// </param>
        /// <param name="publish">
        /// mark as published?
        /// </param>
        /// <returns>
        /// 1 if successful
        /// </returns>
        internal async Task <bool> EditPost(string postId, string userName, string password, MWAPost sentPost, bool publish)
        {
            var currentUser = await GetVerifyUserAsync(userName, password);

            //if (!_permissionChecker.IsValid(currentUser, PermissionKeys.PostEdit))
            //{
            //    throw new MetaWeblogException("11", "User authentication failed");
            //}

            var post = await _postManager.FindByIdAsync(postId);

            string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author;

            var authorUser = await _userManager.FindByEmailAsync(author);

            if (authorUser != null)
            {
                post.UserId = authorUser.Id;
            }

            post.Title       = sentPost.title;
            post.Content     = sentPost.description;
            post.IsDraft     = !publish;
            post.Slug        = sentPost.slug;
            post.Description = sentPost.excerpt;
            if (sentPost.postDate != new DateTime())
            {
                post.PublishedTime = sentPost.postDate;
            }

            if (sentPost.commentPolicy != string.Empty)
            {
                post.EnableComment = sentPost.commentPolicy == "1";
            }

            post.Categories.Clear();
            foreach (var item in sentPost.categories.Where(c => c != null && c.Trim() != string.Empty))
            {
                Category cat;
                if (LookupCategoryGuidByName(item, out cat))
                {
                    post.Categories.Add(new PostCategory()
                    {
                        CategoryId = cat.Id, PostId = post.Id
                    });
                }
                else
                {
                    // Allowing new categories to be added.  (This breaks spec, but is supported via WLW)
                    var newcat = new Category()
                    {
                        Name         = item,
                        DisplayOrder = 1,
                    };

                    post.Categories.Add(new PostCategory()
                    {
                        Category = newcat, PostId = post.Id
                    });
                }
            }

            post.Tags.Clear();
            foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty))
            {
                var tag = await _tagsManager.CreateOrUpdateAsync(item);

                post.Tags.Add(new PostTags()
                {
                    TagsId = tag.Id, PostId = post.Id
                });
            }

            await _postManager.UpdateAsync(post);

            return(true);
        }