Exemple #1
0
        public void UpdateTag(MyStoryContext dbContext, PostInput input, Post post)
        {
            if (post.Tags != null) post.Tags.Clear();

            if (input.Tags != null)
            {
                foreach (var item in input.Tags.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var tagText = item.Trim();

                    if (!string.IsNullOrWhiteSpace(tagText))
                    {
                        var cnt = dbContext.Tags.Count(t => t.TagText == tagText);
                        if (cnt > 0)
                        {
                            var tag = dbContext.Tags.First(t => t.TagText == tagText);
                            post.Tags.Add(tag);
                        }
                        else
                        {
                            post.Tags.Add(new Tag { TagText = tagText });
                        }
                    }
                }
            }
        }
        public void Valid_Model_Should_Be_Saved()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);

            var controllerContext = new Mock<ControllerContext>();
            controllerContext.SetupGet(x => x.HttpContext.Request.IsAuthenticated).Returns(true);
            controllerContext.SetupGet(x => x.HttpContext.User.Identity.Name).Returns("[email protected]");

            _controller = new PostController(new TagService());
            _controller.ControllerContext = controllerContext.Object;

            var postInput = new PostInput
            {
                Title = "title",
                Content = "content",
            };

            // Act
            var result = _controller.Write(postInput) as RedirectToRouteResult;

            // Assert
            _dbContext.Posts.Count().ShouldEqual(1);
            result.RouteValues["controller"].ShouldEqual("Home");
            result.RouteValues["action"].ShouldEqual("Index");
        }
        public void Invalid_Model_Should_Not_Save()
        {
            // Arrange
            _controller = new PostController();
            _controller.ModelState.AddModelError("modelerror", "modelerror");
            var postInput = new PostInput();

            // Act
            var result = _controller.Write(postInput) as ViewResult;

            // Assert
            result.ViewData.ModelState.IsValid.ShouldBeFalse();
            result.ViewName.ShouldEqual("Write");
        }
        public void Edit_Method_Should_Save_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController(new TagService());
            _controller.SetFakeControllerContext();

            // valueprovider is needed for TryUpdateModel() method
            FormCollection form = new FormCollection();
            _controller.ValueProvider = form.ToValueProvider();

            var input = new PostInput
            {
                Content = "edited content",
                Id = 1,
                Title = "edited title",
            };

            // Act
            var result = _controller.Edit(input) as RedirectToRouteResult;

            // Assert
            result.RouteValues["controller"].ShouldEqual("Post");
            result.RouteValues["action"].ShouldEqual("Detail");
            var post = _dbContext.Posts.SingleOrDefault(p => p.Id == 1);
            post.ShouldNotBeNull();
            post.Title.Contains("edited");
            post.Content.Contains("edited");
        }
Exemple #5
0
        public ActionResult Write(PostInput input)
        {
            if (!ModelState.IsValid)
                return View("Write", input);

            var blogId = GetCurrentBlog().Id;

            var post = Mapper.Map<PostInput, Post>(input);
            post.BlogId = blogId;
            post.DateCreated = post.DateModified = DateTime.Now;

            _tagService.UpdateTag(DbContext, input, post);

            DbContext.Posts.Add(post);
            DbContext.SaveChanges();

            return RedirectToAction("Index", "Home");
        }
Exemple #6
0
        public ActionResult Edit(PostInput input)
        {
            if (!ModelState.IsValid)
                return View("Edit", input);

            var post = DbContext.Posts.Single(p => p.Id == input.Id);

            if (TryUpdateModel(post, "", null, new string[]{"Tags"}))
            {
                post.DateModified = DateTime.Now;

                _tagService.UpdateTag(DbContext, input, post);

                DbContext.Entry(post).State = System.Data.EntityState.Modified;
                DbContext.SaveChanges();

                return RedirectToAction("Detail", "Post", new { id = input.Id });
            }

            return View("Edit", input);
        }