示例#1
0
        public IList<Article> GenerateArticles(int count)
        {
            var category = this.GenerateCategory();
            var tags = this.GenerateTagsCollection();

            var articles = new List<Article>();
            for (int i = 0; i < count; i++)
            {
                var article = new Article
                {
                    ID = i,
                    Title = "Title #" + i,
                    Content = "Content #" + i,
                    Category = category,
                    DateCreated = DateTime.Now.AddHours(rnd.Next(-5 * 365, 5 * 365)),
                    Tags = tags,
                    Author = new ApplicationUser()
                };

                articles.Add(article);
            }

            return articles;
        }
        public IHttpActionResult Create(ArticleProjection articleProjection)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest("Invalid article!");
            }

            var tags = this.GetTags(articleProjection);
            var categoryId = this.GetCategoryId(articleProjection.Category);
            var article = new Article()
            {
                Title = articleProjection.Title,
                Content = articleProjection.Content,
                DateCreated = DateTime.Now,
                CategoryID = categoryId,
                Tags = tags,
                AuthorID = this.GetUserId()
            };

            this.ForumSystemData.Articles.Add(article);
            this.ForumSystemData.SaveChanges();

            articleProjection.ID = article.ID;
            articleProjection.DateCreated = article.DateCreated;
            articleProjection.Tags = article.Tags.AsQueryable().Select(TagProjection.FromTag).ToList();

            return this.Ok(articleProjection);
        }