예제 #1
0
        public void Add_ValidArticle_ShouldArticleIdBiggerThanNull()
        {
            var article = new Article
            {
                Author = "Pesho",
                Category = new Category
                {
                    PictureUrl = "http://www.test.com/",
                    Title = "Testss"
                },
                Password = "******",
                Date = DateTime.Now
            };

            this.dbContext.Set<Article>().Add(article);
            this.dbContext.SaveChanges();
            Assert.IsTrue(article.Id > 0);
        }
예제 #2
0
        public void Add_WhenArticleIsValid_ShouldReturnNotZeroId()
        {
            int articleId;
            using (TransactionScope scope = new TransactionScope())
            {
                var article = new Article
                {
                    Author = "Pesho",
                    Category = new Category
                    {
                        PictureUrl = "http://www.test.com/",
                        Title = "Testss"
                    },
                    Password = "******",
                    Date = DateTime.Now
                };

                this.dbContext.Set<Article>().Add(article);
                this.dbContext.SaveChanges();
                scope.Complete();
                articleId = article.Id;
            }

            Assert.IsTrue(articleId != 0);

            var articleEntity = this.dbContext.Set<Article>().Find(articleId);
            Assert.IsNotNull(articleEntity);
        }
예제 #3
0
        public IHttpActionResult PostArticle(ArticleModel article)
        {
            if (article == null || !ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var tags = GenerateTags(article.ArticleHead);
            var newArticle = new Article
            {
                Heading = article.ArticleHead,
                Author = article.ArticleAuthor,
                Text = article.ArticleText,
                Date = DateTime.Now,
                CategoryId = article.Category,
                Password = article.Password
            };

            foreach (var tag in tags)
            {
                newArticle.Tags.Add(tag);
            }

            db.Articles.Add(newArticle);
            db.SaveChanges();

            return Ok(newArticle);
        }