コード例 #1
0
        // POST api/news
        public IHttpActionResult Post([FromBody]NewsItemOutputModel newsItemOutput)
        {
            if (newsItemOutput == null)
            {
                this.ModelState.AddModelError("newsItem", "There is no news to add");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var userId = this.User.Identity.GetUserId();
            var newNewsItem = new NewsItem
               {
                   Content = newsItemOutput.Content,
                   PublishDate = newsItemOutput.PublishDate,
                   Title = newsItemOutput.Title,
                   UserId = userId,
               };

            this.NewsData.News.Add(newNewsItem);
            this.NewsData.SaveChanges();

            newsItemOutput.Id = newNewsItem.Id;
            return this.Created("DefaultApi", newsItemOutput);
        }
コード例 #2
0
        public void CreateNewsItemWithCorrectDataShouldCreatesAndReturnsAnItem()
        {
            var newsItem = new NewsItem
                {
                    Title = "New News", 
                    Content = "New News Content", 
                    PublishDate = new DateTime(2015, 09, 09)
                };

            this.data.News.Add(newsItem);
            this.data.SaveChanges();

            var createdItem = this.data.News.GetAll()
                .FirstOrDefault(n =>
                                n.Title == newsItem.Title &&
                                n.Content == newsItem.Content &&
                                n.PublishDate == newsItem.PublishDate);

            if (createdItem == null)
            {
                Assert.Fail("Items is not created");
            }

            Assert.AreEqual(createdItem.Title, newsItem.Title);
            Assert.AreEqual(createdItem.Content, newsItem.Content);
            Assert.AreEqual(createdItem.PublishDate, newsItem.PublishDate);
        }
コード例 #3
0
        public IHttpActionResult PostNewsItem(NewsBindingModel newsBindingModel)
        {
            if (newsBindingModel == null)
            {
                return this.BadRequest("News data is missing");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newNews = new NewsItem
                {
                    Title = newsBindingModel.Title, 
                    Content = newsBindingModel.Content, 
                    PublishDate = newsBindingModel.PublishDate
                };

            this.Data.News.Add(newNews);
            this.Data.SaveChanges();

            var newsView = this.Data.News.GetAll()
                .Where(n => n.Id == newNews.Id)
                .Select(NewsViewModel.Create())
                .FirstOrDefault();

            var uriLocation = new
                {
                    controller = "news",
                    id = newNews.Id
                };

            return this.CreatedAtRoute("DefaultApi", uriLocation, newsView);
        }
コード例 #4
0
        public IHttpActionResult PostNewsItem(NewsItem newsItem)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            this.Data.NewsRepository.Add(newsItem);
            this.Data.NewsRepository.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = newsItem.Id }, newsItem);
        }
コード例 #5
0
        public IHttpActionResult PutNewsItem(int id, NewsItem newsItem)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var item = this.Data.NewsRepository.GetById(id);
            if (id != newsItem.Id || item == null)
            {
                return BadRequest();
            }

            this.Data.NewsRepository.Update(newsItem);
            this.Data.NewsRepository.SaveChanges();
            return StatusCode(HttpStatusCode.OK);
        }
コード例 #6
0
        public void AddNewsItemThenGetTheNewItemSouldBeTheSame()
        {
            DbContext = new NewsDbContext();
            this.data = new NewsData(DbContext);

            var news = new NewsItem
            {
                Title = "Test",
                Content = "Test",
                PublishDate = DateTime.Now
            };

            this.data.News.Add(news);
            this.data.SaveChanges();

            Assert.AreEqual(news, this.data.News.All().OrderBy(n => n.Id).FirstOrDefault());
        }
コード例 #7
0
        public void CreateNewsItemWithIncorrectDataShouldReturnBadRequest()
        {
            NewsItem invalidNewsItem = new NewsItem
                {
                    Content = "Content",
                    PublishDate = DateTime.Now
                };

            this.data.News.Add(invalidNewsItem);
            this.data.SaveChanges();
        }
コード例 #8
0
        private void PopulateDatabase()
        {
            var newsBodyOne = this.GenerateBodyData(this.newsItemOne);
            var newsBodyTwo = this.GenerateBodyData(this.newsItemTwo);

            var newsOneResponse = this.client.PostAsync("api/news", newsBodyOne).Result;
            var newsTwoResponse = this.client.PostAsync("api/news", newsBodyTwo).Result;

            this.newsOne = newsOneResponse.Content.ReadAsAsync<NewsItem>().Result;
        }
コード例 #9
0
        public void ModifyExistingNewsItemWithValidData()
        {
            DbContext = new NewsDbContext();
            this.data = new NewsData(DbContext);

            var news = new NewsItem
            {
                Title = "Tesaat",
                Content = "Testaa",
                PublishDate = DateTime.Now
            };

            this.data.News.Add(news);
            this.data.SaveChanges();

            news.Title = "New title";
            this.data.SaveChanges();

            var itemAfterChange = this.data.News.All().FirstOrDefault();

            Assert.AreEqual("New title", itemAfterChange.Title);
        }