Exemplo n.º 1
0
        public long Create(BlogInputModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var blog = new BlogEntity
            {
                Title   = model.Title,
                Content = model.Content,
                Created = DateTime.Now
            };

            _dbContext.Blogs.Add(blog);
            _dbContext.SaveChanges();
            return(blog.Id);
        }
Exemplo n.º 2
0
        public IActionResult AddBlog([FromBody] Blog blog)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _dbContext.Blogs.Add(blog);
            _dbContext.SaveChanges();
            return(Ok("Blog added"));
        }
        public void Add_EmptyTitle_ShouldFail()
        {
            Blog twitter;
            Post post;
            int  beforeInsertCount;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                bool hasTwitter = dbContext.Blogs
                                  .Where(b => b.Url.Contains("twitter"))
                                  .Count() > 0;
                if (!hasTwitter)
                {
                    dbContext.Blogs.Add(new Blog
                    {
                        Url = "www.twitter.com"
                    });
                    dbContext.SaveChanges();
                }
                twitter = dbContext.Blogs
                          .Where(b => b.Url.Contains("twitter"))
                          .Single();

                post = new Post
                {
                    Title   = "",
                    Content = "lahfpwqohavskjlsbatuwqhaps;vddsah",
                    Blog    = twitter
                };
                beforeInsertCount = dbContext.Posts.Count();
            }

            int result;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                result = service.Add(post.Title, post.Content, twitter.Url);
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                Assert.AreEqual(0, result);
                Assert.AreEqual(beforeInsertCount, dbContext.Posts.Count());
            }
        }