public BlogPost SaveBlogPost(BlogPost post)
        {
            //add a post...
            if (post.Id == 0)
            {
                //first assign the post a new id
                if (!context.BlogPosts.Any())
                {
                    post.Id = 1;
                }
                else
                {
                    post.Id = context.BlogPosts.Max(x => x.Id) + 1;
                }
                //then add it to the db and save changes
                context.BlogPosts.Add(post);
                context.SaveChanges();
                return post;
            }

            //otherwise post already exists and should be edited
            else
            {
                var oldPost = context.BlogPosts.Find(post.Id);
                if (oldPost != null)
                {
                    oldPost.Title = post.Title;
                    oldPost.Text = post.Text;
                    oldPost.Date = post.Date;
                    context.SaveChanges();
                }
                return oldPost;
            }
        }
        public void CanDeleteBlogPost()
        {
            var postToDelete = new BlogPost
            {
                Id = 1,
                Date = new DateTime(2014, 1, 1),
                RouteName = "blog-post",
                Text = "this is a sample",
                Title = "Sample"
            };

            var target = GetAdminController();
            var result = target.DeleteBlogPost(postToDelete.Id);
            blogMock.Verify(m => m.DeleteBlogPost(postToDelete.Id));
        }
        public void CanEditBlog()
        {
            var mockedBlogRepo = new Mock<IBlogPostRepository>();
            var mockedProjectRepo = new Mock<IPortfolioProjectRepository>();
            var blogPost = new BlogPost
            {
                Id = 1,
                Date = new DateTime(2012, 1, 1),
                RouteName = "sample-post",
                Text = "This is a sample",
                Title = "Sample Post"
            };
            mockedBlogRepo.Setup(m => m.GetBlogPost(1)).Returns(blogPost);

            var target = new AdminController(mockedBlogRepo.Object, mockedProjectRepo.Object);

            var result = (AdminBlogFull)target.AddOrEditBlogPost(1).ViewData.Model;
            Assert.AreEqual(result.Title, "Sample Post");
        }
Пример #4
0
        public ActionResult AddOrEditBlogPost(AdminBlogFull model)
        {
            if (ModelState.IsValid)
            {
                //convert data format
                var post = new BlogPost
                {
                    Date = DateTime.Parse(model.Date),
                    Id = model.Id,
                    Text = model.Text,
                    Title = model.Title,
                    RouteName = model.RouteName
                };

                _blogRepo.SaveBlogPost(post);
                return RedirectToAction("Blog");
            }

            //if model not valid, send user back to form with model
            return View(model);
        }