Example #1
0
        public IHttpActionResult PutComment([FromUri] int commentId, EditCommentBindingModel comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Comment editComment = unitOfWork.Comments.Get(commentId);

            if (editComment == null)
            {
                return(BadRequest("Comment doesn't exist."));
            }

            editComment.Text     = comment.Text;
            editComment.DateTime = DateTime.Now;

            try
            {
                unitOfWork.Comments.Update(editComment);
                unitOfWork.Complete();
            }
            catch (DBConcurrencyException)
            {
                return(NotFound());
            }

            return(Ok(HttpStatusCode.OK));
        }
Example #2
0
        public ActionResult Edit(EditCommentBindingModel model)
        {
            if (ModelState.IsValid)
            {
                var comment = this.service.FindCommentById(model.Id);

                if (comment == null)
                {
                    return(HttpNotFound());
                }

                if (comment.Author.Id != User.Identity.GetUserId() && !User.IsInRole("admin"))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                this.service.EditComment(comment, model);

                return(RedirectToAction("Details", "Songs", new { id = comment.Song.Id }));
            }

            var viewModel = Mapper.Instance.Map <EditCommentBindingModel, EditCommentViewModel>(model);

            return(View(viewModel));
        }
Example #3
0
        public IHttpActionResult Put(int postId, int commentId, EditCommentBindingModel comment)
        {
            var existingPost = this.SocialNetworkData.Posts.All()
                               .FirstOrDefault(p => p.Id == postId);

            if (existingPost == null)
            {
                return(this.NotFound());
            }

            var existingComment = this.SocialNetworkData.Comments.All()
                                  .FirstOrDefault(c => c.Id == commentId);

            if (existingComment == null)
            {
                return(this.NotFound());
            }

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

            if (userId == null)
            {
                return(this.BadRequest("Invalid session token."));
            }

            if (existingComment.AuthorId != userId)
            {
                return(this.BadRequest("Not comment author."));
            }

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

            existingComment.Content = comment.CommentContent;
            this.SocialNetworkData.SaveChanges();

            comment.Id = commentId;
            return(this.Ok(comment));
        }
Example #4
0
 public void EditComment(Comment comment, EditCommentBindingModel model)
 {
     comment = Mapper.Instance.Map <EditCommentBindingModel, Comment>(model, comment);
     this.context.Entry(comment).State = EntityState.Modified;
     this.context.SaveChanges();
 }