public IHttpActionResult CreateArticle(ArticleModel model)
        {
            if (!this.ModelState.IsValid)
            {
                //return this.BadRequest(this.ModelState);
            }

            var article = new Article()
            {
                AuthorId = this.User.Identity.GetUserId(),
                Title = model.Title,
                Content = model.Content,
                Category = this.GetCategory(model.Category),
                Tags = this.GetTags(model.Tags),
                DateCreated = DateTime.Now
            };
            this.data.Articles.Add(article);
            this.data.SaveChanges();
            model.Id = article.Id;

            //var location = new Uri(this.Url.Link("DefaultApi", new { id = model.Id }));

            //var response = this.Request.CreateResponse(HttpStatusCode.Created, article);
            //response.Headers.Location = new Uri(this.Url.Link("DefaultApi", new { id = model.Id }));
            //return response;

            return this.Ok(model);
        }
        private Article[] GenerateValidTestArticles(int count)
        {
            Article[] articles = new Article[count];
            var category = new Category()
            {
                ID = 1,
                Name = "Test Category"
            };

            var tags = new Tag[]{
                new Tag(){
                     ID = 1,
                     Name="tag"
                }
            };

            for (int i = 0; i < count; i++)
            {
               var article = new Article
                {
                    ID = i,
                    Title = " Title #" + i,
                    Content = "The Content #" + i,
                    Category = category,
                    DateCreated = DateTime.Now,
                    Tags = tags,
                    Author = new ApplicationUser()
                };
               articles[i] = article;
            }

            return articles;
        }
示例#3
0
        public IHttpActionResult Create(ArticleDataModel model)
        {
            var currentUserID = this.User.Identity.GetUserId();

            var category = GetCategory(model);
            var tags = GetTags(model);

            var article = new Article
            {
                AuthorID = currentUserID,
                DateCreated = DateTime.Now,
                Title = model.Title,
                Content = model.Content,
                CategoryID = category.ID,
                Tags = tags,
            };

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

            model.ID = article.ID;
            model.DateCreated = article.DateCreated;
            model.Tags = article.Tags.Select(t => t.Name);

            return Ok(model);
        }
        public IHttpActionResult Create(ArticleDataModel model)
        {
            var userID = this.User.Identity.GetUserId();
            var tags = GetTags(model);
            var category = GetCategory(model.Category);

            var article = new Article
            {
                Title = model.Title,
                Content = model.Content,
                DateCreated = DateTime.Now,
                CategoryID = category.ID,
                AuthorID = userID,
                Tags = tags,
            };
            this.data.Articles.Add(article);
            this.data.SaveChanges();

            model.ID = article.ID;
            model.DateCreated = article.DateCreated;
            model.Tags = article.Tags.AsQueryable()
                .Select(TagDataModel.FromTag).ToArray();

            return Ok(model);
        }
        public Article Add(Article articleToCreate)
        {
            this.articles.Add(articleToCreate);
            this.articles.SaveChanges();

            return this.articles.GetById(articleToCreate.Id);
        }
 public ArticleDetailsDataModel(Article article)
 {
     this.ID = article.ID;
     this.Title = article.Title;
     this.Content = article.Content;
     this.Category = article.Category.Name;
     this.DateCreated = article.DateCreated;
     this.Tags = article.Tags.Select(t => t.Name).ToArray();
     this.Comments = article.Comments.AsQueryable().Select(CommentDataModel.FromComment).ToArray();
     this.Likes = article.Likes.AsQueryable().Select(LikeDataModel.FromLike).ToArray();
 }
 public static ArticleResponseModel GetFromModel(Article a)
 {
     return new ArticleResponseModel
     {
         ID = a.Id,
         Title = a.Title,
         Content = a.Content,
         Category = a.Category.Name,
         DateCreated = a.DateCreated,
         Tags = a.Tags.Select(t => t.Name)
     };
 }
        public void PostArticle_WhenTitleIsEmpty_ShouldReturn400()
        {
            IArticlesData data = Mock.Create<IArticlesData>();

            Article article = new Article() { DateCreated = DateTime.Now };
            Mock.Arrange(() => data.Articles.Add(Arg.IsAny<Article>()));

            var server = new InMemoryHttpServer(this.inMemoryServerUrl, data);
            var response = server.Post("/api/articles", article);

            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void PostArticle_WhenValid_ShouldReturn201AndLocationHeader()
        {
            IArticlesData data = Mock.Create<IArticlesData>();

            Article article = new Article() { Title = "Test", DateCreated = DateTime.Now };
            Mock.Arrange(() => data.Articles.Add(Arg.IsAny<Article>()));

            var server = new InMemoryHttpServer(this.inMemoryServerUrl, data);
            var response = server.Post("/api/articles", article);

            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            Assert.IsNotNull(response.Headers.Location);
        }
 public ArticleDetailsViewModel(Article article)
 {
     this.Id = article.Id;
     this.Title = article.Title;
     this.Content = article.Content;
     this.Category = article.Category != null ? article.Category.Name : "";
     this.DateCreated = article.DateCreated;
     this.Tags = article.Tags.Select(t => t.Name).ToArray();            
     this.Likes = article.Likes.AsQueryable().Select(LikeViewModel.FromLike).ToArray();
     this.Comments = article.Comments.AsQueryable()
         .OrderBy(c => c.DateCreated)
         .Take(10)
         .Select(CommentViewModel.FromComment)
         .ToArray();
 }
        public IHttpActionResult Post(ArticleRequestModel article)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest();
            }

            var currentUserId = this.User.Identity.GetUserId();

            var articleToAdd = new Article
            {
                AuthorId = currentUserId,
                Category = this.categories.GetByName(article.Category),
                Content = article.Content,
                DateCreated = DateTime.Now,
                Title = article.Title,
                Tags = this.tags.GetCollection(article.Tags)
            };

            this.articlesService.Add(articleToAdd);

            return this.Ok();
        }
        public IHttpActionResult Create(ArticleViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                return this.BadRequest("Article title cannot be null or empty!");
            }

            var userID = this.User.Identity.GetUserId();
            var tags = GetTags(model);
            var category = GetOrCreateCategory(model.Category);

            var article = new Article
            {
                Title = model.Title,
                Content = model.Content,
                DateCreated = DateTime.Now,
                CategoryId = category.Id,
                AuthorId = userID,
                Tags = tags,
            };
            this.data.Articles.Add(article);
            this.data.SaveChanges();

            model.Id = article.Id;
            model.DateCreated = article.DateCreated;
            model.Tags = article.Tags.Select(t => t.Name);

            return this.CreatedAtRoute("DefaultApi", new { id = model.Id }, model);
        }
 public void Add(Article article)
 {
     this.articles.Add(article);
     this.articles.SaveChanges();
 }