Ejemplo n.º 1
0
        public async Task UpdateBlogPostCommandHandler_UpdatesBlog()
        {
            var category = new Category("Hello test category", 1, "Test description");

            using (var context = TestContext.CreateNewContext())
            {
                context.Categories.Add(category);
                await context.SaveChangesAsync();
            }
            var blogPost = RequestDbContext.BlogPosts.AsNoTracking().First();
            var message  = new UpdateBlogPostCommand()
            {
                Id           = blogPost.Id,
                Subject      = "Test new",
                ContentIntro = "Test",
                Content      = "Test",
                CategoryId   = category.Id
            };
            var builder        = new ExistingBlogPostBuilder(null);
            var handlerContext = TestContext.CreateHandlerContext <BlogPostSummaryViewModel>(RequestDbContext, CreateMapper());
            var handler        = new UpdateBlogPostCommandHandler(handlerContext, builder);

            var result = await handler.Handle(message, CancellationToken.None);

            Assert.Equal("Test new", result.Subject);
            Assert.Equal(category.Name, result.Category);
        }
Ejemplo n.º 2
0
        public async Task UpdateBlogPostCommandHandler_WithFile_UpdatesBlog()
        {
            var imageFactory   = TestContext.CreateImageService();
            var builder        = new ExistingBlogPostBuilder(imageFactory);
            var handlerContext = TestContext.CreateHandlerContext <BlogPostSummaryViewModel>(RequestDbContext, CreateMapper());
            var handler        = new UpdateBlogPostCommandHandler(handlerContext, builder);
            var fileMock       = TestContext.CreateFileMock();
            var file           = fileMock.Object;
            var category       = new Category("Hello test category", 1, "Test description");

            using (var context = TestContext.CreateNewContext())
            {
                context.Categories.Add(category);
                await context.SaveChangesAsync();
            }

            var blogPost = RequestDbContext.BlogPosts.AsNoTracking().First();
            var message  = new UpdateBlogPostCommand()
            {
                Id           = blogPost.Id,
                Subject      = "Test new",
                ContentIntro = "Test",
                Content      = "Test",
                File         = file,
                CategoryId   = category.Id
            };

            var result = await handler.Handle(message, CancellationToken.None);

            Assert.NotNull(result.Image.UriPath);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> EditAsync(Guid Id, UpdateBlogPostCommand command)
        {
            if (Id != command.Id)
            {
                return(BadRequest());
            }

            await Mediator.Send(command);

            return(NoContent());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> EditPost(EditBlogPostViewModel model)
        {
            var post = await _qpa.ProcessAsync(new GetBlogPostByIdQuery { Id = model.Id });

            if (post != null && ModelState.IsValid)
            {
                var ubpc = new UpdateBlogPostCommand();

                if (post.Author == null)
                {
                    var curUser = await _userManager.GetUserAsync(User);

                    var author = await _qpa.ProcessAsync(new GetAuthorByAppUserIdQuery { Id = curUser.Id });

                    ubpc.NewAuthor = author;
                }
                else
                {
                    ubpc.NewAuthor = post.Author; // keep the same author
                }

                ubpc.Id             = model.Id;
                ubpc.NewTitle       = model.Title;
                ubpc.NewDescription = model.Description;
                ubpc.NewContent     = model.Content;
                ubpc.LastModifiedAt = DateTime.Now;
                ubpc.NewPublishOn   = model.PublishOn;
                ubpc.NewPublic      = model.Public;

                // update the post

                var ubpcResult = await _cp.ProcessAsync(ubpc);

                if (ubpcResult.Succeeded)
                {
                    // do nothing
                    _logger.LogInformation("Successfully updated BlogPost Id {0}", model.Id);
                }
                else
                {
                    _logger.LogWarning("Unable to update BlogPost {0}", model.Id);
                    // an error
                    return(NotFound());
                }

                List <long> topicIds = new List <long>();

                if (model.TopicsList != null)
                {
                    foreach (var topic in model.TopicsList)
                    {
                        if (topic.IsSelected)
                        {
                            topicIds.Add(topic.TopicId);
                        }
                    }

                    var addTopicsResult =
                        await _cp.ProcessAsync(new SetBlogPostTopicsCommand
                    {
                        BlogPostId = post.Id,
                        TopicIds   = topicIds
                    });

                    if (addTopicsResult.Succeeded)
                    {
                        // do nothing
                        _logger.LogInformation("Successfully set Topics for BlogPost {0}", model.Id);
                    }
                    else
                    {
                        // log the error
                        _logger.LogInformation("Unable to set Topics for BlogPost {0}", model.Id);

                        return(NotFound());
                    }
                }
                else
                {
                    model.TopicsList = new List <TopicsCheckBox>();
                }

                List <long> categoryIds = new List <long>();

                if (model.CategoriesList != null)
                {
                    foreach (var category in model.CategoriesList)
                    {
                        if (category.IsSelected)
                        {
                            categoryIds.Add(category.CategoryId);
                        }
                    }

                    var addCategoriesResult =
                        await _cp.ProcessAsync(new SetBlogPostCategoriesCommand
                    {
                        BlogPostId  = post.Id,
                        CategoryIds = categoryIds
                    });

                    if (addCategoriesResult.Succeeded)
                    {
                        // do nothing
                        _logger.LogInformation("Successfully set Categories for BlogPost {0}", model.Id);
                    }
                    else
                    {
                        // log the error
                        _logger.LogWarning("Unable to set Categories for BlogPost {0}", model.Id);
                        return(NotFound());
                    }
                }
                else
                {
                    model.CategoriesList = new List <CategoriesCheckBox>();
                }

                ViewData["SavedMessage"] = "Post saved.";

                return(View(model));
            }
            else
            {
                if (post == null)
                {
                    _logger.LogWarning("Post to EditPost for update invoked for a non-existant BlogPost Id {0}", model.Id);
                    return(NotFound());
                }

                // these are needed for rendering the view when there
                // are not categories or topics created in the engine
                if (model.CategoriesList == null)
                {
                    model.CategoriesList = new List <CategoriesCheckBox>();
                }
                if (model.TopicsList == null)
                {
                    model.TopicsList = new List <TopicsCheckBox>();
                }

                return(View(model));
            }
        }
Ejemplo n.º 6
0
 public async Task <ActionResult <BlogPostSummaryViewModel> > Update([FromForm] UpdateBlogPostCommand command)
 => await _mediator.Send(command);
Ejemplo n.º 7
0
        public async Task <IActionResult> EditPost(EditBlogPostViewModel model)
        {
            // fix these queries, the query for author can select from the author table without the join

            var post = await _qpa.ProcessAsync(new GetBlogPostByIdQuery { Id = model.Id });

            var ubpc = new UpdateBlogPostCommand();

            if (post.Author == null)
            {
                var curUser = await _userManager.GetUserAsync(User);

                var author = await _qpa.ProcessAsync(new GetAuthorByAppUserIdQuery { Id = curUser.Id });

                ubpc.NewAuthor = author;
            }
            else
            {
                ubpc.NewAuthor = post.Author; // keep the same author
            }

            ubpc.Id             = model.Id;
            ubpc.NewTitle       = model.Title;
            ubpc.NewDescription = model.Description;
            ubpc.NewContent     = model.Content;
            ubpc.LastModifiedAt = DateTime.Now;
            ubpc.NewPublishOn   = model.PublishOn;
            ubpc.NewPublic      = model.Public;

            // update the post

            var ubpcResult = await _cp.ProcessAsync(ubpc);

            if (ubpcResult.Succeeded)
            {
                // do nothing
            }
            else
            {
                // an error
                return(NotFound());
            }

            List <long> featureIds = new List <long>();

            if (model.FeaturesList != null)
            {
                foreach (var feature in model.FeaturesList)
                {
                    if (feature.IsSelected)
                    {
                        featureIds.Add(feature.FeatureId);
                    }
                }

                var addFeaturesResult =
                    await _cp.ProcessAsync(new SetBlogPostFeaturesCommand
                {
                    BlogPostId = post.Id,
                    FeatureIds = featureIds
                });

                if (addFeaturesResult.Succeeded)
                {
                    // do nothing
                }
                else
                {
                    // log the error
                    return(NotFound());
                }
            }
            else
            {
                model.FeaturesList = new List <FeaturesCheckBox>();
            }

            List <long> categoryIds = new List <long>();

            if (model.CategoriesList != null)
            {
                foreach (var category in model.CategoriesList)
                {
                    if (category.IsSelected)
                    {
                        categoryIds.Add(category.CategoryId);
                    }
                }

                var addCategoriesResult =
                    await _cp.ProcessAsync(new SetBlogPostCategoriesCommand
                {
                    BlogPostId  = post.Id,
                    CategoryIds = categoryIds
                });

                if (addCategoriesResult.Succeeded)
                {
                    // do nothing
                }
                else
                {
                    // log the error
                    return(NotFound());
                }
            }
            else
            {
                model.CategoriesList = new List <CategoriesCheckBox>();
            }

            ViewData["SavedMessage"] = "Post saved.";

            return(View(model));
        }