예제 #1
0
        public bool EditPost(string postid, string username, string password, Post post, bool publish)
        {
            if (validateUser(username, password))
            {
                if (long.TryParse(postid, out var id))
                {
                    var author = getAuthor(username);

                    var up = new UpdateBlogPostCommand
                    {
                        Id             = id,
                        NewTitle       = post.title,
                        NewAuthor      = author,
                        NewContent     = post.description,
                        NewDescription = post.mt_excerpt,
                        NewPublic      = publish,
                        NewPublishOn   = publish ? DateTime.Now : DateTime.Now.AddDays(365),
                        NewImage       = getImgUrl(post.description),
                        LastModifiedAt = DateTime.Now
                    };

                    var result = _cp.ProcessAsync(up).GetAwaiter().GetResult();

                    if (result.Succeeded)
                    {
                        if (post.categories.Any())
                        {
                            _logger.LogInformation($"Successfully edited post via metaweblog {post.title}");
                            var sc = new SetBlogPostCategoriesByStringArrayCommand
                            {
                                Categories = post.categories.ToList(),
                                BlogPostId = result.Command.Id
                            };

                            var scResult = _cp.ProcessAsync(sc).GetAwaiter().GetResult();

                            if (scResult.Succeeded)
                            {
                                _logger.LogInformation($"Successfully set categories for post {post.title}");
                                return(true);
                            }
                            else
                            {
                                _logger.LogError($"Failed to set categories for post {post.title}");
                                return(false);
                            }
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }
            }
            _logger.LogError($"Failed to edit post {post.title}");
            return(false);
        }
예제 #2
0
        public string AddPost(string blogid, string username, string password, Post post, bool publish)
        {
            if (validateUser(username, password))
            {
                var author = getAuthor(username);

                var np = new AddBlogPostCommand()
                {
                    Title       = post.title,
                    Author      = author,
                    Description = post.mt_excerpt,
                    Content     = post.description,
                    CreatedAt   = post.dateCreated,
                    ModifiedAt  = DateTime.Now,
                    Public      = publish,
                    PublishOn   = DateTime.Now,
                    Image       = getImgUrl(post.description),
                    IsHtml      = true
                };

                var result = _cp.ProcessAsync(np).GetAwaiter().GetResult();

                if (result.Succeeded)
                {
                    _logger.LogInformation($"Successfully added post through metaweblog {post.title}");
                    if (post.categories.Any())
                    {
                        var sc = new SetBlogPostCategoriesByStringArrayCommand
                        {
                            Categories = post.categories.ToList(),
                            BlogPostId = result.Command.Id
                        };

                        var scResult = _cp.ProcessAsync(sc).GetAwaiter().GetResult();

                        if (scResult.Succeeded)
                        {
                            _logger.LogInformation($"Successfully added categories for post {post.title} through metaweblog");
                            return(result.Command.Id.ToString());
                        }
                    }
                    else
                    {
                        return(result.Command.Id.ToString());
                    }
                }
            }
            _logger.LogError($"Error adding post through metaweblog {post.title}");
            return("-1");
        }