Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public IHttpActionResult PutArticle(int id, ArticleModel article)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var existingArticle = this.db.Articles.FirstOrDefault(a => a.Id == id);

            if (existingArticle == null)
            {
                return BadRequest("Such article does not exists!");
            }
            else if (existingArticle.Password != article.Password)
            {
                return BadRequest("Invalid password!");
            }

            existingArticle.Heading = article.ArticleHead;
            existingArticle.Text = article.ArticleText;
            existingArticle.Date = DateTime.Now;
            existingArticle.Author = article.ArticleAuthor;
            existingArticle.CategoryId = article.Category;

            this.db.SaveChanges();

            return Ok();
            //return Ok(existingArticle);
        }