Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }