Exemplo n.º 1
0
        public IHttpActionResult CreateNew(CreateNewInputModel inputModel)
        {
            if (inputModel == null)
            {
                return this.BadRequest();
            }

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

            var news = new News { Content = inputModel.Content, Title = inputModel.Title, PublishedAt = DateTime.Now };

            this.Data.News.Add(news);
            this.Data.SaveChanges();
            var newOutputModel = new GetAllNewsViewModel() { Id = news.Id, Content = news.Content, Title = news.Title, PublishedDate = news.PublishedAt};
            return this.CreatedAtRoute("DefaultApi", new { controller = "news", id = newOutputModel.Id }, newOutputModel);
        }
Exemplo n.º 2
0
        public IHttpActionResult UpdateNewById(int id, UpdateNewInputModel inputModel)
        {
            if (inputModel == null)
            {
                return this.BadRequest();
            }

            var news = this.Data.News.All().FirstOrDefault(n => n.Id == id);
            if (news == null)
            {
                return this.NotFound();
            }

            if (inputModel.Title != null)
            {
                news.Title = inputModel.Title;
            }

            if (inputModel.Content != null)
            {
                news.Content = inputModel.Content;
            }

            if (inputModel.PublishedAt != null)
            {
                news.PublishedAt = Convert.ToDateTime(inputModel.PublishedAt);
            }

            this.Data.News.Update(news);

            this.Data.SaveChanges();

            var newOutputModel = new GetAllNewsViewModel() { Id = news.Id, Content = news.Content, Title = news.Title, PublishedDate = news.PublishedAt };
            return this.Ok(newOutputModel);
        }