예제 #1
0
        public IHttpActionResult PostPost(CreatePost post)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            Models.Post postModel = Mapper.Map<Models.Post>(post);

            if (postModel.Published)
                postModel.PublishDate = DateTime.Now;

            postModel.BlogId = (int)CurrentUser.CurrentBlogId;
            postModel.UrlName = ServerTools.GenerateUrlFriendlyString(postModel.Title);
            //todo create tags

            Db.Posts.Add(postModel);
            Db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = post.Id }, post);
        }
예제 #2
0
        public IHttpActionResult PutPost(int id, CreatePost post)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != post.Id)
            {
                return BadRequest();
            }

            var postDb = Db.Posts.FirstOrDefault(p => p.Id == post.Id && p.BlogId == CurrentBlogId);

            if (postDb == null)
                return BadRequest("Post not found");

            Mapper.Map(post, postDb);
            postDb.UrlName = ServerTools.GenerateUrlFriendlyString(postDb.Title);

            Db.Entry(postDb).State = EntityState.Modified;

            try
            {
                Db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }